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

C++ programming Use the C++ Standard Template Library’s stack class to write a p

ID: 3780212 • Letter: C

Question

C++ programming

Use the C++ Standard Template Library’s stack class to write a program for processing a file of postfix expressions.

Recall that in a postfix expression, a binary operator comes after its two operands. Thus the infix expression (w + x) * (yz) becomes w x + y z – * in postfix.

A postfix expression can be efficiently evaluated using a stack as follows.

Read the expression from the left to the right.

When an operand is encountered, push it on the stack.

When an operator is encountered, pop the top two elements from the stack, perform the operation and then push the result.

There should be exactly one element on the stack at the end and this element is the value of the expression.

For example, if the expression 20 5 – 3 * is entered, the evaluation should proceed as follows.

Symbol read

20

5

3

*

Actions taken

Push 20

Push 5

Pop 5, Pop 20, Push 20 – 5

Push 3

Pop 3, Pop 15, Push 15 * 3

The result of 45 should then be output.

Your program should ask the user for the name of an input file. The input file will contain postfix expressions one per line. The program should open the file and evaluate the postfix expressions in the file. Your program should give the user the option of writing the results to an output file or to the screen.

Your program should also check the entered postfix expression for validity. A postfix expression is invalid if there is more than one element on the stack at the end of the evaluation or if ever there are not enough operands on the stack when an operation is performed.

E.g. If the input file contained:

20 5 – 3 *

100 50 60 + +

3 4 5 +

3 4 5 +

3 4 + +

3 4 ?

The output would be:

45

210

Too few operators.

Too few operators.

Too many operators.

Illegal operation

Limit your postfix expression to the arithmetic operations of +, –, *, and / on integer values.

Explanation / Answer

#include<iostream>
#include<cctype>
#include<stack>
#include<cstdlib>
using namespace std;

// returns the value when a specific operator
// operates on two operands
int eval(int op1, int op2, char operate) {
   switch (operate) {
      case '*': return op2 * op1;
      case '/': return op2 / op1;
      case '+': return op2 + op1;
      case '-': return op2 - op1;
      default : return 0;
   }
}

// evaluates the postfix operation
// this program supports multiple digit integers
// looks for valid expression
// evaluate postfix operations using stack

int evalPostfix(char postfix[], int size) {
   stack<int> s;
   int i = 0;
   char ch;
   int val,n=0,k=0;
   s.push(-9999999);
   //cout<<"null"<<endl;
   while (i < size) {
      ch = postfix[i];
    
      if(ch==' '||ch==' '){
      if (k!=1) {
         // we saw an operand
         // push the digit onto stack
         cout<<"Push "<<n<<endl;
         s.push(n);
         n=0;
         k=0;
      }
      else {
         // we saw an operator
         // pop off the top two operands from the
         // stack and evalute them using the current
         // operator
      
       
         if(s.top()==-9999999){
               cout<<" Too few operands Illegal operation ";
   exit(0);
         }
         int op1 = s.top();
         cout<<"Pop "<<op1<<",";
         s.pop();
         if(s.top()==-9999999){
               cout<<" Too few operands Illegal operation ";
   exit(0);
         }
         int op2 = s.top();
         cout<<"Pop "<<op2<<",";
         s.pop();
         cout<<"Push "<<op2<<(char)n<<op1<<endl;
         val = eval(op1, op2, n);
         // push the value obtained after evaluating
         // onto the stack
         s.push(val);
         n=0;
         k=0;
      }
       }
      i++;

      //cout<<ch<<endl;
      if(isdigit(ch))
      {
           n=n*10+ch-'0';
      }
      else {
       if(ch!=' ')      
       {n=ch;k=1;}
       }
    
   }
   val =s.top();
   s.pop();
   if(s.top()==-9999999)
   return val;
   else
{cout<<" Too few operators Illegal operation ";
   exit(0);}
}

// main
int main() {
   char postfix[10000];
   string s;
   cout<<"Enter expression:";
   getline(cin,s);
   int i;
   //converting string to char array
   for(i=0;s[i]!='';i++)
   {
           postfix[i]=s[i];
   }
   postfix[i++]=' ';

   int size = i;

   cout<<"Symbols used: ";
   for(i=0;i<size;i++)
{
   cout<<postfix[i];
   if(postfix[i]==' ')cout<<' ';
}

   cout<<"Actions taken:"<<endl;
   int val = evalPostfix(postfix, size);
   cout<<" Expression evaluates to "<<val;
   cout<<endl;
   return 0;
}

output:-

Enter expression:20 5 - 3 *
Symbols used:
20
5
-
3
*
Actions taken:
Push 20
Push 5
Pop 5,Pop 20,Push 20-5
Push 3
Pop 3,Pop 15,Push 15*3

Expression evaluates to 45


Process exited normally.
Press any key to continue . . .

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