The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Infixx
By Guest on 4th December 2023 04:45:58 AM | Syntax: PYTHON | Views: 90



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. #include<iostream>
  2. #include<stack>
  3. using namespace std;
  4. bool isOperator(chae c)
  5. {
  6.         if(c=='+'||c=='/'||c=='^')
  7.         {
  8.                 return true;
  9.         }
  10.         else
  11.         {
  12.                 return false;
  13.         }
  14. }
  15. int precedence(char c)
  16. {
  17.         if(c=='^')
  18.         return 3;
  19.         else if(c=='*'||c=='/')
  20.         return 2;
  21.         else if(c=='+'||c=='-')
  22.         return 1;
  23.         else
  24.         return -1;
  25. }
  26. string infixtopostfix(stack<char<s,string infix)
  27. {
  28.         string postfix;
  29.         for(int i=0;i<infix.length();i++)
  30.         {
  31.                 if((infix[i]>='a' && infix[i]<='z') || (infix[i]>='A' && infix[i]<='Z'))
  32.                 {
  33.                         postfix+=infix[i];
  34.                 }
  35.                 else if(infix[i] == '(')
  36.                 {
  37.                          s.push(infix[i]);
  38.                        
  39.                 }
  40.                 else if(infix[i] == ')')
  41.                 {
  42.                         while((s.top()!='(') && (!s.empty()))
  43.                         {
  44.                                  char temp= s.top();
  45.                                  postfix+=temp;
  46.                                  s.pop();
  47.                         }
  48.                 }
  49.                 else if (isOperator(infix[i]))
  50.                 {
  51.                         if(s.empty())
  52.                         {
  53.                                 s.push(infix[i]);
  54.                         }
  55.                         else
  56.                         {
  57.                                 if(precedence(infix[i])>precedence(s.top()))
  58.                                 {
  59.                                         s.push(infix[i]);
  60.                                 }
  61.                                 else if((precedence(infix[i])==precedence(s.top()))&& (infix[i]=='^'))
  62.                                 {
  63.                                         s.push(infix[i]);
  64.                                 }
  65.                                 else
  66.                                 {
  67.                                         while((!s.empty())&&(precedence(infix[i]<=precedence(s.top()))))
  68.                                         {
  69.                                                 char temp=s.top();
  70.                                                 postfix+=temp;
  71.                                                 s.pop();
  72.                                         }
  73.                                         s.push(infix[i]);
  74.                                 }
  75.                         }
  76.                 }
  77.                
  78.         }
  79.         while(!s.empty())
  80.         {
  81.                 postfix+=s.top();
  82.                 s.pop();
  83.         }
  84.        
  85.         return postfix;
  86.        
  87. }
  88.  
  89.  
  90.  
  91.  
  92. int main()
  93. {
  94.         string infix_exp,postfix_exp;
  95.         cout<<"Enter infix exp:"<<endl;
  96.         cin>>infix_exp;
  97.         stack <char> stack;
  98.         cout<<"infix exp:"<<infix_exp;
  99.         postfix_exp=infixtopostfix(stack,infix_exp);
  100.         cout<<"postfix:"<<postfix_exp<<endl;
  101.        
  102.         return 0;
  103. }





infixx