Advertisement
Guest User

Reverse Polish Calculator

a guest
Apr 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1.  
  2. #
  3.  
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <string.h>
  7. #include <math.h>
  8.  
  9. #define MAXOP 100
  10. #define NUMBER '\0'
  11. #define BUFSIZE 100
  12. #define MAXVAL 100
  13. int sp = 0;
  14. double val[MAXVAL];
  15.  
  16. char buf[BUFSIZE];
  17. int bufp = 0;
  18. void push(double f) {
  19.     if (sp < MAXVAL) {
  20.         val[sp++] = f;
  21.     }
  22.     else {
  23.         printf("error: stack full can't push");
  24.     }
  25. }
  26.  
  27.  
  28. double pop(void) {
  29.     if (sp > 0) {
  30.         return val[--sp];
  31.  
  32.     }
  33.     else {
  34.         printf("error: stack empty can't pop");
  35.         return 0.0;
  36.     }
  37.    
  38. }
  39.  
  40.  
  41. int getch(void) {
  42.  
  43.     return (bufp > 0) ? buf[--bufp] : getchar();
  44. }
  45.  
  46. void ungetch(int c) {
  47.     if (bufp >= BUFSIZE) {
  48.         printf("ungetch: too many characters\n");
  49.     }
  50.     else {
  51.         buf[bufp++] = c;
  52.     }
  53. }
  54.  
  55.  
  56. int getop(char s[]) {
  57.     int i, c;
  58.  
  59.     while ((s[0] = c = getch()) == ' ' || c == '\t') {
  60.  
  61.     }
  62.     s[1] = '\0';
  63.     if (!isdigit(c) && c != '.') {
  64.         return c;
  65.     }
  66.     i = 0;
  67.     if (!isdigit(c)) {
  68.         while (isdigit(s[++i] = c = getch())) {
  69.         }
  70.     }
  71.     if (c == '.') {
  72.         while (isdigit(s[++i] = c = getch())) {
  73.  
  74.         }
  75.     }
  76. }
  77.  
  78. int main(void) {
  79.  
  80.     int type;
  81.     double op2;
  82.     int sign = 1;
  83.     char s[MAXOP];
  84.     while ((type = getop(s)) != EOF) {
  85.         if (type == '-') {
  86.             sign = -1;
  87.             continue;
  88.         }
  89.         switch (type) {
  90.  
  91.         case NUMBER:
  92.             push(atof(s) * sign);
  93.             break;
  94.         case '+':
  95.             push(pop() + pop());
  96.             break;
  97.         case '-':
  98.             op2 = pop();
  99.             push(pop() - op2);
  100.             break;
  101.         case '/':
  102.             op2 = pop();
  103.             if (op2 != 0.0) {
  104.                 push(pop() / op2);
  105.                 break;
  106.             }
  107.             else {
  108.                 printf("Error: zero divisor\n");
  109.                 break;
  110.             }
  111.         case '\n':
  112.             printf("\t%.8g\n", pop());
  113.             break;
  114.         case '%':
  115.             op2 = pop();
  116.             push(fmod(pop(), op2));
  117.             break;
  118.         case 'P':
  119.             printf("%c", buf[bufp - 1]);
  120.             break;
  121.         case 'D':
  122.             push(buf[bufp - 1]);
  123.             break;
  124.         case 'S':
  125.             int c = buf[bufp - 1];
  126.             buf[bufp - 1] = buf[bufp - 2];
  127.             buf[bufp - 2] = c;
  128.             break;
  129.         case 'V':
  130.             for (int i = 0;i < BUFSIZE - 1;i++) {
  131.                 buf[i] = 0;
  132.             }
  133.             break;
  134.         case 'B':
  135.             push(sin(pop()));
  136.             break;
  137.         case 'N':
  138.             op2 = pop();
  139.             push(pow(pop(), op2));
  140.             break;
  141.         case 'L':
  142.             break;
  143.         default:
  144.             printf("error: unknown command %s\n", s);
  145.             break;
  146.         }
  147.         sign = 1;
  148.     }
  149.    
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement