MastaC729

CMPSC 122 Homework 3 evaluate.cpp

Feb 22nd, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.38 KB | None | 0 0
  1. // Simple Expression Evaluation
  2. // This program will evaluate simple arithmetic expressions
  3. // represented as a linked-list of tokens.  Keyboard input
  4. // will be accepted into a string, which will be converted
  5. // into that linked list.
  6. //
  7. // If the first symbol in the input string is an operator,
  8. // then the value of the previous expression will be taken
  9. // as an implicit first operand.
  10. //
  11. // The expressions may consist of the following:
  12. // -- integer values (which may have multiple digits)
  13. // -- simple arithmetic operators ( +, -, *, /, % )
  14. // -- matched parentheses for grouping
  15.  
  16. // This implementation consists of a set of mutually-recursive
  17. // functions. which will track the structure of the expression.
  18. //
  19. // A sum expression is the sum or difference of one or more products.
  20. // A product expression is the product or quotient of one or more factors.
  21. // A factor may be a number or a parenthesized sum expression.
  22.  
  23. #include <iostream>
  24. using namespace std;
  25. #include "tokenlist.h"
  26.  
  27. // Conversion to postfix function
  28. TokenList convertToPostfix(TokenList);
  29. void convertToPostfixSum(ListIterator &, TokenList, TokenList &, TokenList &);
  30. void convertToPostfixProduct(ListIterator &, TokenList, TokenList &, TokenList &);
  31. void convertToPostfixFactor(ListIterator &, TokenList, TokenList &, TokenList &);
  32.  
  33. int evaluatePostfix(TokenList);
  34.  
  35. // Evaluate
  36. // Begins to tokenize the string, and then recursively evaluates it.
  37. int evaluate(const char str[])
  38. {
  39.     TokenList list = TokenList(str);                    // Conversion from character array to TokenList
  40.    
  41.     cout << "Expression: " << (list) << endl;
  42.  
  43.     TokenList listPostfix = convertToPostfix(list);     // Conversion to postfix
  44.    
  45.     cout << "Expression in postfix: " << (listPostfix) << endl;
  46.  
  47.     return 0;
  48. }
  49.  
  50. // ConvertToPostfix
  51. // Converts the TokenList to a TokenList in the postfix notation
  52. TokenList convertToPostfix(TokenList t) {
  53.     TokenList newT = TokenList();
  54.     TokenList operStack = TokenList();
  55.     ListIterator iter = t.begin();          // Create iterator for list
  56.  
  57.     if (iter.currentIsInteger())
  58.         cout << iter.integerValue() << " " << "convertToPostFix" << endl;
  59.     else
  60.         cout << iter.tokenChar() << " " << "convertToPostFix" << endl;
  61.  
  62.     if (iter.currentIsInteger()){           // Sets the first token in the postfix list
  63.         newT.push_back(iter.integerValue());
  64.         iter.advance();
  65.         if (iter.currentIsInteger())
  66.             cout << iter.integerValue() << " " << "convertToPostFix" << endl;
  67.         else
  68.             cout << iter.tokenChar() << " " << "convertToPostFix" << endl;
  69.     }
  70.     else {                                  // Accounts for the first number being a negative (e.g. -4)
  71.         iter.advance();
  72.         if (iter.currentIsInteger())
  73.             cout << iter.integerValue() << " " << "convertToPostFix" << endl;
  74.         else
  75.             cout << iter.tokenChar() << " " << "convertToPostFix" << endl;;
  76.         newT.push_back(-iter.integerValue());
  77.         iter.advance();
  78.         if (iter.currentIsInteger())
  79.             cout << iter.integerValue() << " " << "convertToPostFix" << endl;
  80.         else
  81.             cout << iter.tokenChar() << " " << "convertToPostFix" << endl;
  82.     }
  83.  
  84.     convertToPostfixSum(iter, t, newT, operStack);  // Move on to recursion algorithm
  85.  
  86.     cout << "End of: " << "convertToPostFix" << endl;
  87.  
  88.     return newT;
  89. }
  90.  
  91. // ConvertToPostfixSum
  92. // Converts a sum expression: the sum or difference of one or more products
  93. // There may be the possibility of a leading - that would be implicitly
  94. // subtracting the first product from zero.
  95. void convertToPostfixSum(ListIterator &iter, TokenList t, TokenList &newT, TokenList &operStack)
  96. {
  97.     char oper = 0;                                  // possible operation
  98.     if (iter.tokenChar() == '-')                    // if negative
  99.     {
  100.         iter.advance();                             // go past the negative sign
  101.         if (iter.currentIsInteger())
  102.             cout << iter.integerValue() << " " << "convertToPostFixSum" << endl;
  103.         else
  104.             cout << iter.tokenChar() << " " << "convertToPostFixSum" << endl;
  105.         newT.push_front(-iter.integerValue());      // and change the token to a negative integer
  106.     }
  107.  
  108.     oper = iter.tokenChar();
  109.     while (oper == '+' || oper == '-')
  110.     {
  111.         if (operStack.empty()) {
  112.             operStack.push_back(oper);
  113.         }
  114.         else {
  115.             operStack.push_front(oper);
  116.         }
  117.        
  118.         iter.advance();     // get past the operator
  119.         if (iter.currentIsInteger())
  120.             cout << iter.integerValue() << " " << "convertToPostFixSum" << endl;
  121.         else
  122.             cout << iter.tokenChar() << " " << "convertToPostFixSum" << endl;
  123.         convertToPostfixProduct(iter, t, newT, operStack);
  124.         oper = iter.tokenChar();
  125.     }
  126.     cout << "End of: " << "convertToPostFixSum" << endl;
  127. }
  128.  
  129.  
  130. // ConvertToPostfixProduct
  131. // Converts a product expression: the product or quotient of factors
  132. void convertToPostfixProduct(ListIterator &iter, TokenList t, TokenList &newT, TokenList &operStack)
  133. {
  134.     char oper = 0;      // possible operation
  135.  
  136.     convertToPostfixFactor(iter, t, newT, operStack);
  137.  
  138.     oper = iter.tokenChar();
  139.     while (oper == '*' || oper == '/' || oper == '%')
  140.     {
  141.         iter.advance(); // get past the operator
  142.         if (iter.currentIsInteger())
  143.             cout << iter.integerValue() << " " << "convertToPostFixProduct" << endl;
  144.         else
  145.             cout << iter.tokenChar() << " " << "convertToPostFixProduct" << endl;
  146.         if (oper == '*')        // multiplication
  147.             convertToPostfixFactor(iter, t, newT, operStack);
  148.         else if (oper == '/')       // division
  149.             convertToPostfixFactor(iter, t, newT, operStack);
  150.         else                // remainder
  151.             convertToPostfixFactor(iter, t, newT, operStack);
  152.         oper = iter.tokenChar();
  153.     }
  154.     cout << "End of: " << "convertToPostFixProduct" << endl; // It crashes somewhere after this line
  155. }
  156.  
  157. // ConvertToPostfixFactor
  158. // A factor may either be a single-digit number
  159. // or a parenthsized expression.
  160. void convertToPostfixFactor(ListIterator &iter, TokenList t, TokenList &newT, TokenList &operStack)
  161. {
  162.     if (iter.currentIsInteger())
  163.     {
  164.         newT.push_front(iter.integerValue());
  165.         iter.advance();
  166.     }
  167.     else
  168.     {
  169.         iter.advance();     // go past assumed (
  170.         if (iter.currentIsInteger())
  171.             cout << iter.integerValue() << " " << "convertToPostFixFactor" << endl;
  172.         else
  173.             cout << iter.tokenChar() << " " << "convertToPostFixFactor" << endl;
  174.         convertToPostfixSum(iter, t, newT, operStack);
  175.         iter.advance();     // go past assumed )
  176.         if (iter.currentIsInteger())
  177.             cout << iter.integerValue() << " " << "convertToPostFixFactor" << endl;
  178.         else
  179.             cout << iter.tokenChar() << " " << "convertToPostFixFactor" << endl;
  180.     }
  181.     cout << "End of: " << "convertToPostFixFactor" << endl;
  182. }
  183.  
  184. int evaluatePostfix(TokenList t) {
  185.  
  186.     return 0; // Temporary, ya dangus
  187. }
Advertisement
Add Comment
Please, Sign In to add comment