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

bbhosted.cuny.edu Assignment # 3 Assignment # 3 is due on Nov. 10, Write a progr

ID: 3599002 • Letter: B

Question

bbhosted.cuny.edu Assignment # 3 Assignment # 3 is due on Nov. 10, Write a program that uses C++ STL stacks to evaluate a postfix expression. The program should evaluate the binary operators ( ', and %). The program should read an expression in postfix notation. The program should contain a while loop to allow the user to evaluate as many postfix expressions as needed. You may Use these expressions to test your program after converting them to postfix notation. assume that the user will enter only single digit numbers. (2+3) -1)-20 2 3 5)-1-16

Explanation / Answer

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

int main(){
   string expression;

   cout<<"Enter expression to evaluate: ";       //output syntax
   cin>>expression;       //input syntax

   int size=expression.length();       //return length of the array
   vector<string> stack1;       //vector of a type string
   vector<string> stack2;

   string str0="";
   for(int i=0; i<size; i++){
       //char to string{
       stringstream ss;
       string s;
       char c=expression.at(i);   //char at ith position of the string
       ss<<c;
       ss>>s;
       //}
       if(s=="+" || s=="*" || s=="-" || s=="/" || s=="(" || s==")"){
           if(str0!=""){
               stack1.push_back(str0);       //push an element to the vector
               stack1.push_back(s);
               str0="";
           }
           else{
               stack1.push_back(s);
           }
       }
       else{
           str0+=s;       //concatenate two strings
       }
   }
   if(str0!=""){
       stack1.push_back(str0);
   }

   while(!stack1.empty()){       //run loop till stack1 is not empty
       string str1=stack1.back();       //back() function return the topest element of the stack
       stack1.pop_back();       //remove the topest element of the list
       if(str1=="*" || str1=="/"){
           string x=stack1.back();
           if(x==")"){
               stack2.push_back(str1);
           }
           else{
               string y=stack2.back();
               stack1.pop_back();
               stack2.pop_back();
               if(str1=="*"){
                   stack2.push_back(to_string(stoi(x)*stoi(y)));
               }
               else{
                   stack2.push_back(to_string(stoi(x)/stoi(y)));
               }
           }
       }
       else if(str1=="("){
           while(true){
               string str2=stack2.back();
               stack2.pop_back();
               if(str2==")"){
                   break;
               }
               else if(str2=="+" || str2=="-"){
                   string x2=stack1.back();
                   string y2=stack2.back();
                   stack1.pop_back();
                   stack2.pop_back();
                   if(str2=="+"){
                       stack2.push_back(to_string(stoi(x2)+stoi(y2)));
                   }
                   else{
                       stack2.push_back(to_string(stoi(x2)-stoi(y2)));  
                   }
               }
               else{
                   stack1.push_back(str2);
               }
           }
           if(!stack2.empty()){
               string str3=stack2.back();
               if(str3=="*" || str3=="/"){
                   string x3=stack1.back();
                   string y3=stack2.back();
                   stack1.pop_back();
                   stack2.pop_back();
                   if(str3=="*"){
                       stack2.push_back(to_string(stoi(x3)*stoi(y3)));
                   }
                   else{
                       stack2.push_back(to_string(stoi(x3)/stoi(y3)));
                   }
               }
               else{
                   stack2.push_back(stack1.back());
                   stack1.pop_back();
               }
           }
       }
       else{
           stack2.push_back(str1 );
       }
   }
   while(!stack2.empty()){
       string str4=stack2.back();
       stack2.pop_back();
       if(str4=="+" || str4=="-"){
           string x4=stack1.back();
           string y4=stack2.back();
           stack1.pop_back();
           stack2.pop_back();
           if(str4=="+"){
               stack1.push_back(to_string(stoi(x4)+stoi(y4)));
           }
           else{
               stack1.push_back(to_string(stoi(x4)-stoi(y4)));
           }
       }
       else{
           stack1.push_back(str4);
       }
   }
   cout<<stoi(stack1.back());
   return stoi(stack1.back());
   // return 0;
}