/*/ *********************************************************************************** // Author: Suzuki and Ana Pena // // This program reads an expression from // the keyboard, makes it to postfix, sends // to a file and then reads from this file // and evaluates the expression // // last update on 9:30 Monday, April 28th 2003 ********************************************************************************** /*/ #include #include #include #include using namespace std; bool Read_and_evaluate(istream&); // PRECONDITION: it receives a expression fully parenthesized. // POSTCONDITION: this function will returN false if there // is anything wrong with the input expression. Errors // such as unbalanced parentese, letters in the input, // empty line. If nothing is wrong, the function will // read the expression, make is postfix and send to a // temp.txt file void Evaluate(); // PRECONDITION: there is a file with an expression fully // parenthesized in postfix notation. // POSTCONDITION: it reads the file, evaluates and display it. bool More(); // PRECONDITION: none // POSTCONDITION: promps if the user wants to evaluate more // expressions bool Precedence(stack& operations, istream& ins); // PRECONDITION: it receives stack and istream // POSTCONDITION: returns false or true deprending on which // precedence is higher. Is stack has a higher precedence, // it returns true and false if istream has higher precedence // on the operation void Warning(int many_left_parentese, int many_right_parentese); // PRECONDITION: it receives two numbers that represent the // amount of parentese that there is in the expression // POSTCONDITION: it display which parentese is missing. void main() { do { cout<<"Please enter the expression :"; if( Read_and_evaluate(cin)) Evaluate(); } while(More()); } bool More() { char dummy; do { cout<<"Do you want to do more operations(y/n)?:"; cin>>dummy; if(toupper(dummy) == 'Y') { cin.get();//clean the buffer system("cls"); //*this is exremelly necessary*// return true; } else if(toupper(dummy) == 'N') { return false; } else if( toupper(dummy) != 'Y' && toupper(dummy) != 'N' ) { cout<<"Wrong entry. Only 'Y' and 'N' are possible"< operations; double operand; char symbol;// /*-+ char dummy; //while not the endl while (ins.peek() != '\n') { // control the input of letters if(isalpha( ins.peek() )) { cin>>dummy; cout<<" ERROR: letter '"<> symbol; operations.push(symbol); } // digit or . else if (isdigit(ins.peek()) || (ins.peek() == '.')) { ins >> operand; OutFile << operand << " "; } else if (strchr("*+/-", ins.peek()) != NULL) { while (!( operations.empty() || operations.top() == '(' || Precedence(operations, ins))) { OutFile << operations.top() << " "; operations.pop(); } ins >> symbol; operations.push(symbol); } else { ins >> symbol; //unbalance parentese if (operations.empty() ) { Warning(left_par, right_par); return false; } while ( operations.top() != '(' ) { OutFile << operations.top() << " "; operations.pop(); //unbalanced parentese if ( operations.empty() ) { Warning( left_par, right_par); return false; } } operations.pop(); } } while (!operations.empty()) { //unbalanced parentese if (operations.top() == '(') { Warning( left_par, right_par); return false; } OutFile << operations.top() << " "; operations.pop(); } OutFile.close(); return true; } void Evaluate() { ifstream inFile; inFile.open("Temp.txt"); char symbol; double operand1; double operand2; double numb; stack numbers; do { // digit if(isdigit(inFile.peek())) { inFile >> numb; numbers.push(numb); } else { inFile >> symbol; operand2 = numbers.top( ); numbers.pop( ); operand1 = numbers.top( ); numbers.pop( ); switch (symbol) { case '+': numbers.push(operand1 + operand2); break; case '-': numbers.push(operand1 - operand2); break; case '*': numbers.push(operand1 * operand2); break; case '/': numbers.push(operand1 / operand2); break; } } inFile.ignore(); }while (inFile.peek() != -1); inFile.close(); cout<<"Result = "<< numbers.top()<& operations, istream& ins ) { return ((operations.top() == '+' || operations.top()=='-' && ins.peek() == '*' || ins.peek() == '/')); } void Warning(int left_par, int right_par) { if( left_par > right_par) cout<<"Missing right parentese"<