Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please use the code in the description and follow my directions exactly do this in C++. Write a function postFixEval for stack-based evaluation of postfix
please use the code in the description and follow my directions exactly do this in C++.
Write a function postFixEval for stack-based evaluation of postfix expressions. The program reads postfix expressions and prints their values. Each input expression should be entered on its own line, and the program should terminate when the user enters a blank line. Assume that there are only binary operators and that the expressions contain no variables. Your program should use a tack. Here are sample input-output pairs.
78 78
78 6 + 84
78 6 + 9 2 - / 12
#include#include #include #include using namespace std; // skipWhiteSpace for Skiping whitespace in an input stream. * void skipWhiteSpace(istream& in) { while (in.good() && isspace(in.peek()) ) { // Read and discard the space character in.get(); } } // // postFixEval // If the next token in the input is an integer, read // the integer and push it on the stack; but if it is // an operator, pop the last two values from the stack // and apply the operator, and push the result onto // the stack. At the end of the string, the lone // on the stack is the result. // int postFixEval(string str) { istringstream in = istringstream(str); stack postFixStack; skipWhiteSpace(in); while (in) {
} return postFixStack.top(); } int main() { string input; while (true) { cout << "Enter a postfix expression, or press ENTER to quit: "; getline(cin, input); if (input.length() == 0) { break; } int number = postFixEval(input); cout << "The value of " << input << " is " << number << endl; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started