Data structure Program | postfix evaluation | source code
/* program for postfix evaluation */
#include<stdio.h>
#include<conio.h>
#define max 50
typedef struct stack
{
int top;
int item[max];
}stack;
stack s1;
void push(int n)
{
if(s1.top==max-1)
puts("\nstack is full");
else
s1.item[++s1.top]=n;
}
int pop()
{
if(s1.top==-1)
return s1.top;
else
return(s1.item[s1.top--]);
}
void main()
{
int a,b,c,d,e,i,op1,op2;
char pos[30];
s1.top=-1;
clrscr();
puts("\nenter the value of a,b,c,d,e");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("\nenter postfix expression:- ");
scanf("%s",&pos);
for(i=0;pos[i]!='\0';i++)
{
switch(pos[i])
{
case 'a':
push(a);
break;
case 'b':
push(b);
break;
case 'c':
push(c);
break;
case 'd':
push(d);
break;
case 'e':
push(e);
break;
default:
op2=pop();
op1=pop();
}
switch(pos[i])
{
case '+':
push(op1+op2);
break;
case '-':
push(op1-op2);
break;
case '*':
push(op1*op2);
break;
case '/':
push(op1/op2);
break;
case '%':
push(op1%op2);
break;
}
}
printf("\nvalue of postfix expression is:- %d",pop());
getch();
}
/* program for postfix evaluation */
#include<stdio.h>
#include<conio.h>
#define max 50
typedef struct stack
{
int top;
int item[max];
}stack;
stack s1;
void push(int n)
{
if(s1.top==max-1)
puts("\nstack is full");
else
s1.item[++s1.top]=n;
}
int pop()
{
if(s1.top==-1)
return s1.top;
else
return(s1.item[s1.top--]);
}
void main()
{
int a,b,c,d,e,i,op1,op2;
char pos[30];
s1.top=-1;
clrscr();
puts("\nenter the value of a,b,c,d,e");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("\nenter postfix expression:- ");
scanf("%s",&pos);
for(i=0;pos[i]!='\0';i++)
{
switch(pos[i])
{
case 'a':
push(a);
break;
case 'b':
push(b);
break;
case 'c':
push(c);
break;
case 'd':
push(d);
break;
case 'e':
push(e);
break;
default:
op2=pop();
op1=pop();
}
switch(pos[i])
{
case '+':
push(op1+op2);
break;
case '-':
push(op1-op2);
break;
case '*':
push(op1*op2);
break;
case '/':
push(op1/op2);
break;
case '%':
push(op1%op2);
break;
}
}
printf("\nvalue of postfix expression is:- %d",pop());
getch();
}
0 comments: