Advertisement
fahad005

Untitled

Sep 29th, 2021
1,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int Operator(char ch)
  5. {
  6.     if (ch == '^' || ch == '*' || ch == '/' || ch == '+' || ch == '-')
  7.     {
  8.         return 1;
  9.     }
  10.     return 0;
  11. }
  12. int operator_val(char ch)
  13. {
  14.     if (ch == '^')
  15.         return 3;
  16.     else if (ch == '*' || ch == '/')
  17.         return 2;
  18.     else if (ch == '+' || ch == '-')
  19.         return 1;
  20.  
  21.     return 0;
  22. }
  23.  
  24. int main()
  25. {
  26.     stack<char> st;
  27.     char q[500], p[500];
  28.     int i, j, top = 0, p_idx = 0;
  29.  
  30.     st.push('(');
  31.     printf("Enter the expression (no space): ");
  32.     gets(q);
  33.  
  34.     strcat(q, ")");
  35.  
  36.     for (i = 0; i < strlen(q); i++)
  37.     {
  38.         char ch = q[i];
  39.         if (isdigit(ch))
  40.         {
  41.             p[p_idx++] = ch;
  42.         }
  43.         else if (ch == '(')
  44.         {
  45.             st.push(ch);
  46.         }
  47.         else if (Operator(ch) == 1)
  48.         {
  49.             char temp = st.top();
  50.             st.pop();
  51.  
  52.             while (Operator(temp) == 1 && operator_val(temp) >= operator_val(ch) && top > 0)
  53.             {
  54.                 p[p_idx++] = temp;
  55.                 temp = st.top();
  56.                 st.pop();
  57.             }
  58.             st.push(temp);
  59.             st.push(ch);
  60.         }
  61.         else if (ch == ')')
  62.         {
  63.             char temp;
  64.             temp = st.top();
  65.             st.pop();
  66.             while (temp != '(')
  67.             {
  68.                 p[p_idx++] = temp;
  69.                 temp = st.top();
  70.                 st.pop();
  71.             }
  72.         }
  73.     }
  74.  
  75.  
  76.  
  77.     // evaluating the postfix expression
  78.  
  79.     int a, b, size, item, k = 0, l = 0;
  80.     j = 0, top = 0;
  81.     char ch, temp[100];
  82.  
  83.     stack<int> st1;
  84.  
  85.  
  86.     while (k < p_idx * 2 - 1)
  87.     {
  88.         if (k % 2 == 1) ch = ',';
  89.         if (k % 2 == 0) ch = p[l++]; // inserting the elements from the user
  90.  
  91.         if (ch == ')') // checking the end of the expression.
  92.         {
  93.             break;
  94.         }
  95.         else if (ch == '*' || ch == '/' || ch == '+' || ch == '-') // checking for operator
  96.         {
  97.             a = st1.top(); // getting the last element of the stack
  98.             st1.pop();
  99.  
  100.             b = st1.top(); // getting the second last element of the stack
  101.             st1.pop();
  102.  
  103.             if (ch == '*')  // doing operation according to the operator
  104.             {
  105.                 item = b * a;
  106.             }
  107.             else if (ch == '/')
  108.             {
  109.                 item = b / a;
  110.             }
  111.             else if (ch == '+')
  112.             {
  113.                 item = b + a;
  114.             }
  115.             else
  116.             {
  117.                 item = b - a;
  118.             }
  119.  
  120.             st1.push(item); // pushing the new value after the operation
  121.         }
  122.         else if (ch != ',') // inserting the digits of an operand to a temporary array
  123.         {
  124.             temp[j++] = ch;
  125.         }
  126.         else // after inserting all the digits of an operand , I converted it to an integer
  127.         {
  128.             if (j != 0)
  129.             {
  130.                 char temp1[j];
  131.                 for (int k = 0; k < j; k++)
  132.                 {
  133.                     temp1[k] = temp[k]; // inserting the elements of the temporary array to another array for integer conversion
  134.                 }
  135.                 item = atoi(temp1); // converting the temporary array to integer format
  136.                 st1.push(item);
  137.             }
  138.             j = 0;
  139.         }
  140.  
  141.         k++;
  142.     }
  143.  
  144.     cout << "\nThe result is: " << st1.top() << endl << endl;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement