Advertisement
fahad005

Untitled

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