Write a program to evaluate infix expressions. An infix expression looks like th
ID: 640275 • Letter: W
Question
Write a program to evaluate infix expressions. An infix expression looks like the following:
9 * (5 - 4) + 2 / 6
This program must first convert the infix expression to a postfix expression, and then evaluate the postfix expression. A postfix expression for the above infix expression looks like the following:
9 5 4 - * 2 6 / +
For this assignment you will need to implement and apply one stack to convert from infix to postfix, and implement and apply a second stack to evaluate the postfix expression.
Please complete exercises 12.12 and 12.13 form your Deitel& Deitel C How to Program textbook. These exercises provide you with the algorithms required to perform the correct conversions and evaluations. Create a single program only, which combines both exercises. Also, be sure to allow the user to continue to enter infix expressions until they want to quit the program.
BONUS:
Provide appropriate test cases and drivers for each function in your stack application to receive up to 10 bonus points. For an additional 10 points allow for your program to accept multi-digit operands and unary sign operators (+ and -).
The code must be written in C.
Explanation / Answer
class Evaluator { public: Evaluator( const string & s ) : str( s ) { opStack.push_back( EOL ); } // The only publicly visible routine NumericType getValue( ); // Do the evaluation private: stack opStack; // Operator stack for conversion stack postFixStack; // Stack for postfix machine istringstream str; // String stream // Internal routines NumericType getTop( ); // Get top of postfix stack void binaryOp( TokenType topOp ); // Process an operator void processToken(const Token &lastToken); // Handle LastToken }; // Public routine that performs the evaluation. // Examines the postfix stack to see if a single result // is left and if so, returns it; otherwise prints error. template NumericType Evaluator::getValue( ) { Tokenizer tok( str ); Token lastToken; do { lastToken = tok.getToken( ); processToken( lastToken ); } while( lastToken.getType( ) != EOL ); if( postFixStack.isEmpty( ) ) { cerrRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.