2016-04-17 15:06:43
include<math.h>
#include<string.h>
int top=0,i,l;
char a[100],b[200],c[100];
int main()
{
int t=0;
printf("Infix Exp1: ");
gets(a);
 
l=strlen(a);
for(i=0;i<l;i++)
{
if((a[i]>='0'&&a[i]<='9')||(a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z'))
{
c[t]=a[i];
++t;
}
else if(a[i]=='(')
{
b[top]='(';
top++;
}
else if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'||a[i]=='^')
{
int j=0,k=0;
if(a[i]=='+'||a[i]=='-')
j=1;
else if(a[i]=='*'||a[i]=='/')
j=2;
else if(a[i]=='^')
j=3;
while(b[top-1]=='+'||b[top-1]=='-'||b[top-1]=='*'||b[top-1]=='/'||b[top-1]=='^')
{
k=0;
if(b[top-1]=='+'||b[top-1]=='-')
k=1;
else if(b[top-1]=='*'||b[top-1]=='/')
k=2;
else if(b[top-1]=='^')
k=3;
if(k>=j)
{
c[t]=b[top-1];
++t;
c[t]=' ';
++t;
top--;
}
else
break;
}
b[top]=a[i];
++top;
}
else if(a[i]==')')
{
while(b[top-1]!='(')
{
c[t]=b[top-1];
++t;
c[t]=' ';
++t;
top--;
}
if(b[top-1]=='(')
top--;
}
else
continue;
if(((a[i]>='0'&&a[i]<='9')&&(a[i+1]<'0'||a[i+1]>'9'))||((a[i]>='a'&&a[i]<='z')&&(a[i+1]<'a'||a[i]>'z'))||((a[i]>='A'&&a[i]<='Z')&&(a[i+1]<'A'||a[i+1]>'Z')))
{
c[t]=' ';
++t;
}
}
if(top>0){
for(int u=top-1;u>=0;u--)
{
c[t]=b[u];
++t;
c[t]=' ';
t++;
}
}
printf("Postfix Expretion1: ");
for(i=0;i<t;i++)
printf("%c",c[i]);
//****************************postfix evaluation ******************************************
int p;
top=0;
for(i=0;i<t;i++)
{
if(c[i]>='0'&&c[i]<='9')
{
p=0;
while(c[i]>='0'&&c[i]<='9')
{
p=p*10+(c[i]-48);
i++;
}
a[top]=p;
top++;
}
else if(c[i]=='+')
{
a[top-2]=(a[top-2]+a[top-1]);
top-=1;
}
else if(c[i]=='-')
{
a[top-2]=(a[top-2]-a[top-1]);
top-=1;
}
else if(c[i]=='*')
{
a[top-2]=(a[top-2]*a[top-1]);
top-=1;
}
else if(c[i]=='/')
{
a[top-2]=(a[top-2]/a[top-1]);
top-=1;
}
else if(c[i]=='^')
{
a[top-2]=pow(a[top-2],a[top-1]);
top-=1;
}
else
continue;
}
printf("\nResult of infix Exp1: %d\n",a[0]);
 
}
Invalid Email or Password