栈的三种表达式为:前缀,中缀,后缀
前缀表达式(波兰表达式)
前缀表达式的计算机求值
中缀表达式
后缀表达式 (逆波兰表达式)
后缀表达式的计算机求值
代码完成一个计算器(逆波兰计算器)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| package com.algorithm.stack;
import java.util.ArrayList; import java.util.List; import java.util.Stack;
public class BackPolandCalculatorDemo {
public static void main(String[] args) { String expre = "30 4 + 5 * 6 - ";
List<String> listString = getListString(expre); System.out.println(listString); int calculate = calculate(listString); System.out.println(calculate); }
public static List<String> getListString(String expre) { String[] strings = expre.split(" "); List<String> stringList = new ArrayList<>(); for (String string : strings) { stringList.add(string); } return stringList; }
public static int calculate(List<String> stringList){ Stack<String> stack = new Stack<>(); for (String item : stringList) { if (item.matches("\\d+")){ stack.push(item); }else { int num2 = Integer.parseInt(stack.pop()); int num1 = Integer.parseInt(stack.pop()); int res; if (item.equals("+")) { res = num1 + num2; } else if (item.equals("-")) { res = num1 - num2; } else if (item.equals("*")) { res = num1 * num2; } else if (item.equals("/")) { res = num1 / num2; } else { throw new RuntimeException("运算符有误"); } stack.push(String.valueOf(res)); }
} return Integer.parseInt(stack.pop()); }
}
|
中缀转后缀
大家看到,后缀表达式适合计算式进行运算,但是人却不太容易写出来,尤其是表达式很长的情况下,因此在开发中,我们需要将中缀表达式转成后缀表达式。
具体步骤如下:
- 初始化两个栈:运算符栈s1和储存中间结果的栈s2;
- 从左至右扫描中缀表达式;
- 遇到操作数时,将其压s2;
- 遇到运算符时,比较其与s1栈顶运算符的优先级:
- 如果s1为空,或栈顶运算符为左括号“(",则直接将此运算符入栈;
- 否则,若优先级比栈顶运算符的高,也将运算符压入s1;
- 否则,将s1栈顶的运算符弹出并压入到s2中,再次转到(4-1)与s1中新的栈顶运算符相比较;
- 遇到括号时:
- 如果是左括号“(”,则直接压入s1
- 如果是右括号“)”,则依次弹出s1栈顶的运算符,并压入s2,直到遇到左括号为止,此时将这一对括号丢弃
- 重复步骤2至5,直到表达式的最右边
- 将s1中剩余的运算符依次弹出并压入s2
- 依次弹出s2中的元素并输出,结果的逆序即为中缀表达式对应的后缀表达式
举例说明:
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| package com.algorithm.stack;
import java.util.ArrayList; import java.util.List; import java.util.Stack;
public class BackPolandCalculatorDemo {
public static void main(String[] args) {
String exp = "1+((2+3)*4)-5";
List<String> list = toInfixExpList(exp); System.out.println(list); List<String> list1 = parseSufExpList(list); System.out.println(list1);
int calculate = calculate(list1); System.out.println(calculate); }
public static List<String> parseSufExpList(List<String> list){ Stack<String> s1 = new Stack<>(); List<String> s2 = new ArrayList<>();
for (String item : list) { if (item.matches("\\d+")){ s2.add(item); }else if (item.equals("(")){ s1.push(item); }else if (item.equals(")")){ while (!s1.peek().equals("(")){ s2.add(s1.pop()); } s1.pop(); }else { while (s1.size() != 0 && Operation.getValue(s1.peek()) >= Operation.getValue(item)){ s2.add(s1.pop()); } s1.push(item); }
} while (s1.size() != 0){ s2.add(s1.pop()); } return s2; }
public static List<String> toInfixExpList(String str) { List<String> list = new ArrayList<>(); int i = 0; String st; char c; do { if ((c = str.charAt(i)) < 48 || (c = str.charAt(i)) > 57) { list.add(String.valueOf(c)); i++; } else { st = ""; while (i < str.length() && (c = str.charAt(i)) >= 48 && (c = str.charAt(i)) <= 57) { st += c; list.add(st); i++; } } } while (i < str.length()); return list; }
public static List<String> getListString(String expre) { String[] strings = expre.split(" "); List<String> stringList = new ArrayList<>(); for (String string : strings) { stringList.add(string); } return stringList; }
public static int calculate(List<String> stringList) { Stack<String> stack = new Stack<>(); for (String item : stringList) { if (item.matches("\\d+")) { stack.push(item); } else { int num2 = Integer.parseInt(stack.pop()); int num1 = Integer.parseInt(stack.pop()); int res; if (item.equals("+")) { res = num1 + num2; } else if (item.equals("-")) { res = num1 - num2; } else if (item.equals("*")) { res = num1 * num2; } else if (item.equals("/")) { res = num1 / num2; } else { throw new RuntimeException("运算符有误"); } stack.push(String.valueOf(res)); }
} return Integer.parseInt(stack.pop()); }
}
class Operation{ private static int ADD = 1; private static int SUB = 1; private static int MUL = 2; private static int DIV = 2;
public static int getValue(String oper){ int res = 0; switch (oper){ case "+": res = ADD; break; case "-": res = SUB; break; case "*": res = MUL; break; case "/": res = DIV; break; default: System.out.println("运算符有误"); break; } return res; } }
|