#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXCOLS 80
#include<conio.h>
#define TRUE 1
#define FALSE 0

double eval(char expr[]);
double pop(struct stack *);
void push(struct stack * ,double);
int empty(struct stack *);
int isdigit(char);
double oper(int,double,double);
struct stack { int top; double items[MAXCOLS];};

void main()
{ clrscr();
  char expr[MAXCOLS]; int position=0;
  printf("Enter the postfix expression: ");   gets(expr);
//  while((expr[position++]=getchar())!='\n');
//  exp[--position]='\0';
  printf("\nThe original postfix expression is %s", expr);
  printf("\n%f", eval(expr));
  getch();
}

double eval(char expr[])
{
 int c,position;
 double opnd1,opnd2,value;
 struct stack opndstk;
 opndstk.top=-1;
 for(position=0;(c=expr[position])!='\0';position++)
 {
  if(isdigit(c)) push(&opndstk,(double)(c-'0'));
  else
  {
   opnd2=pop(&opndstk);
   opnd1=pop(&opndstk);
   value=oper(c,opnd1,opnd2);
   push(&opndstk,value);
  }
 }
 return(pop(&opndstk));
}
int isdigit(char symb) {return(symb>='0'&&symb<='9'); }

double pop(struct stack *s)
{
  if(!empty(s))  return(s->items[s->top--]);
  else return(0);
}
void push(struct stack *s, double c)
{
 if(s->top==(MAXCOLS-1)) printf("Stack overflow");
 else  s->items[++s->top]=c;
}
double oper(int symb,double op1,double op2)
{
  switch(symb)
  {
   case '+': return (op1+op2);
   case '-': return (op1-op2);
   case '*': return (op1*op2);
   case '/': return (op1/op2);
   case '$': return(pow(op1,op2));
   default : printf("%s", "illegal operation");
	     exit(1);return(0);
  }
}
int empty(struct stack *s)
{ return(s->top==-1); }