Question: write a program that will allow the user to enter an arithmetic expression. As an example, 3+2/7*6-(4+1). Design a program to Evaluate the expression and

write a program that will allow the user to enter an arithmetic expression. As an example, 3+2/7*6-(4+1). Design a program to Evaluate the expression and show the result following PEMDAS. Write the program in c++. here is the given code but it is not a complete code

const char * expressionToParse = "3*2+4*1+(4+9)*6"; char peek() { return *expressionToParse; } char get() { return *expressionToParse++; } int expression(); int number() { int result = get() - '0'; while (peek() >= '0' && peek() <= '9') { result = 10*result + get() - '0'; } return result; } int factor() { if (peek() >= '0' && peek() <= '9') return number(); else if (peek() == '(') { get(); // '(' int result = expression(); get(); // ')' return result; } else if (peek() == '-') { get(); return -factor(); } return 0; // error } int term() { int result = factor(); while (peek() == '*' || peek() == '/') if (get() == '*') result *= factor(); else result /= factor(); return result; } int expression() { int result = term(); while (peek() == '+' || peek() == '-') if (get() == '+') result += term(); else result -= term(); return result; } int _tmain(int argc, _TCHAR* argv[]) { int result = expression(); 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!