Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that uses stacks to evaluate an arithmetic expression. The progr

ID: 3557316 • Letter: W

Question

Write a program that uses stacks to evaluate an arithmetic expression.

The program takes as input a numeric expression, such as 3+4*2, and outputs the result.

1)   Operators are +, -, *, /

2) Assume that the expression is formed correctly so that each operation has two arguments.

3) The expression can have parenthesis, for example: 3*(4-2)+6.

4) The expression can have negative numbers.

5) The expression can have spaces in it, for example: 3 * (4-2) +6 .

Here are some useful method that you may need:

               char cin.peek(); -- returns the next character of the cin input stream ( without reading it)

               bool isdigit(char c); -- returns true if c is one of the digits

Explanation / Answer

#include #include #include using namespace std; // Computes the precedence of an operator. int prec( char op ); // Applies an operator to numbers. long evaluate( char op, long x, long y ); int main( ) { long nst[41]; // The number stack. long ntop = -1; // The top of the number stack. char ost[41]; // The operator stack. long otop = -1; // The top of the operator stack. // Prompt the user for an expresion and read it. string ebuff; // Buffers the arithmetic expression. int ie = 0; // Indexes the arithmetic expression. cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote