Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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