Hollowfires

Untitled

Apr 21st, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. Function: convertToPostfix
  2. Input parameter: infix as string
  3. Output return value: postfix as string
  4.  
  5. 1. Create an empty string to hold string our function will build, this is the post fix string.
  6. 2. Set a unary flag to true.
  7. 3. Create an empty stack of string.
  8. 4. Create an object of type CStringTokenizer to tokenize the infix string with the delimiters "()^+-/%* ", and set flag for including delimiter to true.
  9.  
  10. 5. while our CStringTokenizer object has more tokens:
  11. A. Create a token string holding the next token with any trailing whitespaces trimmed.
  12. B. if (token is equal to "") // do nothing
  13. C. else if:
  14. i. if (token is equal to "(":
  15. a. Push(token) onto the stack.
  16. ii. else if (token is equal ")":
  17. a. Create a string holding the operator character.
  18. b. while (!(operator = pop()) is equal to "("):
  19. 1. postfix string += " " + operator.
  20.  
  21. iii. else if (token is equal to "*" or token is equal to "^" or
  22. token is equal to "+" or token is equal to "-" or token is equal to "%" or token is equal to "/"):
  23. a. if (unary==true):
  24. 1. token = "u" + token;
  25. // a unary op always goes on
  26. // the stack without popping any other op
  27. 2. Push(token) onto the stack
  28. iv. else:
  29. a. Create an integer variable holding the operatorPrecedence(token)
  30. b. While (!stack.isEmpty() and !stack.peek() is equal to "(")
  31. and operatorPrecedence(stack.peek()) >= p):
  32. 1. Create a string holding the operator with the return value of stack.pop();
  33. 2. postfix string += " " + op;
  34.  
  35. c. Push(token) onto stack.
  36.  
  37. v. unary = true; // if an operator is after this one, ithas to be unary
  38. D. else // Else for if statement in 5B
  39. postfix += " |" + token + "|";
  40. unary = false; // any operator after an operand is binary
  41.  
  42.  
  43. 6. While (!stack.isEmpty()) {
  44. A.Create a string holding the operator with the return value of stack.pop();
  45. B. post fix string += " " + op;
  46.  
  47. 7. return postfix;
  48.  
  49. Function: operatorPrecedence
  50. Input parameter: operator of type string
  51. Output return value: integer holding precedence level
  52.  
  53. 1. if (operator is equal to "u-" or operator is equal to "u+"): return 3 //highest level operator
  54. 2. else if (operator is equal to "^"): return 2
  55. 3. else if (operator is equal to "*" or operator is equal to"/"
  56. or operator is equal to "%"): return 1
  57. 4. else if (operator is equal to "-" or operator is equal to "+"):
  58. return 0;
  59. 5. else {
  60. return 0;
  61.  
  62. Main function test driver:
  63.  
  64. 1. Prompt: "\nEnter an infix expression to convert: "
  65. 2. Read in a string input from keyboard (highly recommend using getline function).
  66. 3. Create string holding result of convertToPostfix(input).
  67. 4. Output the result to the terminal screen.
Advertisement
Add Comment
Please, Sign In to add comment