#include #include #include #include #include struct st { int data; struct st *next; }; typedef struct st st; st *base,*newnode,*old; char operators[]={'+','-','*','/','%','\0'}; void push(int num) { if(base==NULL) { base=(st *)malloc(sizeof(st)); base->next=NULL; base->data=num; old=base; } else { newnode=(st *)malloc(sizeof(st)); newnode->next=NULL; newnode->data=num; old->next=newnode; old=newnode; } } int pop() { int num; st *temp; if(base==NULL) return(NULL); if(old==base) { num=base->data; free(base); base=NULL; } else { temp=old; old=base; while(old->next!=temp) old=old->next; num=temp->data; free(temp); } return(num); } int isoperator(char op) { int i; for(i=0;operators[i]!='\0';i++) { if(op==operators[i]) return(1); } return(0); } void main() { int num,op1,op2; char ch; clrscr(); printf("Enter the postfix expression: "); base=NULL; while(1) { ch=getche(); if(ch==13) break; if(isdigit(ch)) { num=atoi(&ch); push(num); } if(isoperator(ch)) { op1=pop(); op2=pop(); if(op1==NULL||op2==NULL) { printf("\nError formulating or typing postfix expression. Aborting."); getch(); exit(EXIT_FAILURE); } switch(ch) { case '+': num=op1+op2; break; case '-': num=op1-op2; break; case '*': num=op1*op2; break; case '/': num=op1/op2; break; case '%': num=op1%op2; break; default: break; } push(num); } } num=pop(); printf("\nFinal answer of computation=%d",num); getch(); exit(EXIT_SUCCESS); }