#include#define MAX 100
void push(int);
int pop();
int top=-1,f=0,i;
int stack[MAX];
void main()
{
char post[100],x;
int value, a,b;
clrscr();
printf("Enter the Postfix Expression....");
gets(post);
for( i=0;(x=post[i])!='';i++)
{
if(isdigit(x))
{
push(x -'0');
}
else
{
a=pop();
b=pop();
value=perform(x,a,b);
push(value);
}
}
gets(post);
for(i=0;(x=post[i])!='';i++)
{
if(isdigit(x))
{
push(x=0);
}
else
{
a=pop();
b=pop();
value=perform(x,a,b);
push(value);
}
}
printf("The value of the postfix expression is :%d
",stack[top]);
getch();
}
int perform(char y,int m, int n)
{
int k;
switch(y)
{
case '+':k=n+m;
break;
case '-':k=n-m;
break;
case '*':k=n*m;
break;
case '/':k=n/m;
break;
case '^':k=pow(n,m);
break;
}
return(k);
}
void push(int item)
{
if(top==MAX)
{
printf("overflow
");
return;
}
else
{
top=top+1;
stack[top]=item;
}
return;
}
int pop(int item)
{
if(top==-1)
{
printf("underflow
");
return;
}
else
{
item=stack[top];
top=top-1;
return item;
}
}
Comments
: #define MAX 100
: void push(int);
: int pop();
: int top=-1,f=0,i;
: int stack[MAX];
: void main()
: {
: char post[100],x;
: int value, a,b;
: clrscr();
: printf("Enter the Postfix Expression....");
: gets(post);
: for( i=0;(x=post[i])!='';i++)
: {
: if(isdigit(x))
: {
: push(x -'0');
: }
: else
: {
: a=pop();
: b=pop();
: value=perform(x,a,b);
: push(value);
: }
: }
: gets(post);
: for(i=0;(x=post[i])!='';i++)
: {
: if(isdigit(x))
: {
: push(x=0);
: }
: else
: {
: a=pop();
: b=pop();
: value=perform(x,a,b);
: push(value);
: }
: }
: printf("The value of the postfix expression is :%d
",stack[top]);
: getch();
: }
:
: int perform(char y,int m, int n)
: {
: int k;
: switch(y)
: {
: case '+':k=n+m;
: break;
: case '-':k=n-m;
: break;
: case '*':k=n*m;
: break;
: case '/':k=n/m;
: break;
: case '^':k=pow(n,m);
: break;
: }
: return(k);
: }
: void push(int item)
: {
: if(top==MAX)
: {
: printf("overflow
");
: return;
: }
: else
: {
: top=top+1;
: stack[top]=item;
: }
: return;
: }
: int pop(int item)
: {
: if(top==-1)
: {
: printf("underflow
");
: return;
: }
: else
: {
: item=stack[top];
:
: top=top-1;
: return item;
: }
: }
:
the way your declaring your functions are not good.
if your pushing into the stack then declare your prototype before your main method
void push(int p);
int pop(void);
and for your pop you put a variable in the method declaration
int pop(int item);
you don't need to put a variable for any stack pop method
should be int pop(void);
the way your declaring your functions are not good.
if your pushing into the stack then declare your prototype before your main method
void push(int p);
int pop(void);
and for your pop you put a variable in the method declaration
int pop(int item);
you don't need to put a variable for any stack pop method
should be int pop(void);
the way your declaring your functions are not good.
if your pushing into the stack then declare your prototype before your main method
void push(int p);
int pop(void);
and for your pop you put a variable in the method declaration
int pop(int item);
you don't need to put a variable for any stack pop method
should be int pop(void);
the way your declaring your functions are not good.
if your pushing into the stack then declare your prototype before your main method
void push(int p);
int pop(void);
and for your pop you put a variable in the method declaration
int pop(int item);
you don't need to put a variable for any stack pop method
should be int pop(void);