// Expression.h // tom bailey 28 oct 08 // Modified to remove verbosity of constructors and // destructors. // Add value() stub. // tom bailey 7 mar 07 // Modified to limit includes. // Add two validity checks to read infix code. // tom bailey 19 oct 05 // Modified to minimize code. // Remove read prefix expressions, print postfix, evaluate. // tom bailey 5 apr 04 // Add read of marked prefix expression. // Add preorder print of the expression. // Destructors are verbose. // tom bailey 15 jan 04 // Remove preOrder printout. // Remove read of prefix expression. // Add read of fully parenthesized infix expression. // Comment out vebosity of destructors. // tom bailey 21 nov 03 // Isolate the Expression class in a header file. // Add preOrder and postOrder printouts. // tom bailey 13 jan 03 // Use inheritance to define an arithmetic expression. // This defines a tree representation for the expression // that can be used to display or evaluate. #ifndef EXPRESSION_H_ #define EXPRESSION_H_ #include using std::ostream; using std::istream; using std::cin; using std::cout; using std::endl; #include using std::string; class Expression { public: Expression() { } virtual ~Expression() { } virtual void preOrderPrint( ostream & ) const = 0; virtual long value() = 0; protected: }; class Atom : public Expression { public: Atom( long aVal ) : val( aVal ) { } virtual ~Atom() { } virtual void preOrderPrint( ostream & outfile ) const { outfile << val; } long value() { return 123; } protected: long val; }; class BinaryOperation : public Expression { public: BinaryOperation( Expression & aLeft, char aSymbol, Expression & aRight ) : left( aLeft ), symbol( aSymbol ), right( aRight ) { } virtual ~BinaryOperation() { delete & left; delete & right; } virtual void preOrderPrint( ostream & outfile ) const { outfile << symbol << " "; this->left.preOrderPrint( outfile ); outfile << " "; this->right.preOrderPrint( outfile ); } protected: char symbol; Expression & left; Expression & right; }; class Times : public BinaryOperation { public: Times( Expression & aLeft, Expression & aRight ) : BinaryOperation( aLeft, '*', aRight ) { } virtual ~Times() { } long value() { return 123; } }; class Plus : public BinaryOperation { public: Plus( Expression & aLeft, Expression & aRight ) : BinaryOperation( aLeft, '+', aRight ) { } virtual ~Plus() { } long value() { return 123; } }; // Read one valid, marked, fully parenthesized infix expression // and return a reference to an Expression that represents // the input expression. // Client code must delete the Expression whose reference is // returned. Expression & readFPIE( istream & infile ) { static string ops( "*+" ); char symbol; infile >> symbol; if( symbol == 'n' ) { long number; infile >> number; return * new Atom( number ); } else if( symbol == '(' ) { Expression & left = readFPIE( infile ); char op; infile >> op; if( ops.find( op ) == string::npos ) { cout << "Unknown operator symbol" << endl; exit(1); } Expression & right = readFPIE( infile ); infile >> symbol; // read the ending right parenthesis if( symbol != ')' ) { cout << "Invalid symbol" << endl; exit(1); } switch( op ) { case '*': return * new Times( left, right ); case '+': return * new Plus( left, right ); default: exit(1); } } else { cout << "Invalid symbol" << endl; exit(1); } } #endif