Web Site under testing.....

C++ code for Infix to Postfix conversion


#include 
#include 
using namespace std;

int top=-1;
char stack[20];
void push(char a );
char pop();
int is_operator(char symbol);
int check_precedence(char symbol);
int empty();
int main(int argc, const char * argv[]) {
    string infix;
    string postfix;
    char element;
    cout<<"Please enter an infix equation\n";
    cin>>infix;
    
    for (int a=0; a=check_precedence(stack[top]))
                push(infix[a]);
            else if (check_precedence(infix[a])
        
        else if ((infix[a])==')'){
            int b=top;
            
            while (((stack[b--])!='(')){
                element=pop();
                if ((element!=')')&&(element!='(')) {
                    postfix+=element;
                }
                //   a-(b+c*d)/e   (a/(b-c+d))*(e-a)*c
            }
        }
        else
        {
        }
    }
    while (!empty()) {
        element=pop();
        if ((element!=')')&&(element!='('))
            postfix+=element;
    }
cout << postfix"\n";
    return 0;
}
    
    
void push(char item){
    if (top==10) {
        cout<<"Stack Overflow \n";
    }
    else
        stack[++top]=item;
}

char pop(){
    return stack[top--];
}

int is_operator(char symbol){
    if (symbol=='+'|| symbol=='-'|| symbol=='*'||
         symbol=='/'|| symbol=='^') {
        return 1;
    }
    else
        return 0;
}
int check_precedence(char symbol){
    if (symbol=='^')
        return 3;
    else if (symbol=='*'||symbol=='/')
        return 2;
    else if (symbol=='+'||symbol=='-')
        return 1;
    else
        return 0;
}

int empty(){
    if (top==-1)
        return 1;
    else
        return 0;
}

Free Web Hosting