Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <ctype.h>
  5.  
  6.  
  7. typedef double number_t;
  8. typedef char op_t;
  9.  
  10. void push (number_t);
  11. number_t pop ();
  12. bool empty ();
  13.  
  14. bool is_operator (char);
  15. bool is_number (char);
  16.  
  17. number_t apply_operator (op_t, number_t, number_t);
  18. number_t parse_number ();
  19. void trim_left ();
  20.  
  21. void read_buffer ();
  22. void evaluate_buffer ();
  23.  
  24.  
  25. // --
  26.  
  27.  
  28. #define STACK_SIZE 256
  29. #define BUFFER_SIZE 512
  30.  
  31.  
  32. char buffer[BUFFER_SIZE + 1];
  33. number_t stack[STACK_SIZE];
  34.  
  35. int sp = 0, bp = 0;
  36.  
  37.  
  38. void push (number_t num)
  39. {
  40.     if (sp >= STACK_SIZE)
  41.     {
  42.         // stack overflow!!!
  43.         return;
  44.     }
  45.     stack[sp++] = num;
  46. }
  47. number_t pop ()
  48. {
  49.     if (empty())
  50.     {
  51.         // stack underflow!!
  52.         return 0;
  53.     }
  54.     return stack[--sp];
  55. }
  56. bool empty ()
  57. {
  58.     return sp == 0;
  59. }
  60.  
  61. bool is_operator (char c)
  62. {
  63.     return c == '+' ||
  64.             c == '-' ||
  65.             c == '*' ||
  66.             c == '/';
  67. }
  68. bool is_number (char c)
  69. {
  70.     return c == '.' || isdigit(c);
  71. }
  72.  
  73.  
  74.  
  75. number_t apply_operator (op_t op, number_t a, number_t b)
  76. {
  77.     switch (op)
  78.     {
  79.         case '+': return a + b;
  80.         case '-': return a - b;
  81.         case '*': return a * b;
  82.         case '/': return a / b;
  83.         default:
  84.             return 0; // this case likely won't be reached
  85.     }
  86. }
  87. number_t parse_number ()
  88. {
  89.     number_t num = 0,
  90.         mag = .1;
  91.    
  92.     for (; isdigit(buffer[bp]); bp++)
  93.         num = (num * 10) + (buffer[bp] - '0');
  94.    
  95.     if (buffer[bp] == '.')
  96.         for (bp++; isdigit(buffer[bp]); bp++)
  97.         {
  98.             num += (buffer[bp] - '0') * mag;
  99.             mag *= .1; // this is probably inefficient :|
  100.         }
  101.    
  102.     return num;
  103. }
  104. void trim_left ()
  105. {
  106.     while (isspace(buffer[bp]))
  107.         bp++;
  108. }
  109.  
  110.  
  111.  
  112. void read_buffer ()
  113. {
  114.     int c, i;
  115.    
  116.     for (i = 0; i < BUFFER_SIZE; i++)
  117.         if ((c = getchar()) == '\n')
  118.             break;
  119.         else
  120.             buffer[i] = c;
  121.    
  122.     buffer[i] = '\0';
  123.     bp = 0;
  124. }
  125. void evaluate_buffer ()
  126. {
  127.     sp = 0;
  128.    
  129.    
  130.     for (trim_left(); buffer[bp]; trim_left())
  131.     {
  132.         if (is_operator(buffer[bp]))
  133.         {
  134.             op_t op = buffer[bp++];
  135.             number_t b = pop();
  136.             number_t a = pop();
  137.            
  138.             push(apply_operator(op, a, b));
  139.         }
  140.         else if (is_number(buffer[bp]))
  141.             push(parse_number());
  142.         else
  143.         {
  144.             // syntax error!
  145.             break;
  146.         }
  147.     }
  148. }
  149.  
  150.  
  151. int main ()
  152. {
  153.     for (;;)
  154.     {
  155.         printf("> ");
  156.         read_buffer();
  157.         evaluate_buffer();
  158.         while (!empty())
  159.             printf("%.3f ", pop());
  160.         printf("\n");
  161.     }
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement