Question: #include #include #include #include #include #include using namespace std; // test pattern: 1+2-3*4/5+6-7 => 12+34*5/-6+7- (postfix) int calc(char op, int num1, int num2) { int
| #include | |
| #include | |
| #include | |
| #include | |
| #include | |
| #include | |
| using namespace std; | |
| // test pattern: 1+2-3*4/5+6-7 => 12+34*5/-6+7- (postfix) | |
| int calc(char op, int num1, int num2) { | |
| int result; | |
| switch(op) { | |
| case '*': result = num1 * num2; break; | |
| case '/': result = num1 / num2; break; | |
| case '+': result = num1 + num2; break; | |
| case '-': result = num1 - num2; break; | |
| case '^': result = pow(num1, num2); break; | |
| } | |
| return result; | |
| } | |
| int main() { | |
| string postfix; | |
| cout | |
| getline( cin, postfix ); | |
| deque | |
| for( auto item:postfix ) | |
| postfixQueue.push_back(item); | |
| stack | |
| int num1, num2, result; | |
| while( !postfixQueue.empty() ) { | |
| char token = postfixQueue.front(); | |
| if( isalnum(token) ) { | |
| stk.push( token - '0' ); | |
| postfixQueue.pop_front(); | |
| } | |
| else if( token == '+' || token == '-' || | |
| token == '*' || token == '/' ) { | |
| postfixQueue.pop_front(); | |
| num2 = stk.top(); stk.pop(); | |
| num1 = stk.top(); stk.pop(); | |
| result = calc( token, num1, num2 ); | |
| stk.push(result); | |
| cout | |
| } | |
| } | |
| } I need help to 1. the user input 2. the calculation of following 4 operators: + - * / 3. tracing of all the content of the Operand Stack and Postfix Queue between changes.(I am missing this part)
|
Enter a postfix expression, q to quit: 12345+-+* Operand Stack Postfix Queue 2, 1 3, 2, 1 4, 3, 2, 1 5, 4, 3, 2, 1 3, 2, 1 9, 3, 2, 1 4+5 = 2, 1 -6, 2, 1 = 3-9 -4 1*-4 = Enter a postfix expression, q to quit: Enter a postfix expression, q to quit: 12345+-+* Operand Stack Postfix Queue 2, 1 3, 2, 1 4, 3, 2, 1 5, 4, 3, 2, 1 3, 2, 1 9, 3, 2, 1 4+5 = 2, 1 -6, 2, 1 = 3-9 -4 1*-4 = Enter a postfix expression, q to quit
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts

