Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  v
Language: C/C++
Code:
#include<stdio.h>
int f();
int g();
void push(char)
char pop();
char stack[30],infix[30],postfix[30];
int top=-1,i,j=0;
int main()
{
    printf("Enter the expression");
    scanf("%s",infix);
    push('#');
    for(i=0;infix[i];i++)
    while(f()>g())
    postfix[j++]=pop();
    if (f()!=g())
    push(infix[i]);
}
while(stack[top]!='#')
postfix[j++]=pop();
postfix[j]='/0';
printf("The postfix expression is %s",postfix);
return 0;
int g()
{
    switch(infix[i])
    {
        case'+':return1;
        case'-':return1;
        case'*':return3;
        case'/':return3;
        case'$':return6;
        case'^':return6;
        case'(':return9;
        case')':return0;
        default:return7;
        
    }
}
int f()
{
    switch(stack[top])
    {
        case'+':return2;
        case'-':return2;
        case'*':return4;
        case'/':return4;
        case'$':return5;
        case'^':return5;
        case'(':return0;
        case')':return-1;
        default:return8;
        
    }
}
void push(char ele)
{
    stack[++top]=ele;
    
}
char pop()
{
    return(stack[top--]);
}
          
Comments: