Answers for "convert infix to postfix c++"

C++
2

infix to postfix program in c++

/*https://github.com/Sudhanshu1304/Stack-Application*/

#include <iostream>
#include<string>
using namespace std;



class Stack{


private:


    char A[5];
    int Size;

public:
    int top;
    Stack(){

        top=-1;
        Size=sizeof(A)/sizeof(char);

    }



    bool IsFull(){

        if(top==Size-1){
            return true;
        }
        else{
            return false;
        }
    }

    bool IsEmpty(){

        if(top==-1){
            return true;
        }
        else{
            return false;
        }
    }

    char peek(){

        return A[top];
    }

    void Push(char val){

        if (IsFull()==false){
            top++;
            A[top]=val;
        }
        else{
            cout<<"nThe Stack is Full"<<endl;
        }
    }

    char Pop(){

        if(IsEmpty()==false){
            char temp=A[top];
            A[top]='0';
            top--;
            return temp;
        }
        else{
            return '-1';
        }

    }

    void Show_Stack(){


        for(int i=0;i<top+1;i++){
            cout<<A[i];
        }


    }

};


int Search(char A){


    string CHAR[]={"([","{)","]}","+-","*/","^$"};
    int Size=(sizeof(CHAR)/sizeof(string));

    for(int i=0;i<Size;i++){

        if(A==CHAR[i][0]){
                if(i+i>=6){
                    return i+i;
                }
                else{
                    return i+i+0;
                }

        }
        else if(CHAR[i][1]==A){
             if(i+i>=6){
                    return i+i;
                }
                else{
                    return i+i+1;
                }
        }
    }
    return -1;

}




void Display(char ch,string vari, Stack &s){

    int Size=s.top+1;


    cout<<"n   "<<ch<<"           ";
    s.Show_Stack();
    for(int i=0;i<10-Size;i++){
        cout<<" ";
    }
    cout<<vari<<endl;

}


int main(){

    Stack STACK;
    char temp;
    string exp;//"A+B*C";
    cout<<"Enter Your Expression :";
    cin>>exp;

    string out="";
    cout<<"nnExpression   Stack   Postfixn"<<endl;
    for(int i=0;i<exp.size();i++){

        temp=exp[i];

        int ab=Search(temp);

        if (ab!=-1){

            /* If We ENCOUNTER CLOSING BRACKETS*/
            if(ab<=5 && ab>=3){



                while(Search(STACK.peek())>2){
                    char val=STACK.Pop();
                        out=out+val;

                    Display(temp,out,STACK);
                }
                STACK.Pop();
                Display(temp,out,STACK);
                }
            /* Search Precedence*/
            else{
                if (Search(temp)>=0 && Search(temp)<=2){
                    STACK.Push(temp);
                    Display(temp,out,STACK);
                }

                /* If TOP < Temp */

                else if(Search(STACK.peek())<ab){

                        STACK.Push(temp);
                        Display(temp,out,STACK);
                }
                else{
                    /* if STACK= +,* and temp= + then we have to remove two times */


                    while(Search(STACK.peek())>=ab){

                        char val=STACK.Pop();
                            out=out+val;
                        Display(temp,out,STACK);
                    }
                    STACK.Push(temp);
                    Display(temp,out,STACK);
                }
            }
        }
        /* If an Alphabet */
        else{

            out=out+temp;
            Display(temp,out,STACK);

            }


    }
    while(STACK.IsEmpty()==false){

        char val=STACK.Pop();

            out=out+val;
        Display(temp,out,STACK);

    }
    cout<<"nnFINAL STRING : "<<out<<endl;


}
Posted by: Guest on December-01-2020
1

infix to postfix conversion

Best Solution
-------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;

int prec(char c) {
    if(c == '^') {
        return 3;
    }
    else if(c == '*' || c == '/') {
        return 2;
    }
    else if(c == '+' || c =='-') {
        return 1;
    }
    else {
        return -1;
    }
}

string infixToPostfix(string s) {
    stack<char> st;
    string res;

    for (int i = 0; i < s.length(); i++)
    {
        if((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')) {
            res += s[i];
        }
        else if(s[i] == '(') {
            st.push(s[i]);
        }
        else if(s[i] == ')') {
            while (!st.empty() && st.top() != '(')
            {
                res += st.top();
                st.pop();
            }
            if(!st.empty()) {
                st.pop(); // Popping '(' here
            }
        }
        else {
            while (!st.empty() && prec(st.top()) >= prec(s[i]))
            {
                res += st.top();
                st.pop();
            }
            st.push(s[i]);
        }
    }
    
    while (!st.empty())
    {
       res += st.top();
       st.pop();
    }
    
    return res;
}

int main() {
    string exp = "a+b*(c^d-e)^(f+g*h)-i";
    cout<<infixToPostfix(exp);
    return 0;
}
Posted by: Guest on July-02-2021

Browse Popular Code Answers by Language