//////////////////////////////////////////////////////// // // High School Programming Contest 2000 // // Implementation of level 0 of the takehome problem. // // Author: Chad Barb [ hspc@cs.utah.edu ] // //////////////////////////////////////////////////////// // You may replace these with iostream and string. #include // For cin and cout classes #include // For strcmp() function #include // For exit() // Define a "Token" type that may have of one of the listed values. enum Token { TOKEN_ZERO, TOKEN_ONE, TOKEN_TWO, TOKEN_THREE, TOKEN_FOUR, TOKEN_FIVE, TOKEN_SIX, TOKEN_SEVEN, TOKEN_EIGHT, TOKEN_NINE, TOKEN_PLUS, TOKEN_MINUS, TOKEN_UNKNOWN // add your own tokens here. }; // function prototypes Token getSymbol(); int getNumber(); int lineValue(); ////////////////////////////////////////// // // the main function of the program. // ////////////////////////////////////////// int main() { // infinite loop. // getToken() will exit() when it receives a "bye" token. while ( true ) { // input and evaluate a line, then output it cout << lineValue() << endl; } return 0; } ///////////////////////////////////////////////////////////////////// // // Called repeatedly by main() to input and evaluate a single line. // Returns the result of the operation on that line. // ///////////////////////////////////////////////////////////////////// int lineValue() { int operand1, operand2; // the operands of the operation Token operation; // the operation type // get the operation specifics operand1 = getNumber(); operation = getSymbol(); operand2 = getNumber(); // Ignore anything which remains on this line, so erroneous input doesn't // mess up anything further (up to 1024 characters). // This is not required, since the program will not receive erroneous input // in normal use, but it may facilitate human testing somewhat. cin.ignore( 1024, '\n' ); // do it, based on the operation switch( operation ) { case TOKEN_PLUS: return ( operand1 + operand2 ); case TOKEN_MINUS: return ( operand1 - operand2 ); default: return 0; } } ////////////////////////////////////////////////////////////////////////// // // Called to get a number from cin (stdin). This can call getToken() // repeatedly to accomplish its task. In the simple case, it calls it // once, but once you implement multi-word numbers (like "one hundred"), // you will need some way of deciding when numbers end. // ////////////////////////////////////////////////////////////////////////// int getNumber() { int inToken; // get the next symbol inToken = getSymbol(); // decide what should be returned based on the received token. switch( inToken ) { case TOKEN_ZERO: return 0; break; case TOKEN_ONE: return 1; break; case TOKEN_TWO: return 2; break; case TOKEN_THREE: return 3; break; case TOKEN_FOUR: return 4; break; case TOKEN_FIVE: return 5; break; case TOKEN_SIX: return 6; break; case TOKEN_SEVEN: return 7; break; case TOKEN_EIGHT: return 8; break; case TOKEN_NINE: return 9; break; default: return 0; } } ///////////////////////////////////////////////// // // Gets a single symbol from cin (stdin) // ///////////////////////////////////////////////// Token getSymbol() { char thisSymbol[1024]; // a temporary string to store the input symbol // get the next symbol (all characters up to the next white space) cin >> thisSymbol; if ( strcmp( thisSymbol, "bye" ) == 0 ) exit( 0 ); // exit the program immediately. else if ( strcmp( thisSymbol, "zero" ) == 0 ) return TOKEN_ZERO; else if ( strcmp( thisSymbol, "one" ) == 0 ) return TOKEN_ONE; else if ( strcmp( thisSymbol, "two" ) == 0 ) return TOKEN_TWO; else if ( strcmp( thisSymbol, "three" ) == 0 ) return TOKEN_THREE; else if ( strcmp( thisSymbol, "four" ) == 0 ) return TOKEN_FOUR; else if ( strcmp( thisSymbol, "five" ) == 0 ) return TOKEN_FIVE; else if ( strcmp( thisSymbol, "six" ) == 0 ) return TOKEN_SIX; else if ( strcmp( thisSymbol, "seven" ) == 0 ) return TOKEN_SEVEN; else if ( strcmp( thisSymbol, "eight" ) == 0 ) return TOKEN_EIGHT; else if ( strcmp( thisSymbol, "nine" ) == 0 ) return TOKEN_NINE; else if ( strcmp( thisSymbol, "plus" ) == 0 ) return TOKEN_PLUS; else if ( strcmp( thisSymbol, "minus" ) == 0 ) return TOKEN_MINUS; else // the token type is unknown to the system return TOKEN_UNKNOWN; }