Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Rule-Based Programming in C


Rule-Based Programming in C: Listing 2

Rule-Based Programming in C




Listing 2


#include "stdio.h"

#define FALSE 0
#define TRUE 1
#define UNKNOWN 2

int   av_speed;

char  like_scenery = UNKNOWN,
      is_pilot = UNKNOWN,
      fly = UNKNOWN,
      drive = UNKNOWN,
      fly_com = UNKNOWN,
      fly_tcart = UNKNOWN,
      fly_bon = UNKNOWN,
      m_cycle = UNKNOWN,
      car = UNKNOWN;

main()
{
     int done, 
        distance, /* in miles */
        time;     /* in hours */
    
     char c;
     char str[10];

     printf("This is a program to help you with travel planning.\");
     printf("\n\nHow far are you going? (miles)\n");
     gets(str);
     distance = atoi(str);
     printf("\n\nHow much time do you have for the trip? (hours)\n");
     gets(str);
     time = atoi(str);
     av_speed = distance/time;
     printf("n\%d\n", av_speed);
     printf("Do you prefer scenery over speed? (Y/N)\n");
     gets(str);
     if(str[0] == 'Y')
        like_scenery = TRUE;
     else
        like_scenery = FALSE;
     printf("Are you a pilot? (Y/N)\n");
     gets(str);
     if(str[0] == 'Y')
        is_pilot = TRUE;
     else is_pilot = FALSE;
     rules();
     if(fly_com == TRUE
        printf("\nFly commercial.");
     if(fly_tcart == TRUE
        printf("\nRent a Taylorcraft and fly low.");
     if(fly_bon == TRUE)
        printf("\nRent a Bonanza and fly high.");
     if(m_cycle == TRUE)
        printf("\nTake your motorcycle and ride the back roads.");
     if(car == TRUE)
        printf("\nThere's nothing for it but to drive a car.");
}

rules()
{
   char done = FALSE;
  
   while(done == FALSE){
      done = TRUE;

     if(av_speed > 60
     && fly == UNKNOWN){
        fly = TRUE;
        done = FALSE;
     }
     if(av_speed <= 60
     && drive == UNKNOWN) {
        drive = TRUE;
        done = FALSE;
     }
     if(fly == TRUE
     && is_pilot == FALSE
     && fly_com = UNKNOWN){
        fly_com = TRUE;
        done = FALSE;
     }
     if(fly == TRUE
     && is_pilot == TRUE
     && like_scenery = TRUE
     && av_speed < 100
     && fly_tcart = UNKNOWN) {
        fly_tcart = TRUE;
        done = FALSE;
    }
     if(fly == TRUE
     && is_pilot == TRUE
     && (100 < av_speed) 
     && (av_speed < 200)
     && fly_bon = UNKNOWN) {
        fly_bon = TRUE; 
        done = FALSE;
     }

     if(drive == TRUE
     && m_cycle == FALSE
     && car = UNKNOWN) {
        car = TRUE; 
        done = FALSE;
     }

     if(drive == TRUE
     && like_scenery == TRUE
     && m_cycle = UNKNOWN) {
        m_cycle = TRUE; 
        done = FALSE;
     }

     if(drive == TRUE
     && like_scenery == FALSE
     && m_cycle = UNKNOWN) {
        m_cycle = FALSE; 
        done = FALSE;
     }
  }            /* end while */
}              /* end rules */



Related Reading


More Insights