Question
Can somebody solve the following problem in c++. //TODO //Requires the accurately convered expression as a post fix string //Effects: computes and evaluates the postfix
Can somebody solve the following problem in c++.
//TODO //Requires the accurately convered expression as a post fix string //Effects: computes and evaluates the postfix expression by performing Stack //operations, Uses multiple stacks //Modifies nothing
int evaluate(std::string postfix){ std::istringstream input(postfix); char ch; int data=0; Stack
//store the expression on the stack while (input >> ch) { if (isOperand(ch)) { data = 0; while (isOperand(ch)) { data = data * 10 + (int)(ch - 48); input >> ch; } result.push(data); continue; } } //since this reverses the postfi expression, store in another stack to //reverse the stack //if operand, store numeric value on result stack // use int(ch)-48 to convert char to integer value // if its an operator, pop two operands from result stack // apply the operator on them and store the resulting value in result stack // GIVEN //switch (ch){ // case '*': result.push(one*two); break; // case '/':result.push(one/two);break; // case '+': result.push(one+two); break; //case '-': result.push(one-two); break; // default: break;
// the final value is on the top of the stack, return this return result.pop(); }
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