Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>     /* for atof() */
  3. #include <ctype.h>
  4.  
  5. #define MAXOP  100      /* max size of operand or operator */
  6. #define NUMBER '0'      /* signal that a number was found */
  7. #define MAXVAL 100      /* max depth of val stack */
  8.  
  9. #define BUFSIZE 100     /* buffer for ungetch */
  10.  
  11. int bufp = 0;           /* next free position in buf */
  12.  
  13. int sp = 0;             /* next free stack position */
  14. extern double val[MAXVAL]       /* value stack */
  15.  
  16. int getops(char []);        /* function protos */
  17. void push(double);
  18. double pop(void);
  19.  
  20. int getch(void);       
  21. void ungetch(int);
  22.  
  23. /* reverse Polish notation calculator */
  24. main()
  25. {
  26.     int type;
  27.     double op2;
  28.     char s[MAXOP];
  29.    
  30.     while ((type = getops(s)) != EOF)
  31.     {
  32.         switch (type)
  33.         {
  34.             case NUMBER:
  35.                 push(atof(s));
  36.                 break;
  37.            
  38.             case '+':
  39.                 push(pop() + pop());
  40.                 break;
  41.            
  42.             case '*':
  43.                 push(pop() * pop());
  44.                 break;
  45.            
  46.             case '-':
  47.                 op2 = pop();
  48.                 push(pop() - op2);
  49.                 break;
  50.            
  51.             case '/':
  52.                 op2 = pop();
  53.                 if (op2 != 0.0)
  54.                     push(pop() / op2);
  55.                 else
  56.                     printf("error: zero divisor\n");
  57.                 break;
  58.            
  59.             case '\n':
  60.                 printf("\t%.8g\n", pop());
  61.                 break;
  62.            
  63.             default:
  64.                 printf("error: unknown command %s\n", s);
  65.                 break;
  66.         }
  67.     }
  68.     return 0;
  69. }
  70.  
  71. /* push(): push f onto value stack */
  72.  
  73. void push(double f)
  74. {
  75.     if (sp < MAXVAL)
  76.     {
  77.         val[sp++] = f;
  78.     }
  79.    
  80.     else
  81.         printf("error: stack full, can't push %g\n", f);
  82. }
  83.  
  84. /* pop(): pop and return top val from stack */
  85.  
  86. double pop(void)
  87. {
  88.     if (sp > 0)
  89.     {
  90.         return val[--sp];
  91.     }
  92.     else
  93.     {
  94.         printf("error: stack empty\n");
  95.         return 0.0;
  96.     }
  97. }
  98.  
  99. /* getop(): get next operator or numeric operand */
  100. int getops(char s[])
  101. {
  102.     int i, c;
  103.    
  104.     while ((s[0] = c = getch()) == ' ' || c == '\t')
  105.         ;
  106.    
  107.     s[1] = '\0';
  108.    
  109.     if (!isdigit(c) && c != '.')
  110.     {
  111.         return c;       /* NaN */
  112.     }
  113.    
  114.     i = 0;
  115.    
  116.     if (isdigit(c))     /* collect int part */
  117.     {
  118.         while (isdigit(s[++i] = c = getch()))
  119.             ;
  120.     }
  121.     if (c == '.')       /* collect fraction part */
  122.     {
  123.         while (isdigit(s[++i] = c = getch()))
  124.             ;
  125.     }
  126.    
  127.     s[i] = '\0';
  128.    
  129.     if (c != EOF)
  130.     {
  131.         ungetch(c);
  132.     }
  133.    
  134.     return NUMBER;
  135. }
  136.  
  137. int getch(void)             /* get a (possibly pushed back) char */
  138. {
  139.     return (bufp > 0) ? buf[--bufp] : getchar();
  140. }
  141.  
  142. void ungetch(int c)         /* push char back on input */
  143. {
  144.     if (bufp >= BUFSIZE)
  145.         printf("ungetch: too many chars\n");
  146.     else
  147.         buf[bufp++] = c;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement