Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. /** @author
  2. * @file main.cpp */
  3.  
  4. #include <iostream>
  5. #include <stack>
  6. #include <string>
  7.  
  8. using namespace::std;
  9.  
  10. string infixToPostfix(string parExpression);
  11. double evaluate(string parExpression);
  12. int precedence(char parC);
  13.  
  14. int main()
  15. {
  16.     // Test the following cases
  17.     cout << evaluate(infixToPostfix("2-9")) << endl;
  18.     cout << evaluate(infixToPostfix("(3+2)*5")) << endl;
  19.     cout << evaluate(infixToPostfix("(1+7)*(5/2)")) << endl;
  20.     cout << evaluate(infixToPostfix("4+3*5/6")) << endl;
  21.     return 0;
  22. }
  23.  
  24.  
  25. string infixToPostfix(string parExpression)
  26. {
  27.     stack<char> theStack;
  28.     string postFixExpression = "";
  29.     for (unsigned int c = 0; c < parExpression.length(); c++)
  30.     {
  31.         switch (parExpression[c])
  32.         {
  33.         case '0':
  34.         case '1':
  35.         case '2':
  36.         case '3':
  37.         case '4':
  38.         case '5':
  39.         case '6':
  40.         case '7':
  41.         case '8':
  42.         case '9':
  43.             postFixExpression = postFixExpression + parExpression[c];
  44.             break;
  45.         case '(':
  46.             theStack.push(c);
  47.             break;
  48.         case '+':
  49.         case '-':
  50.         case '*':
  51.         case '/':
  52.             while ((!theStack.empty()) && (theStack.top() != '(') && (precedence(c) <= precedence(theStack.top())))
  53.             {
  54.                 postFixExpression = postFixExpression + theStack.top();
  55.                 theStack.pop();
  56.             }
  57.             theStack.push(c);
  58.             break;
  59.         case ')':
  60.             while (theStack.top() != '(')
  61.             {
  62.                 postFixExpression = postFixExpression + theStack.top();
  63.                 theStack.pop();
  64.             }
  65.             theStack.pop();
  66.             break;
  67.         }
  68.  
  69.     }
  70.  
  71.     while (!theStack.empty())
  72.     {
  73.         postFixExpression = postFixExpression + theStack.top();
  74.         theStack.pop();
  75.     }
  76.     return postFixExpression;
  77. }
  78.  
  79. double evaluate(string parExpression)
  80. {
  81.     stack<double> theStack;
  82.     double result = 0;
  83.     double operand1, operand2;
  84.     for (unsigned int x = 0; x < parExpression.length(); x++)
  85.     {
  86.         if (isdigit(x))
  87.         {
  88.             theStack.push(x);
  89.         }
  90.         else
  91.         {
  92.             operand2 = (double)theStack.top();
  93.             theStack.pop();
  94.             operand1 = (double)theStack.top();
  95.             theStack.pop();
  96.             switch (parExpression[x])
  97.             {
  98.             case '+':
  99.                 result = operand1 + operand2;
  100.                 theStack.push(result);
  101.                 break;
  102.             case '-':
  103.                 result = operand1 - operand2;
  104.                 theStack.push(result);
  105.                 break;
  106.             case '*':
  107.                 result = operand1 * operand2;
  108.                 theStack.push(result);
  109.                 break;
  110.             case '/':
  111.                 result = operand1 / operand2;
  112.                 theStack.push(result);
  113.                 break;
  114.             }
  115.  
  116.            
  117.         }
  118.     }
  119.     return theStack.top();
  120. }
  121.  
  122. int precedence(char parC)
  123. {
  124.     if ((parC == '*') || (parC == '/'))
  125.         return 1;
  126.     else
  127.         return 0;
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement