AJTAMjid2000

Untitled

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