Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics

- Write a program to convert an infix expression to a postfix expression?

/**************************/


/**********cReDo**********/

 

/*****mchinmay@live.com***/

 

//C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.

 

#include<stdio.h>

 

#include<conio.h>

 

#include<string.h>

 

#define MAX 20

 

char stack[MAX];

 

int top=-1;

 

char pop();

 

void push(char item);

 

int prcd(char symbol)

 

{

 

switch(symbol)

 

{

 

case '+':

 

case '-':return 2;

 

break;

 

case '*':

 

case '/':return 4;

 

break;

 

case '^':

 

case '$':return 6;

 

break;

 

case '(':

 

case ')':

 

case '#':return 1;

 

break;

 

}

 

}

 

int isoperator(char symbol)

 

{

 

switch(symbol)

 

{

 

case '+':

 

case '-':

 

case '*':

 

case '/':

 

case '^':

 

case '$':

 

case '(':

 

case ')':return 1;

 

break;

 

default:return 0;

 

}

 

}

 

void convertip(char infix[],char postfix[])

 

{

 

int i,symbol,j=0;

 

stack[++top]='#';

 

for(i=0;i<strlen(infix);i++)

 

{

 

symbol=infix[i];

 

if(isoperator(symbol)==0)

 

{

 

postfix[j]=symbol;

 

j++;

 

}

 

else{

 

if(symbol=='(')push(symbol);

 

else if(symbol==')')

 

{

 

while(stack[top]!='(')

 

{

 

postfix[j]=pop();

 

j++;

 

}

 

pop();//pop out (.

 

}

 

else{

 

if(prcd(symbol)>prcd(stack[top]))

 

push(symbol);

 

else{

 

while(prcd(symbol)<=prcd(stack[top]))

 

{

 

postfix[j]=pop();

 

j++;

 

}

 

push(symbol);

 

}//end of else.

 

}//end of else.

 

}//end of else.

 

}//end of for.

 

while(stack[top]!='#')

 

{

 

postfix[j]=pop();

 

j++;

 

}

 

postfix[j]='\0';//null terminate string.

 

}

 

void main()

 

{

 

char infix[20],postfix[20];

 

clrscr();

 

printf("Enter the valid infix string:\n");

 

gets(infix);

 

convertip(infix,postfix);

 

printf("The corresponding postfix string is:\n");

 

puts(postfix);

 

getch();

 

}

 

void push(char item)

 

{

 

top++;

 

stack[top]=item;

 

}

 

char pop()

 

{

 

char a;

 

a=stack[top];

 

top--;

 

return a;

 

}


Improve Answer Discuss the question "- Write a program to convert an infix expression to a postfix expression?" Click here to register and get updates when this answer is edited. Share on Facebook Share on Twitter

First answer by ID1010119635. Last edit by Mchinmay. Contributor trust: 0 [recommend contributor]. Question popularity: 3 [recommend question].

Answers.com > Wiki Answers > Categories > Technology > Computers > Computer Programming > - Write a program to convert an infix expression to a postfix expression?

Our contributors said this page should be displayed for the questions below. (Where do these come from)
If any of these are not a genuine rephrasing of the question, please help out and edit these alternates.
Infix to postfix cpp?  What is a K-expression?  Acliche is a expression?  A clichc is a expression?  A cliche'is a expression?  Convert postfix to infix?  Whatis vriable expression?  10 expression of idiamatic?  Infix convert into postfix?  Postfix convert into infix?