PROGRAM:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void push(int);
int pop();
int stack[20],top=-1;
void main()
{
char expr[20];
int len,a,b,c,i,x;
clrscr();
printf("$$$ POSTFIX EVALUATION $$$");
printf("\nEnter the postfix expression:");
scanf("%s",expr);
len=strlen(expr);
for(i=0;i<len;i++)
{
if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/')
{
b=pop();
a=pop();
switch(expr[i])
{
case '+':c=a+b;
push(c);
break;
case '-':c=a-b;
push(c);
break;
case '*':c=a*b;
push(c);
break;
case '/':c=a/b;
push(c);
break;
}
}
else
{
printf("Enter the value of %c:",expr[i]);
scanf("%d",&x);
push(x);
}
}
printf("The resultant value is : %d",stack[top]);
getch();
}
void push(int item)
{
stack[++top]=item;
}
int pop()
{
int a;
a=stack[top];
top=top-1;
return a;
}
OUTPUT:
$$$ POSTFIX EVALUATION $$$
Enter the postfix expression: abc+*
Enter the value of a: 10
Enter the value of b: 20
Enter the value of c: 30
The resultant value is : 500
#include<stdio.h>
#include<string.h>
#include<conio.h>
void push(int);
int pop();
int stack[20],top=-1;
void main()
{
char expr[20];
int len,a,b,c,i,x;
clrscr();
printf("$$$ POSTFIX EVALUATION $$$");
printf("\nEnter the postfix expression:");
scanf("%s",expr);
len=strlen(expr);
for(i=0;i<len;i++)
{
if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/')
{
b=pop();
a=pop();
switch(expr[i])
{
case '+':c=a+b;
push(c);
break;
case '-':c=a-b;
push(c);
break;
case '*':c=a*b;
push(c);
break;
case '/':c=a/b;
push(c);
break;
}
}
else
{
printf("Enter the value of %c:",expr[i]);
scanf("%d",&x);
push(x);
}
}
printf("The resultant value is : %d",stack[top]);
getch();
}
void push(int item)
{
stack[++top]=item;
}
int pop()
{
int a;
a=stack[top];
top=top-1;
return a;
}
OUTPUT:
$$$ POSTFIX EVALUATION $$$
Enter the postfix expression: abc+*
Enter the value of a: 10
Enter the value of b: 20
Enter the value of c: 30
The resultant value is : 500
No comments:
Post a Comment