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: 3839829 • 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 functions 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 ‘0’ through ‘9’, false otherwise

               cin.ignore(); -- reads and discards the next character from the cin input stream

               cin.get(char &c); -- reads a character in c ( could be a space or the new line )

Explanation / Answer

Solution:

#include<iostream>
#include<stack>
#include<sstream>
using namespace std;

bool preced(char oper1, char oper2)
{
if (oper2 == '(' || oper2 == ')')
return false;
if ((oper1 == '*' || oper1 == '/') && (oper2 == '+' || oper2 == '-'))
return false;
else
return true;
}
int main()
{
char ch;
std::stack<int> s;
   std::stack<char> s1;
cout<<"enter the balanced expression ";
int c = std::cin.peek();
   int first;
   int second;
   while(c!=EOF)
   {
       cin.get(ch);
       if(ch==' ')
       {
           cin.ignore();
           //break;
       }
       else if(isdigit(ch))
       {
           int el;
           stringstream ss(ch);
           ss>>el;
           s.push(el);
       }
       else if(ch=='(')
       {
           s1.push(ch);
       }
       else if(ch==')')
       {
           while(s1.top()!=')')
           {
               char op=s1.top();
               s1.pop();
               first=s.top();
               s.pop();
               second=s.top();
               s.pop();
               switch(op)
               {
                   case '+':
                   s.push(first+second);
                   break;
                   case '-':
                   s.push(first-second);
                   break;
                   case '*':
                   s.push(first*second);
                   break;
                   case '/':
                   s.push(first/second);
                   break;
               }
           }
       }
else if (ch== '+' || ch == '-' ||
ch == '*' || ch == '/')
{
while (!s1.empty() && preced(ch, s1.top()))
           {

               char op=s1.top();
               s1.pop();
               first=s.top();
               s.pop();
               second=s.top();
               s.pop();
               switch(op)
               {
                   case '+':
                   s.push(first+second);
                   break;
                   case '-':
                   s.push(first-second);
                   break;
                   case '*':
                   s.push(first*second);
                   break;
                   case '/':
                   s.push(first/second);
                   break;
               }
           }
s1.push(ch);
}
      
   }
   while(!s1.empty())
   {
       char op=s1.top();
           s1.pop();
           first=s.top();
           s.pop();
           second=s.top();
           s.pop();
           switch(op)
           {
               case '+':
               s.push(first+second);
               break;
               case '-':
               s.push(first-second);
               break;
               case '*':
               s.push(first*second);
               break;
               case '/':
               s.push(first/second);
               break;
           }
       }
cout<<"The result is "<<s.top()<<endl;
system("pause");
   return 0;
}

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