Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>     /* for atof() */
  3. #include <ctype.h>
  4. #include <math.h>       /* adds access to f(x)'s sin, exp, and pow. */
  5.  
  6. #define MAXOP  100      /* max size of operand or operator */
  7. #define NUMBER '0'      /* signal that a number was found */
  8. #define MAXVAL 100      /* max depth of val stack */
  9.  
  10. #define BUFSIZE 100
  11.  
  12. #define IDENTIFIER 1    /* identifier for maths functions */
  13.  
  14. char buf[BUFSIZE];      /* buffer for ungetch */
  15. int bufp = 0;           /* next free position in buf */
  16.  
  17. int sp = 0;             /* next free stack position */
  18. double val[MAXVAL];     /* value stack */
  19.  
  20. int getops(char []);        /* function protos */
  21. void push(double);
  22. double pop(void);
  23.  
  24. void printStack(void);
  25. void dupeStack(void);
  26. void swapStack(void);
  27. void clearStack();
  28.  
  29. void expStack(void);
  30. void pwrStack(void);
  31. void sinStack(void);
  32. void dealWithName(char s[]);
  33.  
  34. int getch(void);       
  35. void ungetch(int);
  36.  
  37. /* reverse Polish notation calculator */
  38. main()
  39. {
  40.     int type;
  41.     double op2;
  42.     char s[MAXOP];
  43.    
  44.     printf("To print the top element of the stack type 'p'. \n To duplicate it, type 'd'. \n To swap the top two elements type 's'. \n To clear the stack type 'c'. (Case sensitive)");
  45.     printf("To use exponents, type ! \n to use power, type ~ \n To use sine use # \n");
  46.    
  47.     while ((type = getops(s)) != EOF)
  48.     {
  49.         switch (type)
  50.         {
  51.             case NUMBER:
  52.                 push(atof(s));
  53.                 break;
  54.            
  55.             case IDENTIFIER:
  56.                 dealWithName(s);
  57.                 break;
  58.            
  59.             case '+':
  60.                 push(pop() + pop());
  61.                 break;
  62.            
  63.             case '*':
  64.                 push(pop() * pop());
  65.                 break;
  66.            
  67.             case '-':
  68.                 op2 = pop();
  69.                 push(pop() - op2);
  70.                 break;
  71.            
  72.             case '/':
  73.                 op2 = pop();
  74.                 if (op2 != 0.0)
  75.                     push(pop() / op2);
  76.                 else
  77.                     printf("error: zero divisor\n");
  78.                 break;
  79.            
  80.             case '%':
  81.                 op2 = pop();
  82.                 if (op2 != 0.0)
  83.                     push(fmod(pop(), op2));
  84.                 else
  85.                     printf("error: zero division\n");
  86.            
  87.             case 'p': case 'P':
  88.                 printStack();
  89.                 break;
  90.            
  91.             case 'd': case 'D':
  92.                 dupeStack();
  93.                 break;
  94.            
  95.             case 's': case 'S':
  96.                 swapStack();
  97.                 break;
  98.            
  99.             case 'c': case 'C':
  100.                 clearStack();
  101.                 break;
  102.            
  103.             case '\n':
  104.                 printf("\t%.8g\n", pop());
  105.                 break;
  106.            
  107.             default:
  108.                 printf("error: unknown command %s\n", s);
  109.                 break;
  110.         }
  111.     }
  112.     return 0;
  113. }
  114.  
  115. /* push(): push f onto value stack */
  116.  
  117. void push(double f)
  118. {
  119.     if (sp < MAXVAL)
  120.     {
  121.         val[sp++] = f;
  122.     }
  123.    
  124.     else
  125.         printf("error: stack full, can't push %g\n", f);
  126. }
  127.  
  128. /* pop(): pop and return top val from stack */
  129.  
  130. double pop(void)
  131. {
  132.     if (sp > 0)
  133.     {
  134.         return val[--sp];
  135.     }
  136.     else
  137.     {
  138.         printf("error: stack empty\n");
  139.         return 0.0;
  140.     }
  141. }
  142.  
  143. /* printStack(): print the first item in the stack */
  144. void printStack(void)
  145. {
  146.     if (sp > 0)
  147.     {
  148.         printf("Top of stack contains: %8g\n", val[sp-1]);
  149.     }
  150.     else
  151.         printf("The stack is empty.\n");
  152. }
  153.  
  154. /* dupeStack(): duplicate the first item in the stack */
  155. void dupeStack(void)
  156. {
  157.     double temp = pop();
  158.     push(temp);
  159.     push(temp);
  160. }
  161.  
  162. /* swapStack(): swap the first and second item in the stack */
  163. void swapStack(void)
  164. {
  165.     double temp1 = pop();
  166.     double temp2 = pop();
  167.     push(temp1);
  168.     push(temp2);
  169. }
  170.  
  171. /* clearStack(): clear the stack empty */
  172. void clearStack()
  173. {
  174.     sp = 0;
  175. }
  176.  
  177. /* dealWithName(): identify pwr, exp, or sin, and handle accordingly */
  178. void dealWithName(char s[])
  179. {
  180.     double op2;
  181.    
  182.     if (0 == strcmp(s, "sin"))
  183.     {
  184.         push(sin(pop()));
  185.     }
  186.     else if (0 == strmcp(s, "cos"))
  187.     {
  188.         push(cos(pop()));
  189.     }
  190.     else if (0 == strcmp(s, "exp"))
  191.     {
  192.         push(exp(pop()));
  193.     }
  194.     else if (0 == strcmp(s, "pow"))
  195.     {
  196.         op2 = pop();
  197.         push(pow(pop(), op2));
  198.     }
  199.     else
  200.         printf("%s is not a supported function.\n", s);
  201. }
  202.  
  203. /* getop(): get next operator or numeric operand */
  204. int getops(char s[])
  205. {
  206.     int i, c;
  207.    
  208.     while ((s[0] = c = getch()) == ' ' || c == '\t')
  209.         ;
  210.    
  211.     s[1] = '\0';
  212.    
  213.     if (!isdigit(c) && c != '.')
  214.     {
  215.         return c;       /* NaN */
  216.     }
  217.    
  218.     i = 0;
  219.    
  220.     if (isdigit(c))     /* collect int part */
  221.     {
  222.         while (isdigit(s[++i] = c = getch()))
  223.             ;
  224.     }
  225.     if (c == '.')       /* collect fraction part */
  226.     {
  227.         while (isdigit(s[++i] = c = getch()))
  228.             ;
  229.     }
  230.    
  231.     s[i] = '\0';
  232.    
  233.     if (c != EOF)
  234.     {
  235.         ungetch(c);
  236.     }
  237.    
  238.     return NUMBER;
  239. }
  240.  
  241. int getch(void)             /* get a (possibly pushed back) char */
  242. {
  243.     return (bufp > 0) ? buf[--bufp] : getchar();
  244. }
  245.  
  246. void ungetch(int c)         /* push char back on input */
  247. {
  248.     if (bufp >= BUFSIZE)
  249.         printf("ungetch: too many chars\n");
  250.     else
  251.         buf[bufp++] = c;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement