Advertisement
UMIT_GATH

DS - HW#3

Dec 25th, 2023
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | Source Code | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. class myStack
  9. {
  10. public:
  11.     int top;
  12.     char *stack_array;
  13.     int stack_size;
  14.  
  15.     myStack(int stack_size)
  16.     {
  17.        this->top = -1;
  18.        this->stack_array = new char[stack_size];
  19.        this->stack_size = stack_size;
  20.  
  21.     }
  22.  
  23.     ~myStack()
  24.     {
  25.         delete [] this->stack_array;
  26.     }
  27.  
  28.  
  29.     void push(char item)
  30.     {
  31.         if((top+1) >= this->stack_size)
  32.         { cout<<"Stack Overflow\n";
  33.             return;}
  34.  
  35.         stack_array[++this->top] = item;
  36.  
  37.     }
  38.  
  39.     char pop()
  40.     {
  41.         return (stack_array[this->top--]);
  42.  
  43.     }
  44.  
  45.     char peek()
  46.     {
  47.         return (stack_array[this->top]);
  48.     }
  49.  
  50.     bool isEmpty()
  51.     {
  52.         return (this->top < 0);
  53.     }
  54.  
  55.  
  56. };
  57.  
  58.  
  59.  
  60. class myStack_float
  61. {
  62. public:
  63.     int top;
  64.     float *stack_array;
  65.     int stack_size;
  66.  
  67.     myStack_float(int stack_size)
  68.     {
  69.        this->top = -1;
  70.        this->stack_array = new float[stack_size];
  71.        this->stack_size = stack_size;
  72.  
  73.     }
  74.  
  75.     ~myStack_float()
  76.     {
  77.         delete [] this->stack_array;
  78.     }
  79.  
  80.  
  81.     void push(float item)
  82.     {
  83.         if((top+1) >= this->stack_size)
  84.         { cout<<"Stack Overflow\n";
  85.             return;}
  86.  
  87.         stack_array[++this->top] = item;
  88.  
  89.     }
  90.  
  91.     float pop()
  92.     {
  93.         return (stack_array[this->top--]);
  94.  
  95.     }
  96.  
  97.     float peek()
  98.     {
  99.         return (stack_array[this->top]);
  100.     }
  101.  
  102.     bool isEmpty()
  103.     {
  104.         return (this->top < 0);
  105.     }
  106.  
  107.  
  108. };
  109.  
  110.  
  111.  
  112. bool isNumber(char value)
  113. {
  114.     switch(value)
  115.     {
  116.         case '0': case '1': case '2': case '3': case '4':
  117.         case '5': case '6': case '7': case '8': case '9':
  118.         return true;
  119.         break;
  120.         default :
  121.             return false;
  122.     }
  123. }
  124.  
  125. bool isOperator(char value)
  126. {
  127.     switch(value)
  128.     {
  129.         case '+': case '-': case '*': case '/':
  130.         return true;
  131.         break;
  132.         default :
  133.             return false;
  134.     }
  135. }
  136.  
  137. int OperatorsWeight(char value)
  138. {
  139.     switch(value)
  140.     {
  141.         case '+': case '-':
  142.         return 1;
  143.         case '*': case '/':
  144.         return 2;
  145.         default:
  146.         return 0;
  147.     }
  148. }
  149.  
  150.  
  151.  
  152. string reverse_exp(string exp)
  153. {
  154.     string temp = exp;
  155.  
  156.  
  157.     for(int i=exp.size()-1 , j=0 ; i>=0 ; i-- , j++)
  158.         temp[j] = exp[i];
  159.  
  160.  
  161.     return temp;
  162.  
  163. }
  164.  
  165.  
  166. string infix_to_prefix(string exp)
  167. {
  168.     exp = reverse_exp(exp); // inverse of experssion
  169.  
  170.     myStack postfix(50);
  171.     string temp = "";
  172.  
  173.     for(int i=0 ; i<exp.size(); i++)
  174.     {
  175.         if(isNumber(exp[i])) // Number
  176.         {
  177.             temp +=exp[i];
  178.  
  179.         }else if(isOperator(exp[i])) // + - * /
  180.         {
  181.             if(postfix.isEmpty())
  182.             {
  183.                 postfix.push(exp[i]);
  184.             }
  185.             else if(OperatorsWeight(exp[i]) >= OperatorsWeight(postfix.peek())) // prefix condition
  186.             {
  187.                 postfix.push(exp[i]);
  188.             }
  189.             else
  190.             {
  191.                 while(OperatorsWeight(exp[i]) < OperatorsWeight(postfix.peek())) // prefix condition
  192.                     {temp +=postfix.pop();}
  193.  
  194.                 postfix.push(exp[i]);
  195.             }
  196.  
  197.         }else if(exp[i] == ')') /// )   inverse ( to )
  198.                  {
  199.                      postfix.push(exp[i]);
  200.                  }
  201.  
  202.          else if(exp[i] == '(') /// ( inverse ) to (
  203.          {
  204.              while(postfix.peek() != ')')
  205.                {temp += postfix.pop();}
  206.  
  207.                postfix.pop();
  208.  
  209.          }
  210.  
  211.  
  212.     }
  213.  
  214.  
  215.     while(!postfix.isEmpty()) /// Pop the rest of stack
  216.         {temp +=postfix.pop();}
  217.  
  218.         return reverse_exp(temp); /// revere the result
  219. }
  220.  
  221.  
  222. int char_to_int(char c)
  223. {
  224.     return int(c) - 48; /// 48 is ASCII Code of 0
  225. }
  226.  
  227. float prefix_evaluation(string postfix)
  228. {
  229.  
  230.     myStack_float values(30);
  231.  
  232.     for(int i=postfix.size()-1 ; i>=0; i--) // reverse the traversing
  233.     {
  234.  
  235.         if(isNumber(postfix[i]))
  236.         {
  237.             values.push(char_to_int(postfix[i]));
  238.         }
  239.         else if(isOperator(postfix[i]))
  240.         {
  241.             float value1 = values.pop();
  242.             float value2 = values.pop();
  243.            /// here change order ==> value1 (op) value2 instead of value2 (op) value1
  244.             if(postfix[i] == '+') values.push(value1 + value2) ;
  245.             if(postfix[i] == '-') values.push(value1 - value2) ;
  246.             if(postfix[i] == '*') values.push(value1 * value2) ;
  247.             if(postfix[i] == '/') values.push(value1 / value2) ;
  248.  
  249.  
  250.         }
  251.     }
  252.  
  253.  
  254.  
  255.     return values.pop();
  256. }
  257.  
  258.  
  259.  
  260. int main()
  261. {
  262.  
  263.  
  264.     string exp;
  265.  
  266.  
  267.     cout<<"Please Enter Expression ==> ";
  268.     cin>>exp;
  269.     cout<<infix_to_prefix(exp)<<endl;
  270.     cout<<prefix_evaluation(infix_to_prefix(exp));
  271.  
  272.  
  273.  
  274.  
  275.     return 0;
  276. }
  277.  
  278.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement