Evaluation of Postfix and Prefix Expression
#include <conio.h>
#include <stdio.h>
#include <string.h>
int menu()
{
int ch;
printf("\n\nMENU");
printf("\n1. Evaluate postfix expression");
printf("\n2. Evaluate prefix expression");
printf("\nEnter your choice: ");
scanf("%d",&ch);
return ch;
}
int push(float stack[],float ch,int top)
{
top=top+1;
stack[top]=ch;
return top;
}
float pop(int *top,float stack[])
{
float y;
y=stack[*top];
*top=*top-1;
return y;
}
float exp(float x,float y)
{
float ans=x;
int i=1;
while (i<y)
{
ans=ans*x;
i++;
}
return ans;
}
void postfix()
{
int top=-1,i=0;
float e,var,y,x,ans,stack[100];
char post[100],ch;
printf("\nEnter postfix expression: ");
scanf("%s",post);
while((post[i])!='\0')
{
ch=post[i];
if ((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')||(ch=='^'))
{
y=pop(&top,stack);
x=pop(&top,stack);
switch(ch)
{
case '+': top=push(stack,(x+y),top);
break;
case '-': top=push(stack,(x-y),top);
break;
case '*': top=push(stack,(x*y),top);
break;
case '/': top=push(stack,(x/y),top);
break;
case '^': top=push(stack,exp(x,y),top);
break;
}
}
else
{
printf("\nEnter value for %c: ",ch);
scanf("%f",&var);
top=push(stack,var,top);
}
i=i+1;
}
ans=pop(&top,stack);
printf("\nResultant is: %f",ans);
}
void prefix()
{
int top=-1,i=0,len;
float e,var,y,x,ans,stack[100];
char pre[100],ch;
printf("\nEnter prefix expression: ");
scanf("%s",pre);
len=strlen(pre);
while(len-1>=0)
{
ch=pre[len-1];
if ((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')||(ch=='^'))
{
x=pop(&top,stack);
y=pop(&top,stack);
switch(ch)
{
case '+': top=push(stack,(x+y),top);
break;
case '-': top=push(stack,(x-y),top);
break;
case '*': top=push(stack,(x*y),top);
break;
case '/': top=push(stack,(x/y),top);
break;
case '^': top=push(stack,e=exp(x,y),top);
break;
}
}
else
{
printf("\nEnter value for %c: ",ch);
scanf("%f",&var);
top=push(stack,var,top);
}
len=len-1;
}
ans=pop(&top,stack);
printf("\nResultant is: %f",ans);
}
int main()
{
int ch;
do
{
ch=menu();
switch(ch)
{
case 1: postfix();
break;
case 2: prefix();
break;
default:printf("\nWrong Choice!!");
}
}while(1);
}
#include <stdio.h>
#include <string.h>
int menu()
{
int ch;
printf("\n\nMENU");
printf("\n1. Evaluate postfix expression");
printf("\n2. Evaluate prefix expression");
printf("\nEnter your choice: ");
scanf("%d",&ch);
return ch;
}
int push(float stack[],float ch,int top)
{
top=top+1;
stack[top]=ch;
return top;
}
float pop(int *top,float stack[])
{
float y;
y=stack[*top];
*top=*top-1;
return y;
}
float exp(float x,float y)
{
float ans=x;
int i=1;
while (i<y)
{
ans=ans*x;
i++;
}
return ans;
}
void postfix()
{
int top=-1,i=0;
float e,var,y,x,ans,stack[100];
char post[100],ch;
printf("\nEnter postfix expression: ");
scanf("%s",post);
while((post[i])!='\0')
{
ch=post[i];
if ((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')||(ch=='^'))
{
y=pop(&top,stack);
x=pop(&top,stack);
switch(ch)
{
case '+': top=push(stack,(x+y),top);
break;
case '-': top=push(stack,(x-y),top);
break;
case '*': top=push(stack,(x*y),top);
break;
case '/': top=push(stack,(x/y),top);
break;
case '^': top=push(stack,exp(x,y),top);
break;
}
}
else
{
printf("\nEnter value for %c: ",ch);
scanf("%f",&var);
top=push(stack,var,top);
}
i=i+1;
}
ans=pop(&top,stack);
printf("\nResultant is: %f",ans);
}
void prefix()
{
int top=-1,i=0,len;
float e,var,y,x,ans,stack[100];
char pre[100],ch;
printf("\nEnter prefix expression: ");
scanf("%s",pre);
len=strlen(pre);
while(len-1>=0)
{
ch=pre[len-1];
if ((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')||(ch=='^'))
{
x=pop(&top,stack);
y=pop(&top,stack);
switch(ch)
{
case '+': top=push(stack,(x+y),top);
break;
case '-': top=push(stack,(x-y),top);
break;
case '*': top=push(stack,(x*y),top);
break;
case '/': top=push(stack,(x/y),top);
break;
case '^': top=push(stack,e=exp(x,y),top);
break;
}
}
else
{
printf("\nEnter value for %c: ",ch);
scanf("%f",&var);
top=push(stack,var,top);
}
len=len-1;
}
ans=pop(&top,stack);
printf("\nResultant is: %f",ans);
}
int main()
{
int ch;
do
{
ch=menu();
switch(ch)
{
case 1: postfix();
break;
case 2: prefix();
break;
default:printf("\nWrong Choice!!");
}
}while(1);
}
No comments