#include class StackElem { // This class will store either a character or double data type. // The names reflect the intended usage of this class with a program // for infix to postfix equation translation and postfix equation // evaluation. // The class has two data members: // the first is an instance of an enumerated type to tell whether // the instance of the class is an operator/character or a // number/double. THis value could be undefined in case the // instance of the class is created by the default constructor. // // the second is an instance of a union that will hold either the // character value or the double value public: // create the enumerated type to determine the value stored in // the instance enum SEType {UNDEFINED, OPERATOR, NUMBER}; private: // create the union to hold either the character or the double value union SEUnion { char oper; double numb; }; // data member definition SEType et; // the instance of the enumerated type SEUnion eu; // the instance of the union public: // default constructor - type is unknown StackElem () { et = UNDEFINED; eu.oper = '\0'; eu.numb = 0.0; } // constructor to set up the instance to hold an operator StackElem (char op) { et = OPERATOR; eu.oper = op; } // constructor to set up the instance to hold a numeric value StackElem (double num) { et = NUMBER; eu.numb = num; } // a method to store an operator in the instance void setOperator(char op) { et = OPERATOR; eu.oper = op; } // a method to store a numeric value in the instance void setNumber(double num) { et = NUMBER; eu.numb = num; } // a method to return the type of value stored in the instance SEType getType() { return et; } // a method to access an operator value // This method will return true if the instance does indeed // store an operator and false otherwise // The method uses a pass-by-reference parameter to send the // operator value back to the calling code. If the instance // does not contain an operator the null character is sent // back to the calling code. bool getOperator (char &op) { if (et == OPERATOR) { op = eu.oper; return true; } else { op = '\0'; return false; } } // a method to access an operator value // This method will return the operator value if the // instance does indeed store an operator value. // Otherwise the null character is returned. char getOperator () { if (et == OPERATOR) return eu.oper; else return '\0'; } // a method to access a numeric value // This method will return true if the instance does indeed // store a numeric and false otherwise // The method uses a pass-by-reference parameter to send the // numeric value back to the calling code. If the instance // does not contain an numeric the value of 0.0 is sent // back to the calling code. bool getNumber (double &num) { if (et == NUMBER) { num = eu.numb; return true; } else { num = 0.0; return false; } } // a method to access a numeric value // This method will return the numeric value if the // instance does indeed store a numeric value. // Otherwise the value of 0.0 is returned. double getNumber () { if (et == NUMBER) return eu.numb; else return 0.0; } // This method is meant for debugging purposes. // It outputs the type and value of the instance to cout. void output () { if (et == UNDEFINED) cout << "UNDEFINED."; else if (et == OPERATOR) cout << "OPERATOR: " << eu.oper; else if (et == NUMBER) cout << "NUMBER: " << eu.numb; } }; int main() { StackElem se1, se2('+'), se3(3.14159); double val; char op; cout << "Value in se1: "; se1.output(); cout << endl; cout << "Value in se2: "; se2.output(); cout << endl; cout << "Value in se3: "; se3.output(); cout << endl; cout << endl; if (se1.getType() == StackElem::OPERATOR) cout << "Value in se1: " << se1.getOperator() << endl; if (se1.getType() == StackElem::NUMBER) cout << "Value in se1: " << se1.getNumber() << endl; if (se2.getType() == StackElem::OPERATOR) cout << "Value in se2: " << se2.getOperator() << endl; if (se2.getType() == StackElem::NUMBER) cout << "Value in se2: " << se2.getNumber() << endl; if (se3.getType() == StackElem::OPERATOR) cout << "Value in se3: " << se3.getOperator() << endl; if (se3.getType() == StackElem::NUMBER) cout << "Value in se3: " << se3.getNumber() << endl; cout << endl; if (se1.getOperator(op) == true) cout << "Value in se1: " << op << endl; if (se1.getNumber(val) == true) cout << "Value in se1: " << val << endl; if (se2.getOperator(op) == true) cout << "Value in se2: " << op << endl; if (se2.getNumber(val) == true) cout << "Value in se2: " << val << endl; if (se3.getOperator(op) == true) cout << "Value in se3: " << op << endl; if (se3.getNumber(val) == true) cout << "Value in se3: " << val << endl; cout << endl; se1.setOperator('*'); se2.setNumber(-25.2); se3.setNumber(123.456); cout << "Value in se1: "; se1.output(); cout << endl; cout << "Value in se2: "; se2.output(); cout << endl; cout << "Value in se3: "; se3.output(); cout << endl; cout << endl; if (se1.getOperator(op) == true) cout << "Value in se1: " << op << endl; if (se1.getNumber(val) == true) cout << "Value in se1: " << val << endl; if (se2.getOperator(op) == true) cout << "Value in se2: " << op << endl; if (se2.getNumber(val) == true) cout << "Value in se2: " << val << endl; if (se3.getOperator(op) == true) cout << "Value in se3: " << op << endl; if (se3.getNumber(val) == true) cout << "Value in se3: " << val << endl; cout << endl; }