Advertisement
Guest User

nutzz

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