Answered step by step
Verified Expert Solution
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 | |
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) |
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