0% found this document useful (0 votes)
68 views3 pages

Prog - 4 Infix To Postfix

DSA program 4

Uploaded by

Nitin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views3 pages

Prog - 4 Infix To Postfix

DSA program 4

Uploaded by

Nitin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Develop a Program in C for converting an Infix Expression to Postfix

Expression. Program should support for both parenthesized and free


parenthesized expressions with the operators: +, -, *, /, % (Remainder), ^
(Power) and alphanumeric operands.

#include<stdio.h>

char stack[20];
int top=-1;

void push(char x) {
stack[++top]=x;
}

char pop() {
if(top==-1)
return -1;
else
return stack[top--];
}

int preced(char x) {
if(x=='(')
return 0;
if(x=='+'||x=='-')
return 1;
if(x=='*'||x=='/' ||x=='%')
return 2;
if(x=='$'|| x=='^')
return 3;
return -1;
}

int main() {
char exp[20],x,*e;
printf("Enter the expression: ");
scanf("%s",exp);
e=exp;
printf("Postfix expression: ");
while(*e!='\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e=='(')
push(*e);
else if(*e==')') {
while((x=pop())!='(')
printf("%c",x);
}
else {
while(preced(stack[top])>=preced(*e)) {
printf("%c",pop());
}
push(*e);
}
e++;
}
while(top!=-1)
printf("%c",pop());
}

Output:
Enter the expression: (a+(b*c)/(d-e))
Postfix expression: abc*de-/+

Enter the expression: (3^2*5)/(3*2-3)+5


Postfix expression: 32^5*32*3-/5+

You might also like