Hollowfires

Untitled

Apr 21st, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. #include "CStrTokenizer.h"
  2. #include "Stack.cpp"
  3. int operatorPrecedence(string); //function prototypes
  4. string convertToPostfix(string);
  5.  
  6. /********
  7. Convert an infix expression to reverse polish notation.
  8. * You do not have to have the program evaluate the reverse polish notation
  9. * Variable names should be able to be used in the expression
  10.  
  11. We want to make a new .cpp rather than adding to an old .cpp for reusability.
  12. I will be heavily documenting this code to better understand template classes (hopefully) this time around.
  13. ********/
  14.  
  15. //input string infix. Function that will convert infix to rpn
  16. string convertToPostfix(string infix)
  17. {
  18. string postfix; //output string. we're going to keep adding to this as the program goes on. Eventually returning this.
  19. bool unary = true; //a unary flag set to true. Otherwise the compiler will say 'huh'
  20.  
  21. //create an empty stack of string. create the stack itself that is a string.
  22. Stack<string> *newStack = new Stack<string>();
  23.  
  24. CStrTokenizer cs = CStrTokenizer(infix, "()^+-/%* ", true); //creating an object of type CStringTokenizer
  25. //used to tokenize the infix string^^ with the delimiters "()^+-/%* " (space included) and set flag for including delimiter to true.
  26. //token is holding the value. Run some checks using loops to determine what it is and what to do with it.
  27.  
  28. //5. while our CStringTokenizer object has more tokens. Checking if there are more tokens.
  29. while (cs.hasMoreTokens() )
  30. {
  31. //string token;
  32. //token.getNextToken();
  33. //The code above does not make sense because the string class does not have a getNextToken function
  34. //our new object "cs" has access to the getNextToken function.
  35. string token = cs.getNextToken(); //create a token string holding the next token with any trailing whitespaces trimmed
  36.  
  37. //if token is equal to "" do nothing. Is this supposed to be a space?
  38. if (token == "")
  39. {
  40. //do nothing
  41. }
  42. else
  43. {
  44. //postfix += (" |" + token + "|");
  45. //any operator after an operand is binary, not unary
  46. unary = false;
  47. }
  48.  
  49.  
  50. //C.
  51. //if token is a ( then push the token onto the stack
  52. if (token == "(")
  53. {
  54. newStack->push(token);
  55. }
  56.  
  57. //ii. if token is a ) then create a string holding the operator character
  58. else if (token == ")")
  59. {
  60. //a. string to hold the operator
  61. string op;
  62. //while operator popped is not equal to "(" perform the operation to postfix
  63. while ((op = newStack->pop()) != "(")
  64. {
  65. postfix += (" " + op);
  66. }
  67. }
  68. //C.iii
  69. else if (token == "*" || token == "^" || token == "+" || token == "-" || token == "%" ||token == "/" )
  70. {
  71. if (unary == true)
  72. {
  73. token = "u" + token;
  74. newStack->push(token);
  75. }
  76. }
  77.  
  78. //iv else
  79. else
  80. {
  81. //create an integer variable, p, holding the operatorPrecedence(token).
  82. //but you can't convert strings to ints directly.
  83. int p = operatorPrecedence(token);
  84. //p is the operator precendence of an operator
  85.  
  86. //while the stack isn't empty, while the peek is not "(" and while operator precedence value is greater than or equal to p
  87. while ((!(newStack->isEmpty()) && (newStack->peek() != "(")) && (operatorPrecedence(newStack->peek()) >= p))
  88. {
  89. //create a string holding the operator with the return value of stack.pop();
  90. //Is this correct???
  91. string ops = newStack->pop();
  92. postfix += " " + ops;
  93. } newStack->push(token);
  94. //push token onto stack
  95.  
  96. //v. if an operator is after this one, it has to be unary.
  97.  
  98. }
  99. unary = true;
  100. }
  101.  
  102. //6. while the newStack isn't empty
  103. while (!newStack->isEmpty())
  104. {
  105. //create a string holding the operator with the return value of stack.pop();
  106. string opers = newStack->pop();
  107. //
  108. postfix += " " + opers;
  109. }
  110.  
  111. return postfix;
  112. }
  113.  
  114. //operatorPrecedence function
  115. int operatorPrecedence(string token)
  116. {
  117. if (token == "u-" || token == "u+")
  118. {
  119. //highest level operator precedence
  120. return 3;
  121. }
  122. else if(token == "^")
  123. {
  124. //if the operator is exponent, it's second highest precedence.
  125. return 2;
  126. }
  127. else if(token == "*"||token== "/"||token== "%")
  128. {
  129. //if the operator is multiply, divide, or modulus, it's third highest precedence.
  130. return 1;
  131. }
  132. else if(token == "-" || token == "+")
  133. {
  134. //if the operator is + or -, it's lowest highest precedence.
  135. return 0;
  136. }
  137. else
  138. {
  139. //if none of the operands seem to math those above, return 0 anyway. No precedence.
  140. return 0;
  141. }
  142.  
  143. }
  144.  
  145.  
  146. int main()
  147. {
  148. string str; //new variable to hold a string
  149. cout << "\n Enter an infix expression to convert: "; //prompt the user for a string.
  150. getline (cin, str); //string that the user inputs
  151. string result = convertToPostfix(str); //put the string into the convertToPostFix function to get the result.
  152. cout << "\nThis is your result" << result << endl;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment