Advertisement
PROFESSOR_AIH

Infix to Postfix Line by Line

Feb 4th, 2023
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. //           * * Bismillahir Rahmanir Rahim  * *
  2. // ************************@Author*******************************
  3. /*                    Asik Ifthaker Hamim                       */
  4. #include <bits/stdc++.h>
  5. #include<stdlib.h>
  6. using namespace std;
  7. typedef long long ll;
  8. typedef unsigned long long int ulli;
  9. typedef long long int lli;
  10. typedef unsigned long long ull;
  11. typedef vector<int> vi;
  12. typedef vector<ll> vl;
  13. typedef vector<string> vs;
  14. typedef pair<ll,ll> pr;
  15. #define pb push_back
  16. #define sz(n) n.size()
  17. #define vs(n) (int)n.size()
  18. #define pp pop_back
  19. #define sp(n) setprecision(n)
  20. #define big 1000000007
  21. #define ffor(a, b) for (int i = a; i <b; i++)
  22. #define frev(b, a) for (int i = b; i >= a; i--)
  23. #define PI (acos(-1.0))
  24. #define torad(x) ((x)*((2*acos(0))/180.0))
  25. #define todeg(x) ((x)*(180.0/(2*acos(0))))
  26. #define fixAngle(x) ((x)>1?1:(x)<-1?-1:(x))
  27. #define tan(a) tan(a)/(pi/180)
  28. #define sin(a) sin(a)/(pi/180)
  29. #define cos(a) cos(a)/(pi/180)
  30. #define inverse_sin(a) asin(a)/(pi/180)
  31. #define inverse_cos(a) acos(a)/(pi/180)
  32. #define inverse_tan(a) atan(a)/(pi/180)
  33. #define eps 1e-9
  34. const int dr4[] = {0, 1, 0, -1};
  35. const int dc4[] = {1, 0, -1, 0};
  36. const int dr8[] = {0, 1, 1, 1, 0, -1, -1, -1};
  37. const int dc8[] = {1, 1, 0, -1, -1, -1, 0, 1};
  38. using namespace std;
  39. double neg_infinity(-std::numeric_limits<double>::infinity());
  40. #define ran 1000000
  41. #define MAX_SIZE 100
  42.  
  43. char Stack[MAX_SIZE];
  44. int top = -1;
  45.  
  46. void push(char item)
  47. {
  48.     Stack[++top] = item;
  49. }
  50.  
  51. char pop()
  52. {
  53.     return Stack[top--];
  54. }
  55.  
  56. int priority(char symbol)
  57. {
  58.     switch(symbol)
  59.     {
  60.     case '+':
  61.     case '-':
  62.         return 1;
  63.     case '*':
  64.     case '/':
  65.         return 2;
  66.     case '^':
  67.         return 3;
  68.     default:
  69.         return 0;
  70.     }
  71. }
  72. int main()
  73. {
  74.     ios_base::sync_with_stdio(false);
  75.     char infix[MAX_SIZE], postfix[MAX_SIZE];
  76.     printf("Enter an infix expression: ");
  77.     scanf("%s", infix);
  78.     //infixToPostfix(infix, postfix);
  79.     for(int k=00;k<1;k++)
  80.     {
  81.         int i, j;
  82.         char item;
  83.         push('(');
  84.         strcat(infix, ")");
  85.         i = j = 0;
  86.         item = infix[i];
  87.         while(item != '\0')
  88.         {
  89.             if(item == '(')
  90.             {
  91.                 push(item);
  92.             }
  93.             else if(isalnum(item))
  94.             {
  95.                 postfix[j] = item;
  96.                 j++;
  97.             }
  98.             else if(strchr("+-*/^", item) != NULL)
  99.             {
  100.                 while(priority(Stack[top]) >= priority(item))
  101.                 {
  102.                     postfix[j] = pop();
  103.                     j++;
  104.                 }
  105.                 push(item);
  106.             }
  107.             else if(item == ')')
  108.             {
  109.                 while(Stack[top] != '(')
  110.                 {
  111.                     postfix[j] = pop();
  112.                     j++;
  113.                 }
  114.                 pop();
  115.             }
  116.             else
  117.             {
  118.                 //printf("Invalid expression");
  119.                 break;
  120.             }
  121.             i++;
  122.             item = infix[i];
  123.             postfix[j] = '\0';
  124.             printf("Postfix expression: %s\n", postfix);
  125.  
  126.         }
  127.  
  128.         if(top > 0)
  129.         {
  130.             printf("Invalid expression");
  131.         }
  132.  
  133.     }
  134.  
  135.  
  136.  
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement