Advertisement
Vanya_Shestakov

Untitled

Nov 9th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Stack;
  7.  
  8. public class PolishExpression {
  9.  
  10. private final static Map<String, Integer> relativePriorities = Map.of
  11. ("+", 1, "-", 1, "*", 3, "/", 3, "^", 6, "operand", 7, "(", 9, ")", 0);
  12.  
  13. private final static Map<String, Integer> stackPriorities = Map.of
  14. ("+", 2, "-", 2, "*", 4, "/", 4, "^", 5, "operand", 8, "(", 0);
  15.  
  16. private final String expression;
  17.  
  18. private final int rank;
  19.  
  20. private PolishExpression(String expression) {
  21. this.expression = expression;
  22. this.rank = countRank(expression);
  23. }
  24.  
  25. public int getRank() {
  26. return rank;
  27. }
  28.  
  29. @Override
  30. public String toString() {
  31. return expression;
  32. }
  33.  
  34. public static PolishExpression getInstance(String expression) {
  35. System.out.println("SYMBOL STACK RESULT");
  36. List<String> symbols = getSymbols(expression);
  37. Stack<String> stack = new Stack<>();
  38. StringBuilder res = new StringBuilder();
  39. for (String symbol : symbols) {
  40. boolean deeper = true;
  41. int relativePriority = getPriority(symbol, relativePriorities);
  42. printState(symbol, stack, res.toString());
  43. if (!stack.isEmpty()) {
  44. while (deeper) {
  45. String stackSymbol = stack.peek();
  46. int stackPriority = getPriority(stackSymbol, stackPriorities);
  47. if (stackSymbol.equals("(") && symbol.equals(")")) {
  48. stack.pop();
  49. break;
  50. }
  51. if (relativePriority > stackPriority) {
  52. stack.push(symbol);
  53. deeper = false;
  54. } else {
  55. res.append(stack.pop());
  56. deeper = true;
  57. }
  58. if (stack.isEmpty()) {
  59. stack.push(symbol);
  60. deeper = false;
  61. }
  62. }
  63. } else {
  64. stack.push(symbol);
  65. }
  66. }
  67.  
  68. while (!stack.isEmpty()) {
  69. res.append(stack.pop());
  70. }
  71. return new PolishExpression(res.toString());
  72. }
  73.  
  74. private static void printState(String symbol, Stack<String> stack, String res) {
  75. String stackStr = stack.toString().replaceAll("\\s|,|\\[|\\]", "");
  76. System.out.printf("%2s", symbol);
  77. System.out.printf("%15s", stackStr);
  78. System.out.printf("%20s", res);
  79. System.out.println();
  80. }
  81.  
  82. private int countRank(String expression) {
  83. List<String> symbols = getSymbols(expression);
  84. int rank = 0;
  85. for (String symbol : symbols) {
  86. if (symbol.matches("^\\w$")) {
  87. rank += 1;
  88. } else {
  89. rank -= 1;
  90. }
  91. }
  92. return rank;
  93. }
  94.  
  95. private static List<String> getSymbols(String expression) {
  96. List<String> symbols = new ArrayList<>();
  97. for (int i = 0; i < expression.length(); i++) {
  98. symbols.add(String.valueOf(expression.charAt(i)));
  99. }
  100. return symbols;
  101. }
  102.  
  103. private static int getPriority(String symbol, Map<String, Integer> priorities) {
  104. if (symbol.matches("^\\w$")) {
  105. return priorities.get("operand");
  106. } else {
  107. return priorities.get(symbol);
  108. }
  109. }
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement