home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / tutorial / cpptutor / source / words.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-15  |  5.8 KB  |  202 lines

  1. #include <iostream.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include "words.h"
  5. #include "clock.h"
  6.  
  7. extern clock time_of_day;
  8.  
  9.      // This function reads a line of text from the keyboard, parses
  10.      //  it, and does some limited checking.  Only the first two
  11.      //  words are considered.
  12. void words::get_command(void)
  13. {
  14. enum word wd1, wd2;
  15.  
  16.    do {
  17.       time_of_day.inc_and_print_time();
  18.       verb = (enum word)0;
  19.       noun = (enum word)0;
  20.       read_a_line(wd1, wd2);              // Get a line from the player
  21.       if (wd1) {                          // If there is a value for wd1
  22.          if (is_a_verb(wd1)) verb = wd1;  //   it is a verb
  23.          if (is_a_noun(wd1)) noun = wd1;  //   or a noun.
  24.       }
  25.  
  26.       if (wd2) {                          // If there is a value for wd2
  27.          if (is_a_verb(wd2)) {
  28.             if (verb == 0)
  29.                verb = wd2;                // it is a verb
  30.             else {
  31.                verb = noun = (enum word)0; // Two verbs, illegal
  32.                cout << "Two verbs are illegal, ignored!\n";
  33.             }
  34.          }
  35.          if (is_a_noun(wd2)) {
  36.             if (noun == 0)
  37.                noun = wd2;                //  It is a noun.
  38.             else {
  39.                verb = noun = (enum word)0;
  40.                   cout << "Two nouns are illegal, ignored!\n";
  41.             }
  42.          }
  43.       }
  44.  
  45.       if ((verb == 0) && (noun != 0)) {
  46.          verb = noun = (enum word)0;
  47.          cout << "A verb is required, ignored!\n";
  48.       }
  49.    } while (verb == 0);
  50. }
  51.  
  52.  
  53.  
  54.  
  55.      // This function reads words in ASCII form from the keyboard
  56.      //  ignoring any words after two have been read.  The words
  57.      //  are checked to see if they are in the dictionary as de-
  58.      //  fined by the enumeration variable named "word".
  59.  
  60. static char input_line[80];  // Global, so next func can use it
  61. static int  start_col;
  62. void words::read_a_line(word &wd1, word &wd2)
  63. {
  64. char string1[25], string2[25], string3[25];
  65. char last_char;
  66.  
  67.    start_col = 0;
  68.    cin.getline(input_line, 80);               // Get a line of input
  69.    last_char = get_an_ASCII_word(string1);    // Get first word
  70.    if (last_char != 0) {
  71.       last_char = get_an_ASCII_word(string2); // Get second word
  72.       while (last_char != 0) {    // Ignore all trailing words
  73.          last_char = get_an_ASCII_word(string3);
  74.       }
  75.    } else {
  76.       string2[0] = 0;                         // No second word
  77.    }
  78.  
  79.    wd1 = (enum word)find_in_dictionary(string1);
  80.    wd2 = (enum word)find_in_dictionary(string2);
  81.  
  82. }
  83.  
  84.  
  85.  
  86.  
  87.      // This function reads a string, after ignoring the leading
  88.      //  blanks.  The string is terminated when any character is
  89.      //  read that is not alphabetic.  All characters are converted
  90.      //  to lower case for internal use to allow typing flexibility.
  91.  
  92. int words::get_an_ASCII_word(char in_string[])
  93. {
  94. int char_count = 0;
  95. int char_found = FALSE;
  96. char c; 
  97.    
  98.    for (int index = start_col ; index < 80 ; index++) {
  99.       c = tolower(input_line[index]);
  100.       if (!c) {                  // End of line found
  101.          in_string[char_count] = 0;
  102.          return c;
  103.       }
  104.       if (isalpha(c) && char_count < 25) {
  105.          in_string[char_count++] = c;
  106.          char_found = TRUE;
  107.       } else {
  108.          if (isspace(c) && !char_found)
  109.             ;                           // Ignore leading blanks
  110.          else {
  111.             in_string[char_count] = 0;  // ASCIIZ terminator 
  112.             start_col = index;
  113.             return c;
  114.          }
  115.       }
  116.    }
  117.    return 0;
  118. }
  119.  
  120.  
  121.  
  122.  
  123.      // This function uses the dictionary pairs to convert the
  124.      //  ASCII input strings into the internal enumeration values.
  125.      //  This list must be maintained along with the enumerated
  126.      //  type "word".
  127.  
  128. struct dict_pair {
  129.    char dict_string[10];
  130.    word found_word;
  131. };
  132.  
  133. dict_pair pair[] = {"north"    ,north,
  134.                     "n"        ,north,
  135.                     "east"     ,east,
  136.                     "e"        ,east,
  137.                     "south"    ,south,
  138.                     "s"        ,south,
  139.                     "west"     ,west,
  140.                     "w"        ,west,
  141.                     "drop"     ,drop,
  142.                     "get"      ,get,
  143.                     "look"     ,look,
  144.                     "inventory",inventory,
  145.                     "read"     ,read,
  146.                     "buy"      ,buy,
  147.                     "help"     ,help,
  148.                     "quit"     ,quit,
  149.                     "keys"     ,keys,
  150.                     "candy"    ,candy,
  151.                     "ticket"   ,ticket,
  152.                     "money"    ,money,
  153.                     "monitor"  ,monitor,
  154.                     "paper"    ,paper,
  155.             ""         ,(enum word)0 };   // List terminator
  156.  
  157. int words::find_in_dictionary(char in_string[])
  158. {
  159. dict_pair *pointer = &pair[0];
  160.  
  161.    if (in_string[0] == 0) return 0; // No string to look up
  162.    do {
  163.       if (strcmp(in_string, pointer->dict_string) == 0)
  164.          return pointer->found_word;
  165.       pointer = pointer + 1;        // Next word in list
  166.    } while (pointer->found_word);   // End of word list
  167.    cout << "I don't know what " << in_string << " is.\n";
  168.    return 0;                        // Word not found in list
  169. }
  170.  
  171.  
  172.  
  173.     // Is the input word a verb?
  174. int words::is_a_verb(enum word input_word)
  175. {
  176.    return ((input_word >= north) && (input_word <= quit));
  177. }
  178.  
  179.  
  180.  
  181.     // Is the input word a noun?
  182. int words::is_a_noun(enum word input_word)
  183. {
  184.    return ((input_word >= keys) && (input_word <= paper));
  185. }
  186.  
  187.  
  188.  
  189.     // Is the input word a direction?
  190. int words::is_a_direction(enum word input_word)
  191. {
  192.    return ((input_word >= north) && (input_word <= west));
  193. }
  194.  
  195.  
  196.  
  197.     // Is the input word a operation?
  198. int words::is_an_operation(enum word input_word)
  199. {
  200.    return ((input_word >= drop) && (input_word <= quit));
  201. }
  202.