Advertisement
kartikkukreja

Executing infix arithmetic expressions

Apr 27th, 2013
1,516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. /* Author : Kartik Kukreja
  2.  * Description :    Program to implement a calculator that reads in expressions from
  3.  *                  the console and writes out the results to the console.
  4.  *
  5.  *                  It requires all tokens to be separated by space. The only recognized
  6.  *                  tokens are (, ), +, -, /, * and integers.
  7.  *            
  8.  *                  Sample expressions :
  9.  *                  ( 10 + 11 ) * 12
  10.  *                  ( 10 * 12 ) + ( 12 * 2 )
  11.  */
  12.  
  13. #include <cstdio>
  14. #include <cstdlib>
  15. #include <cstring>
  16. #include <stack>
  17. #include <cctype>
  18. using namespace std;
  19.  
  20. bool error;
  21.  
  22. // returns true if op2 has lower or equal priority than op1, false otherwise
  23. bool hasLowerPriority(char op1, char op2)   {
  24.     switch (op1) {
  25.         case '(': return false;
  26.         case '-': return op2 == '-';
  27.         case '+': return op2 == '-' || op2 == '+';
  28.         case '*': return op2 != '/';
  29.         case '/': return true;
  30.         default : error = true; return false;
  31.     }
  32. }
  33.  
  34. stack < char > operators;
  35. stack < int > operands;
  36.  
  37. // perform the operation 'op' on the two operands on top of the stack
  38. void operation(char op) {
  39.     if(operands.size() < 2) {
  40.         error = true; return;
  41.     }
  42.     int op2 = operands.top(); operands.pop();
  43.     int op1 = operands.top(); operands.pop();
  44.     switch(op)  {
  45.         case '+': operands.push(op1 + op2); break;
  46.         case '-': operands.push(op1 - op2); break;
  47.         case '*': operands.push(op1 * op2); break;
  48.         case '/': operands.push(op1 / op2); break;
  49.         default : error = true; return;
  50.     }
  51. }
  52.  
  53. int main()  {
  54.     char exp[1000], *p;
  55.     int len;
  56.  
  57.     printf("\t\t\t\tCALCULATOR\n");
  58.     while(true) {
  59.         printf("\nEnter an expression (. to exit):\n");
  60.         scanf("%[^\n]%*c", exp);
  61.         if(exp[0] == '.') break;
  62.  
  63.         len = strlen(exp);
  64.         if(len == 0) { getchar(); continue; }
  65.         exp[len] = ' '; exp[len + 1] = ')'; exp[len + 2] = '\0';
  66.         error = false;
  67.         operators.push('(');
  68.  
  69.         p = strtok(exp, " ");
  70.         while(p && !error)    {
  71.             if(isdigit(p[0]))
  72.                 operands.push(atoi(p));
  73.             else switch(p[0])    {
  74.                 case '(' :  operators.push('('); break;
  75.                 case ')' :  while (!operators.empty() && !error && operators.top() != '(') {
  76.                 operation(operators.top()); operators.pop();
  77.                 }
  78.                 if (!operators.empty())
  79.                 operators.pop();
  80.                 else
  81.                 error = true;
  82.                 break;
  83.                 default  :  while(!operators.empty() && !error && hasLowerPriority(operators.top(), p[0])) {
  84.                                 operation(operators.top()); operators.pop();
  85.                             }
  86.                             operators.push(p[0]);
  87.             }
  88.             p = strtok(NULL, " ");
  89.         }
  90.         if(error || !operators.empty() || operands.size() != 1) {
  91.             printf("ERROR\n");
  92.             while(!operands.empty())
  93.                 operands.pop();
  94.             while(!operators.empty())
  95.                 operators.pop();
  96.         } else    {
  97.             printf("%d\n", operands.top()); operands.pop();
  98.         }
  99.     }
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement