home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / rpn / rpn.c < prev   
C/C++ Source or Header  |  1989-03-07  |  4KB  |  176 lines

  1. /* rpn - a reverse polish notation calculator */
  2. /* By Nils McCarthy with special thanks to:
  3.     THE ZOO */
  4. /* COPYRIGHT 1898 NILS MCCARTHY */
  5. /* THIS CODE MAY BE DISTRIBUTED PROVIDED THIS HEADER IS KEPT WITH IT */
  6. /* THIS CODE, EXCLUDING THIS HEADER MAY BE MODIFIED */
  7. /* any questions about the copyright, mail to mtymp01@ux.acss.umn.edu */
  8.  
  9.  
  10. #include <stdio.h>
  11.  
  12. int stacklength,*stack;
  13.  
  14. int top(void)
  15. {
  16.     return stack[stacklength-1];
  17. }
  18.  
  19. pr(options)
  20. char *options;
  21. {
  22.     if(*options==' ') options++;
  23.     if(*options==NULL) {
  24.         printf("%d\n",top());
  25.         return;
  26.     }
  27.     if((stacklength-1-atoi(options))<0) fprintf(stderr,"ERROR: INVALID %s",
  28.                 "STACK ADDRESSING\n");
  29.     printf("%d\n",stack[stacklength-1-atoi(options)]);
  30. }
  31.  
  32. push(thing)
  33. int thing;
  34. {
  35.     stacklength++;
  36.     stack[stacklength-1]=thing;
  37.     if(stacklength>=99) {
  38.         fprintf(stderr,"ERROR: STACK OVERFLOW\n");
  39.         /*exit(-1);*/
  40.     }
  41. }
  42.  
  43. int pop(void)
  44. {
  45.     int thing;
  46.     thing=stack[stacklength-1];
  47.     stacklength--;
  48.     if(stacklength<0) {
  49.           fprintf(stderr,"ERROR: STACK UNDERFLOW\n");    
  50.           /*exit(-1);*/
  51.       }
  52.     return thing;
  53. }
  54.  
  55. void add(void)
  56. {
  57.     int thing;
  58.     thing=pop();
  59.     push(pop()+thing);
  60. }
  61.  
  62. void sub(void)
  63. {
  64.     int temp;
  65.     temp=pop();
  66.     push(pop()-temp);
  67. }
  68.  
  69. void mul(void)
  70. {
  71.     int temp;
  72.     temp=pop();
  73.     push(pop()*temp);
  74. }
  75.  
  76. void div(void)
  77. {
  78.     int temp;
  79.     temp=pop();
  80.     push(pop()/temp);
  81. }
  82.  
  83. void mod(void)
  84. {
  85.     int temp;
  86.     temp=pop();
  87.     push(pop()%temp);
  88. }
  89.  
  90. void not(void)
  91. {
  92.     push(~pop());
  93. }
  94.  
  95. void xor(void)
  96. {
  97.     int thing;
  98.     thing=pop();
  99.     push(pop()^thing);
  100. }
  101.  
  102. void and(void)
  103. {
  104.     int thing;
  105.     thing=pop();
  106.     push(pop()&thing);
  107. }
  108.  
  109. void or(void)
  110. {
  111.     int thing;
  112.     thing=pop();
  113.     push(pop()|thing);
  114. }
  115.  
  116. void help(void)
  117. {
  118.     printf("\n\nWELCOME TO RPN, a reverse polish notation calculator.\n");
  119.     printf("the functions are as follows:\n");
  120.     printf("add - add the top two numbers on the stack\n");
  121.     printf("sub - subtract the top two numbers on the stack\n");
  122.     printf("mul - multiply the top two numbers on the stack\n");
  123.     printf("div - divide the top two numbers on the stack\n");
  124.     printf("pop - discard the top element of the stack\n");
  125.     printf("mod - take the remainder of the division of the top two numbers on the stack\n");
  126.     printf("not - take the compliment of the top element on the stack\n");
  127.     printf("help - display this help\n");
  128.     printf("xor - exclusive or\n");
  129.     printf("or - not-exclusive or\n");
  130.     printf("and - bitwise and\n");
  131.     printf("pr [number] - print the number [number] deep in the stack. defaults to 0\n");
  132.     printf("sw - switch printing top of stack on/off\n");
  133.     printf("(not that you'll ever need this, but:)\n");
  134.     printf("end - terminate the progran\n\n");
  135. }
  136.  
  137. main(argc,argv)
  138. int argc;
  139. char *argv[];
  140. {
  141.     char showstack=(~0); /* closest i can get to boolean */
  142.     int stackspace[100];
  143.     char input[100];
  144.     stack=stackspace;
  145. /* argument stuff */
  146.     if(argc>1) showstack=0;
  147. /* end of argument stuff (short, eh?) */
  148.     printf("\nWELCOME TO RPN!!! (Rpn version 1) by Nils McCarthy\n\n");
  149.     while (scanf("%s",input)!=EOF) {
  150.         if(!strcmp(input,"add")) add();
  151.         else if(!strcmp(input,"sub")) sub();
  152.         else if(!strcmp(input,"mul")) mul();
  153.         else if(!strcmp(input,"div")) div();
  154.         else if(!strcmp(input,"pop")) pop();
  155.         else if(!strcmp(input,"?"))  help();
  156.         else if(!strcmp(input,"h")) help();
  157.         else if(!strcmp(input,"sw")) showstack=~showstack;
  158.         else if(!strncmp(input,"pr",2)) pr(input+2);
  159.         else if(!strcmp(input,"help")) help(); 
  160.         else if(!strcmp(input,"xor")) xor();
  161.         else if(!strcmp(input,"or")) or();
  162.         else if(!strcmp(input,"and")) and();
  163.         else if(!strcmp(input,"x")) break;
  164.         else if(!strcmp(input,"q")) break;
  165.         else if(!strcmp(input,"exit")) break;
  166.         else if(!strcmp(input,"quit")) break;
  167.         else if(!strcmp(input,"end")) break;
  168.         else push(atoi(input));
  169.         if(showstack) {
  170.             printf("\t\tstack top: %d\n",top());
  171.             printf("\t\t\tstacksize: %d\n",stacklength);
  172.         }
  173.     }
  174.     printf("Bye Bye, now! Please try rpn again!\n\n");
  175. }
  176.