Advertisement
Mary_99

LAB 3

Jan 30th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h> /* for atof() */
  4. #include<ctype.h>
  5. #define MAXOP 100 /* max size of operand or operator */
  6. #define NUMBER '0' /* signal that the number was found*/
  7. #define MAXVAL 100
  8. #define BUFSIZE 100
  9. //----------------------------------------function declaration----------------------------------------------------------
  10. int getop(char []);
  11. void push(double);//put on the top
  12. double pop(void); //put down from the stack
  13. // reverse Polish calculator - RPC
  14. main()
  15. {
  16.     int type;
  17.     double op2;
  18.     char s[MAXOP];
  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 'c':
  35.             push(cos((pop()*M_PI)/180.0));
  36.             break;
  37.             case 'i':
  38.             push(sin((pop()*M_PI)/180.0));
  39.             break;
  40.             case 'o':
  41.             break;
  42.             case 's':
  43.             break;
  44.             case 'n':
  45.             break;
  46.             case '/':
  47.             op2 = pop();
  48.             if (op2 != 0.0)
  49.                 push(pop() / op2);
  50.             else
  51.                 printf("error: zero divisor\n");
  52.             break;
  53.             case '\n':
  54.             printf("\t%.8g\n", pop());
  55.             break;
  56.             default:
  57.             printf("error: unknown command %s\n", s);
  58.             break;
  59.         }
  60.     }
  61. }
  62.  
  63.  
  64. int sp = 0;
  65. double val[MAXVAL];
  66.  
  67. void push(double f)
  68. {
  69.     if(sp < MAXVAL)
  70.         val[sp++]=f;
  71.     else
  72.         printf("error:stack full, cant push %g\n",f);
  73. }
  74.  
  75. double pop(void)
  76. {
  77.     if(sp>0)
  78.         return val[--sp];
  79.     else
  80.     {
  81.         printf("error: stack empty\n");
  82.         return 0.0;
  83.     }
  84. }
  85.  
  86.  
  87.  
  88. int getch(void);
  89. void ungetch(int);
  90.  
  91. int getop(char s[])
  92. {
  93.     int i,c;
  94.  
  95.     while((s[0] = c = getch()) == ' ' || c =='\t')
  96.         ;
  97.     s[1] = '\0';
  98.  
  99.     i = 0;
  100.     if(!isdigit(c) && c!='.' && c!='-')
  101.         return c;
  102.  
  103.     if(c=='-')
  104.         if(isdigit(c=getch()) || c == '.')
  105.             s[++i]=c;
  106.         else
  107.         {
  108.             if(c!=EOF)
  109.                 ungetch(c);
  110.             return '-';
  111.         }
  112.  
  113.     if(isdigit(c))
  114.         while(isdigit(s[++i] =c =getch()))
  115.             ;
  116.  
  117.     if(c=='.')
  118.         while(isdigit(s[++i] = c=getch()))
  119.             ;
  120.  
  121.     s[i] = '\0';
  122.     if(c!=EOF)
  123.         ungetch(c);
  124.     return NUMBER;
  125. }
  126.  
  127.  
  128.  
  129. char buf[BUFSIZE];
  130. int bufp = 0;
  131.  
  132. int getch(void)
  133. {
  134.     return (bufp > 0) ? buf[--bufp] : getchar();
  135. }
  136.  
  137. void ungetch(int c)
  138. {
  139.     if(bufp >= BUFSIZE)
  140.         printf("ungetch: too many characters\n");
  141.     else
  142.         buf[bufp++] = c;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement