Advertisement
PROFESSOR_AIH

Infix To Postfix

Jan 31st, 2023 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 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. int precedence(char ch)
  42. {
  43.     if(ch=='+'||ch=='-')
  44.         return 1;
  45.     if(ch=='*'||ch=='/')
  46.         return 2;
  47.     if(ch=='^')
  48.         return 3;
  49.  
  50.     return 0;
  51. }
  52. int main()
  53. {
  54.     ios_base::sync_with_stdio(false);
  55.     string Q="A+(B*C-(D/E^F)*G)*H";
  56.     string p="";
  57.     Q+=')';
  58.     stack<char>st;
  59.     st.push('(');
  60.     for(int i=0;Q[i]!='\0';i++)
  61.     {
  62.         if(Q[i]>='A' && Q[i]<='Z')
  63.         {
  64.             p+=Q[i];
  65.         }
  66.         else if(Q[i]=='(')
  67.         {
  68.             st.push(Q[i]);
  69.         }
  70.         else if(Q[i]==')')
  71.         {
  72.             while(st.top()!='(')
  73.             {
  74.                 p+=st.top();
  75.                 st.pop();
  76.             }
  77.             st.pop();
  78.         }
  79.         else
  80.         {
  81.             while(!st.empty() && precedence(Q[i])<=precedence(st.top()))
  82.             {
  83.                 p+=st.top();
  84.                 st.pop();
  85.             }
  86.             st.push(Q[i]);
  87.         }
  88.  
  89.     }
  90.     while(!st.empty())
  91.     {
  92.         p+=st.top();
  93.         st.pop();
  94.     }
  95.     cout<<p<<endl;
  96.  
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement