postfix

 avatar
user_7025302
plain_text
2 years ago
3.7 kB
8
Indexable
#include <iostream>
#include <bits/stdc++.h>
#include <stack>
#include <string.h>
#include <iomanip>

using namespace std;
bool isOperator(char c)
{
    return (!isalpha(c) && !isdigit(c));
}
 
int getPriority(char C)
{
    if (C == '-' || C == '+')
        return 1;
    else if (C == '*' || C == '/')
        return 2;
    else if (C == '^')
        return 3;
    return 0;
}


// Function to convert infix expression to postfix
string infixToPostfix(string infix)
{
    stack<char> s;
    string output = "";
for (int i = 0; i < infix.length(); i++) {
        
         if (isalpha(infix[i]) || isdigit(infix[i]))
            {
            output += infix[i];
           }
    
    else  if (infix[i] == '(' )
     {
         s.push(infix[i]);
     }
       else if (infix[i] == ')')
        {
            while (!s.empty() && s.top() != '(')
            {
                output += s.top();
                s.pop();
            }

            // Pop the '(' from the stack, but do not add it to the output
            if (!s.empty())
                s.pop();
        }
         else 
        {
            
            
                {
                      while(!s.empty() && getPriority(s.top()) >=getPriority(infix[i])){
				output += s.top();
				s.pop();
			}

			s.push(infix[i]);
                }   

        }
         
    cout<<infix[i]<<"  ";
    cout<<setw(25);
        stack<char> temp = s;
        if(!temp.empty())
        {
      while (!temp.empty())
      {
        cout <<temp.top();
        temp.pop();
      }
    }
    else
    {
        cout<<" ";
    }
      cout<<"\t"<<setw(22);
    cout<<output<<endl;
    }

    // Pop and output from the stack until it is empty
    while (!s.empty())
    {
        output += s.top();
        s.pop();
    }
    cout << setw(47) << "Output: " ;
    return output;
}

string infixToPrefix(string infix)
{
    reverse(infix.begin(), infix.end());
    // stack for operators.
    stack<char> s;
 
    // stack for operands.
    stack<string> operands;
    string output;
 
    for (int i = 0; i < infix.length(); i++) {
        
         if (isalpha(infix[i]) || isdigit(infix[i]))
            {
            output += infix[i];
           }
    
    else  if (infix[i] == ')' )
     {
         s.push(infix[i]);
     }
       else if (infix[i] == '(')
        {
            while (!s.empty() && s.top() != ')')
            {
                output += s.top();
                s.pop();
            }

            // Pop the '(' from the stack, but do not add it to the output
            if (!s.empty())
                s.pop();
        }
         else 
        {
            
            
                {
                      while(!s.empty() && getPriority(s.top()) >getPriority(infix[i])){
				output += s.top();
				s.pop();
			}

			s.push(infix[i]);
                }   

        }
        
        cout<<infix[i]<<"  ";
    cout<<setw(25);
        stack<char> temp = s;
        if(!temp.empty())
        {
      while (!temp.empty())
      {
        cout <<temp.top();
        temp.pop();
      }
    }
    else
    {
        cout<<" ";
    }
      cout<<"\t"<<setw(22);
    cout<<output<<endl;
    }
      while(!s.empty()){
          output += s.top();
        s.pop();
    }
    reverse(output.begin(), output.end());
    return output;
}
int main() {
    // Test the infixToPostfix function
    string s;
    cout<<"Enter expression ";
    cin>>s;
    cout << "Postfix" << endl;
	cout << "Expression \t\t Operator \t\t Operand" << endl;
    cout << infixToPostfix(s) << endl;
    cout << "Prefix" << endl;
	cout << "Expression \t\t Operator \t\t Operand" << endl;
    cout << infixToPrefix(s) << endl;
    return 0;
}
Editor is loading...