Question: in c++ help me fix my program so that it does not crash when the expression contains spaces ------------------------------------------------------------------------------------------------------------------------------- #include #include using namespace std; //

in c++

help me fix my program so that it does not crash when the expression contains spaces

-------------------------------------------------------------------------------------------------------------------------------

#include #include

using namespace std;

// Function to find priority of // operators(2>1>0) int priority(char op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') return 2; return 0; }

// Function to perform arithmetic operations. int solveOp(int a, int b, char op) { switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; } } // Function that returns value of // expression after evaluation.

int evaluate(string expression) { int i; //using two-stack algorithm (by E. W. Dijkstra) // stack to store integer values. stack values; // stack to store operators. stack ops; for (i = 0; i < expression.length(); i++) { // Current token is a whitespace, // skip it. if (expression[i] == ' ') continue; // Current token is an opening // brace, push it to 'ops' else if (expression[i] == '(') { ops.push(expression[i]); } // Current token is a number, push // it to stack for numbers. else if (isdigit(expression[i])) { int val = 0;

// There may be more than one // digits in number. while (i < expression.length() && isdigit(expression[i])) { val = (val * 10) + (expression[i] - '0'); i++; }

values.push(val); i--; } // Closing brace encountered, now solve // entire brace. else if (expression[i] == ')') { while (!ops.empty() && ops.top() != '(') { int val2 = values.top(); values.pop();

int val1 = values.top(); values.pop();

char op = ops.top(); ops.pop();

values.push(solveOp(val1, val2, op)); } // pop opening brace. if (!ops.empty()) ops.pop(); } // else if the current token is an operator. else { // While top of 'ops' has same or greater // priority to current token, which // Apply operator on top // of 'ops' to top two elements in values stack. while (!ops.empty() && priority(ops.top()) >= priority(expression[i])) { int val2 = values.top(); values.pop();

int val1 = values.top(); values.pop();

char op = ops.top(); ops.pop();

values.push(solveOp(val1, val2, op)); }

// Push current token to 'ops'. ops.push(expression[i]); } } // Entire expression has been parsed at this // point, apply remaining ops to remaining // values. while (!ops.empty()) { int val2 = values.top(); values.pop();

int val1 = values.top(); values.pop();

char op = ops.top(); ops.pop();

values.push(solveOp(val1, val2, op)); } // Top of 'values' contains result, return it. return values.top(); }

int main() { cout << "Enter an infix expression to evaluate : "; string expression; cin >> expression; //calling the evaluate function cout << evaluate(expression) << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!