#include <stdio.h>
#include "Stack.h"

main()
 { Stack S;
   StackElementType n, n1, n2;
   int ch;
   clear(&S);
   printf("Reverse Polish Calculator,  Lloyd Allison, Australia\n");
   printf("Type R.P. expression<enter>, e.g.  2 3 4*+\n");

   while((ch=getchar()) == ' ');/*skip spaces*/
   while(ch != '\n')
    {
      if(ch>='0'&&ch<='9')
       { ungetc(ch, stdin);
         scanf("%d", &n); push(n, &S);
       }
      else
      if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
       { pop(&n2, &S); pop(&n1, &S);   /* pop two operands */
         switch(ch)
          { case '+': n=n1+n2; break;  /* appropriate operation */
            case '-': n=n1-n2; break;
            case '*': n=n1*n2; break;
            case '/': n=n1/n2; break;
          }
         push(n, &S);                  /* push result */
       }
      else printf(" <%c>?\n", ch);

      while((ch=getchar()) == ' ');
    }/*end of central loop*/

   printf("stack contents from top down:\n");
   while(!empty(S))
    { pop(&n, &S);
      printf("%d\n", n);
    }
   printf("---\n");
 }/*main*/

/* Simple Reverse Polish Calculator */
