home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 150_01 / hp.c < prev    next >
Text File  |  1985-09-07  |  3KB  |  109 lines

  1. /*
  2.        HEADER:  150.??;
  3.         TITLE:  Reverse Polish Notation (RPN) Calculator;
  4.   DESCRIPTION:  "A small program which turns your PC into a very expensive, 
  5.                  but convenient(?), pocket calculator.  It operates like the
  6.                  Hewlett-Packard calculators, which use RPN.  It has a 6 value
  7.                  stack, although this could easily be modified.  It is NOT a
  8.                  "resident" program, like the commercial desktop assistant
  9.                  programs.  Commands: "," (clear top), "+", "-", "*", "/",
  10.                  "^" (raise to power), "I" (invert, or 1/x), "N" (negate), "R" 
  11.                  (square root), "L" (log, to base e), "E" (exponential),
  12.                  and "Q" (quit).  Latter values to 1E-7 precision.";
  13.      KEYWORDS:  Calculator, RPN, Reverse Polish Notation;
  14.        SYSTEM:  any with suitable C compiler;
  15.      FILENAME:  HP.C;
  16.      WARNINGS:  "1) User documentation not included.  
  17.                  2) Requires C compiler with double precision real numbers.
  18.                  3) Requires file TRAN.C to compute transcendental functions.";
  19.      SEE-ALSO:  TRAN.C;
  20.     COMPILERS:  any C compiler with double prec. reals;
  21.    REFERENCES:  AUTHORS: unknown; TITLE: "HP.C";
  22.                 CITATION: "PC-SIG Disk 50 or 142" 
  23.        ENDREF;
  24. */
  25. #include "stdio.h"
  26. #include "tran.c"
  27. double pop(),push();
  28. double stack[6];
  29. int sp=0;
  30. main()
  31. {
  32. double dp=1;
  33. int x,i,df=0;
  34. while ((x=getchar()) != 'q' && x != 'Q') {
  35.     if (x>='0' && x<='9') {
  36.       if (df) dp=10*dp;
  37.       stack[0]=10*stack[0]+x-'0';
  38.     }
  39.     else if (x=='.') df=1;
  40.     else {
  41.       stack[0] = stack[0] / dp;
  42.       dp=1;
  43.       df=0;
  44.       if (x==',' && stack[0] == 0) stack[0]=stack[sp];
  45.       if (stack[0] == 0) stack[0]=pop();
  46.       switch (x) {
  47.       case ',' :
  48.       case '\13' :
  49.         break;
  50.       case '+' :
  51.         stack[0] = stack[0]+pop();
  52.         break;
  53.       case '-' :
  54.         stack[0] = pop()-stack[0];
  55.         break;
  56.       case '*' :
  57.         stack[0] = pop()*stack[0];
  58.         break;
  59.       case '/' :
  60.         stack[0] = pop()/stack[0];
  61.         break;
  62.       case '^' :
  63.         stack[0] = exp( stack[0] * log( pop(0) ) ) ;
  64.         break;
  65.       case 'I' :
  66.       case 'i' :
  67.         stack[0] = 1/stack[0];
  68.         break;
  69.       case 'N' :
  70.       case 'n' :
  71.         stack[0] = -stack[0];
  72.         break;
  73.       case 'r' :
  74.       case 'R' :
  75.         stack[0]=root(stack[0]);
  76.         break;
  77.       case 'l' :
  78.       case 'L' :
  79.         stack[0]=log(stack[0]);
  80.         break;
  81.       case 'e' :
  82.       case 'E' :
  83.         stack[0]=exp(stack[0]);
  84.         break;
  85.       }
  86.       push (stack[0]);
  87.       stack[0]=0;
  88.       if (x != '\0') {
  89.       for (i=1;i<=sp;i++) printf("%f  ",stack[i]);
  90.       printf ("%c\n",x);
  91.       }
  92.       }
  93.       }
  94. }
  95.  
  96. /* Pop floating from stack */
  97. double pop()
  98. {
  99. if (sp>0) return (stack[sp--]);
  100. else return (0);
  101. }
  102.  
  103. /* Push floating onto stack */
  104. double push(x)
  105. double x;
  106. {
  107. if (sp<5) stack[++sp] = x;
  108. }
  109.