//Evaluation of postfix Expression
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct stk
{
int a[50];
int top;
};
main()
{
int i,a,b,r;
char exp[50],c='\0';
void push(struct stk *s,int item);
extern int isempty(struct stk *s);
extern int isfull(struct stk *s);
int pop(struct stk *s);
struct stk s;
s.top=-1;
printf("Enter the expression: ");
i=0;
while(1)
{
c=getchar();
fflush(stdin);
if(c!='\n')
exp[i]=c;
else
{
exp[i]='\0';
break;
}
i++;
}
printf("%s",exp);
i=0;
while(exp[i]!='\0')
{
if(exp[i]=='^'||exp[i]=='+'||exp[i]=='-'||exp[i]=='*'||exp[i]=='/')
{
a=pop(&s);
printf("\n a %d",a);
b=pop(&s);
printf("\n b %d",b);
if(exp[i]=='^')
r=pow(b,a);
else if(exp[i]=='/')
r=b/a;
else if(exp[i]=='*')
r=b*a;
else if(exp[i]=='+')
r=b+a;
else if(exp[i]=='-')
r=b-a;
push(&s,r);
}
else
{
r=exp[i]-48;
push(&s,r);
}
i=i+1;
}
r=pop(&s);
printf("\n result is %d",r);
getch();
return 0;
}
void push(struct stk *s,int item)
{
int c;
c=isfull(s);
if(c==0)
{
printf("stack is full. it is not possible to PUSH %d ",item);
return;
}
s->a[++(s->top)]=item;
}
int isfull(struct stk *s)
{
if(s->top==9)
return 0;
return 1;
}
int pop(struct stk *s)
{
int c,i;
c=isempty(s);
if(c==0)
{
printf("stack is empty. it is not possible to POP ");
return -1000;
}
i=s->a[(s->top)--];
return i;
}
int isempty(struct stk *s)
{
if(s->top==-1)
return 0;
return 1;
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
struct stk
{
int a[50];
int top;
};
main()
{
int i,a,b,r;
char exp[50],c='\0';
void push(struct stk *s,int item);
extern int isempty(struct stk *s);
extern int isfull(struct stk *s);
int pop(struct stk *s);
struct stk s;
s.top=-1;
printf("Enter the expression: ");
i=0;
while(1)
{
c=getchar();
fflush(stdin);
if(c!='\n')
exp[i]=c;
else
{
exp[i]='\0';
break;
}
i++;
}
printf("%s",exp);
i=0;
while(exp[i]!='\0')
{
if(exp[i]=='^'||exp[i]=='+'||exp[i]=='-'||exp[i]=='*'||exp[i]=='/')
{
a=pop(&s);
printf("\n a %d",a);
b=pop(&s);
printf("\n b %d",b);
if(exp[i]=='^')
r=pow(b,a);
else if(exp[i]=='/')
r=b/a;
else if(exp[i]=='*')
r=b*a;
else if(exp[i]=='+')
r=b+a;
else if(exp[i]=='-')
r=b-a;
push(&s,r);
}
else
{
r=exp[i]-48;
push(&s,r);
}
i=i+1;
}
r=pop(&s);
printf("\n result is %d",r);
getch();
return 0;
}
void push(struct stk *s,int item)
{
int c;
c=isfull(s);
if(c==0)
{
printf("stack is full. it is not possible to PUSH %d ",item);
return;
}
s->a[++(s->top)]=item;
}
int isfull(struct stk *s)
{
if(s->top==9)
return 0;
return 1;
}
int pop(struct stk *s)
{
int c,i;
c=isempty(s);
if(c==0)
{
printf("stack is empty. it is not possible to POP ");
return -1000;
}
i=s->a[(s->top)--];
return i;
}
int isempty(struct stk *s)
{
if(s->top==-1)
return 0;
return 1;
}