Advertisement
desukotem

Untitled

Apr 21st, 2019
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAXOP 100
  5. #define NUMBER '0'
  6.  
  7. int getop(char []);
  8. void push(double);
  9. double pop(void);
  10.  
  11. int main()
  12. {
  13.     int type;
  14.     double op2;
  15.     char s[MAXOP];
  16.    
  17.     while((type = getop(s)) != EOF)
  18.     {
  19.         switch(type)
  20.         {
  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("errore: divisione per zero\n");
  40.                 break;
  41.             case '\n':
  42.                 printf("\t%.8g\n",pop());
  43.                 break;
  44.             default:
  45.                 printf("errore: comando ignoto %s\n",s);
  46.                 break;
  47.         }
  48.     }
  49.     return 0;
  50. }
  51.  
  52. #define MAXVAL 100
  53.  
  54. int sp = 0;
  55. double val[MAXVAL];
  56.  
  57. void push(double f)
  58. {
  59.     if(sp < MAXVAL)
  60.         val[sp++] = f;
  61.     else
  62.         printf("errore; la pila e' piena, non si puo' inserire %g\n",f);
  63. }
  64.  
  65. double pop(void)
  66. {
  67.     if(sp > 0)
  68.         return val[--sp];
  69.     else
  70.     {
  71.         printf("errore: la pila e' vuota\n");
  72.         return 0.0;
  73.     }
  74. }
  75.  
  76. #include <ctype.h>
  77.  
  78. int getch(void);
  79. void ungetch(int);
  80.  
  81. int getop(char s[])
  82. {
  83.     int i,c;
  84.    
  85.     while((s[0] = c = getch()) == ' ' || c == '\t')
  86.         ;
  87.     s[1] = '\0';
  88.    
  89.     i = 0;
  90.    
  91.     if(!isdigit(c) && c != '.')
  92.             return c;
  93.    
  94.     if(isdigit(c))
  95.         while (isdigit(s[++i] = c = getch()))
  96.             ;
  97.     if(c == '.')
  98.         while (isdigit(s[++i] = c = getch()))
  99.             ;
  100.        
  101.     s[i] = '\0';
  102.    
  103.     if (c != EOF)
  104.         ungetch(c);
  105.    
  106.     return NUMBER;
  107. }
  108.  
  109. #define BUFSIZE 100
  110.  
  111. char buf[BUFSIZE];
  112. int bufp = 0;
  113.  
  114. int getch(void)
  115. {
  116.     return (bufp > 0) ? buf[--bufp] : getchar();
  117. }
  118.  
  119. void ungetch(int c)
  120. {
  121.     if(bufp >= BUFSIZE)
  122.         printf("ungetch: troppi caratteri\n");
  123.     else
  124.         buf[bufp++] = c;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement