Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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 postfixQueue;
for( auto item:postfix )
postfixQueue.push_back(item);
stack stk;
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)

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions