home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume24 / hp / hp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-13  |  2.4 KB  |  169 lines

  1. /* hp --- reverse Polish notation calculator program */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5.  
  6. #define MAXSTACK    100
  7. #define TRUE        1
  8. #define FALSE        0
  9.  
  10. double stack[MAXSTACK + 1];
  11. int scanptr = -1;
  12. int sp = 0;
  13. char line[BUFSIZ*4];
  14.  
  15. extern char *strcpy();
  16.  
  17. main(argc, argv)
  18. int argc;
  19. char **argv;
  20. {
  21.     int i, l;
  22.     int sound();
  23.  
  24.     if (argc > 1) {
  25.         l = 0;
  26.         /* concatenate args into one line for input() to use... */
  27.         for (i = 1; argv[i] != NULL; i++) {
  28.             (void) strcpy(& line[l], argv[i]);
  29.             l += strlen(argv[i]);
  30.             line[l] = ' ';
  31.             l++;
  32.         }
  33.         line[l] = '\0';
  34.         scanptr = -1;
  35.         yyparse();
  36.         if (sound(1))
  37.             printf("%1.3lf\n", stack[sp]);
  38.     } else
  39.         while (fgets(line, sizeof line, stdin) != NULL) {
  40.             scanptr = -1;
  41.             yyparse();
  42.         }
  43.     
  44.     exit(0);
  45. }
  46.  
  47. /* ctod --- do atof, but increment pointer into the line buffer */
  48.  
  49. double ctod(text, indx)
  50. char text[];
  51. int *indx;
  52. {
  53.     double result, atof();
  54.     char buf[BUFSIZ];
  55.     int i = 0;
  56.  
  57.     while (isdigit(text[*indx])) {
  58.         buf[i++] = text[*indx];
  59.         (*indx)++;
  60.     }
  61.  
  62.     if (text[*indx] == '.') {
  63.         buf[i++] = '.';
  64.         (*indx)++;
  65.     }
  66.     
  67.     while (isdigit(text[*indx])) {
  68.         buf[i++] = text[*indx];
  69.         (*indx)++;
  70.     }
  71.  
  72.     if (text[*indx] == 'e' ||  text[*indx] == 'E') {
  73.         buf[i++] = text[*indx];
  74.         (*indx)++;
  75.     
  76.         if (text[*indx] == '-' || text[*indx] == '+') {
  77.             buf[i++] = text[*indx];
  78.             (*indx)++;
  79.         }
  80.  
  81.         while (isdigit(text[*indx])) {
  82.             buf[i++] = text[*indx];
  83.             (*indx)++;
  84.         }
  85.     }
  86.  
  87.     buf[i] = '\0';
  88.  
  89.     result = atof(buf);
  90.     return result;
  91. }
  92.  
  93. /* fmod --- do floating modulus */
  94.  
  95. double fmod(x, y)
  96. double x, y;
  97. {
  98.     extern double modf();
  99.     extern double fabs();
  100.     double f;
  101.     int flag = 0;
  102.     double ipart;
  103.  
  104.     if (y == 0.0)
  105.         return x;
  106.  
  107.     flag = (x < 0.0);
  108.     x = fabs(x);
  109.     y = fabs(y);
  110.     (void) modf(x / y, & ipart);
  111.     f = x - y * ipart;
  112.     return (flag ? -f : f);
  113. }
  114.  
  115. /* push --- push one item onto the stack */
  116.  
  117. push(stuff)
  118. double stuff;
  119. {
  120.     if (sp > MAXSTACK)
  121.         fprintf(stderr, "stack overflow\n");
  122.     else {
  123.         sp++;
  124.         stack[sp] = stuff;
  125.     }
  126. }
  127.  
  128.  
  129.  
  130. /* sound --- sound out the depth of the stack */
  131.  
  132. int sound(depth)
  133. int depth;
  134. {
  135.     if (sp < depth) {
  136.         fprintf(stderr, "stack underflow\n");
  137.         return FALSE;
  138.     } else
  139.         return TRUE;
  140. }
  141.  
  142. int input()
  143. {
  144.     register int c;
  145.  
  146. again:
  147.     scanptr++;
  148.     if ((c = line[scanptr]) == '\0') {
  149.         scanptr = -1;
  150.         return 0;
  151.     } else if (isspace(c))
  152.         goto again;
  153.     else {
  154.         return c;
  155.     }
  156. }
  157.  
  158. int unput(c)
  159. int c;
  160. {
  161.     line[--scanptr] = c;
  162. }
  163.  
  164. int yyerror(s)
  165. char *s;
  166. {
  167.     fprintf(stderr, "hp: %s\n", s);
  168. }
  169.