Eternoseeker

Expression

Nov 23rd, 2022 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | Source Code | 0 0
  1. #include<iostream>
  2. #include"stack.h"
  3. using namespace std;
  4.  
  5. class Expression{
  6.     Stack s;
  7. public:
  8.     int evaluate(char* str){
  9.         char* temp = str;
  10.         Stack s;
  11.         while(*temp !='\0'){
  12.             if(*temp>='0' && *temp<='9'){
  13.                 s.push(*temp-48);
  14.             }
  15.             else{
  16.                 int val1 = s.pop();
  17.                 int val2 = s.pop();
  18.                 switch (*temp)
  19.                 {
  20.                     case '+': s.push( val2 + val1); break;
  21.                     case '-': s.push(val2 - val1); break;
  22.                     case '*': s.push(val2 * val1); break;
  23.                     case '/': s.push(val2/val1); break;
  24.                 }
  25.  
  26.             }
  27.             temp++;
  28.         }
  29.         return s.pop();
  30.     }
  31.  
  32.     int precedence(char symbol){
  33.         if(symbol=='+' || symbol == '-')
  34.             return 1;
  35.         else if(symbol=='*' || symbol == '/')
  36.             return 2;
  37.         else if(symbol=='^')
  38.             return 3;
  39.     }
  40.     int isOperator(char ch){
  41.         if(ch=='+' || ch=='-' || ch=='*' ||ch=='/'||ch=='^')
  42.             return true;
  43.         return false;
  44.     }
  45.     void conversion(char* str){
  46.         char *temp = str;
  47.         char ch;
  48.         while(*temp!='\0'){
  49.             if(*temp == '('){
  50.                 s.push(*temp);
  51.             }
  52.             else if(*temp>='0' && *temp<='9')
  53.                 cout<<*temp;
  54.             else if(isOperator(*temp)){
  55.                 //cout<<"*temp: and "<<*temp<<" "<<s.counter()<<endl;
  56.                 if(s.isEmpty())
  57.                     s.push(*temp);
  58.                 else if(s.StackTop()=='('){ //cout<<*temp<< "Pushed";
  59.                     s.push(*temp);
  60.                 }
  61.                 else if(precedence(*temp) > precedence(s.StackTop())){
  62.                     s.push(*temp);
  63.                 }
  64.                 else{
  65.                     char ch = s.pop();
  66.                     cout<<ch;
  67.                     do{
  68.  
  69.                         ch = s.StackTop();
  70.                         if(isOperator(ch) && (precedence(*temp)> precedence(ch)))
  71.                             break;
  72.                         ch=s.pop();
  73.                         if(isOperator(ch))
  74.                             cout<<ch;
  75.                     }while((ch != '(') && !s.isEmpty());
  76.                     s.push(*temp);
  77.  
  78.                 }
  79.             }
  80.             else if(*temp ==')'){
  81.                 do{
  82.                         ch = s.pop();//cout<<ch<<" EElements "<<s.counter()<<endl;
  83.                         if(ch!='(')
  84.                             cout<<ch;
  85.                 }while(!s.isEmpty());
  86.             }
  87.             temp++;
  88.         }
  89.         if(!s.isEmpty()){
  90.            do{
  91.                 //cout<<"While "<<s.counter()<<endl;
  92.             ch=s.pop();
  93.             if(ch!='(')
  94.                 cout<<ch;//" elements "<<s.counter()<<endl;
  95.         }while(!s.isEmpty());
  96.     }
  97.     }
  98.  
  99. };
  100.  
  101. int main(){
  102.     cout<<"Welcome to linked implementation of Stack";
  103.     Expression pe;
  104.     char str[100];
  105.     int choice=1;
  106.     do{
  107.         cout<<"Enter the Expression: "; //1+2*3
  108.         cin>>str;                       //123*+
  109.         pe.conversion(str);
  110.         cout<<"Choice "<<endl;
  111.         cin>>choice;
  112.     }while(choice==1);
  113.     return 0;
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment