c program for convert infix to post fix expression - c program with parth patthar

c program with parth patthar

learn c and c++ in better way and with simple example.

Believe on your own logic

Sunday, September 30, 2018

c program for convert infix to post fix expression

#include<stdio.h>
#include<conio.h>
void main()
{
char q[30],s[30],p[30];
int i,top=0,a=0;
clrscr();
printf("\n enter your expretion :");
scanf("%s",q);
strcat(q,")");
s[top]='(';
for(i=0;q[i];i++)
{
switch(q[i])
{
case '(':
top++;
s[top]=q[i];
break;
case '+':
case '-':
while(s[top]=='+'||s[top]=='-'||s[top]=='*'||s[top]=='/')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case '*':
case '/':
       while(s[top]=='*'||s[top]=='/'||s[top]=='^')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case '^':
while(s[top]=='^')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case ')':

while(s[top]!='(')
{
p[a]=s[top];
top--;
a++;
}
break;
default:
p[a]=q[i];
a++;
}
}
for(i=0;i<a;i++)
   printf("%c",p[i]);

getch();

}

No comments:

Post a Comment