lolamontes69

K+R Exercise4_11

Sep 7th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. #define MAXOP   100
  6. #define MAXVAL  100
  7. #define NUMBER  '0'
  8. #define MAXLINE 1000
  9.  
  10. int getop(char s[]);
  11. void push(double f);
  12. double pop(void);
  13. void getline(char s[], int lim);
  14.  
  15. int sp = 0;
  16. double val[MAXVAL];
  17.  
  18.  
  19. /* Reverse Polish calculator */
  20. main()
  21. {
  22.     int type;
  23.     double op2;
  24.     char s[MAXOP];
  25.  
  26.     while((type = getop(s)) != EOF) {
  27.         switch(type) {
  28.         case NUMBER:
  29.             push(atof(s));
  30.             break;
  31.         case '+':
  32.             push(pop() + pop());
  33.             break;
  34.         case '*':
  35.             push(pop() * pop());
  36.             break;
  37.         case '-':
  38.             op2 = pop();
  39.             push(pop() - op2);
  40.             break;
  41.         case '/':
  42.             op2 = pop();
  43.             if(op2!=0.0)
  44.                 push(pop()/op2);
  45.             else
  46.                 printf("error: zero divisor\n");
  47.             break;
  48.         case '\n':
  49.             printf("\t%.8g\n", pop());
  50.             break;
  51.         default:
  52.             printf("error: unknown command %s\n", s);
  53.             break;
  54.         }
  55.     }
  56.     return 0;
  57. }
  58.  
  59. /* push: push f onto value stack */
  60. void push(double f)
  61. {
  62.     if(sp < MAXVAL)
  63.         val[sp++] = f;
  64.     else
  65.         printf("error: stack full, cant push %g\n", f);
  66. }
  67.  
  68. /* pop: pop and return top value from stack */
  69. double pop(void)
  70. {
  71.     if(sp > 0)
  72.         return val[--sp];
  73.     else {
  74.         printf("error: stack empty\n");
  75.         return 0.0;
  76.     }
  77. }
  78.  
  79. /* getop: get next character or numeric operand */
  80. int getop(char s[])
  81. {
  82.     int i, c;
  83.     static int charstore = 0;
  84.  
  85.     if(charstore==0)     /* if no char in charstore getchar()  */
  86.         c = getchar();   /* note 0 would be '0' not 0, ascii.. */
  87.     else {
  88.         c = charstore;   /* else add it to c and reinitialize it */
  89.         charstore = 0;  
  90.     }
  91.     while((s[0] = c ) == ' ' || c == '\t')
  92.         c = getchar();
  93.     s[1] = '\0';
  94.     if(!isdigit(c) && c != '.')
  95.         return c;
  96.     i = 0;
  97.     if(isdigit(c))
  98.         while(isdigit(s[++i] = c = getchar()))
  99.             ;
  100.     if(c == '.')
  101.         while(isdigit(s[++i] = c = getchar()))
  102.             ;
  103.     s[i] = '\0';
  104.     if(c != EOF)        /* if c has got an extra char add it to charstore */
  105.         charstore = c;
  106.     return NUMBER;
  107. }
  108.  
  109. /* getline: read a line into s, return length */
  110. void getline(char s[], int lim)
  111. {
  112.     int c, i;
  113.  
  114.     for(i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
  115.         s[i]=c;
  116.     if(c=='\n')
  117.         s[i++]=c;
  118.     s[i]='\0';
  119. }
Advertisement
Add Comment
Please, Sign In to add comment