
1.表达式

2.前缀表达式

3.中缀表达式

4.后缀表达式

5.中缀表达式转后缀表达式

6.中缀表达式转前缀表达式

7.中缀表达式转前缀表达式、后缀表达式代码实现
class UtilClass {
/*将字符串表达式,拆分成操作数、运算符数组
* 1.从左到右扫描expression
* 2.遇到运算符及括号直接add入数组中
* 3.遇到操作数就循环判断直到获取完整整数*/
public static ArrayList<String> str2arr(String expression) {
int strsize = expression.length();
ArrayList<String> arr = new ArrayList<>();
for (int i = 0; i < strsize; i++) {
String str = expression.charAt(i) + "";
boolean isnum = str.matches("\\d");
if (isnum) {
String num = "";
while (true) {
str = expression.charAt(i) + "";
if (i == strsize - 1) {
isnum = false;
} else {
isnum = (expression.charAt(i + 1) + "").matches("\\d");
}
num = num + str;
if (isnum != true) {
break;
}
i++;
}
arr.add(num);
} else {
arr.add(str);
}
}
return arr;
}
/*将中缀表达式,转换为后缀表达式*/
public static ArrayList<String> infix2suffix(String expression) {
ArrayList<String> arr = str2arr(expression);
Stack<String> s1 = new Stack();//数字栈
Stack<String> s2 = new Stack();//运算符栈
for (int i = 0; i < arr.size(); i++) {
String x = arr.get(i);
boolean isnum = x.matches("\\d+");
boolean isright = x.matches("\\)");
boolean isaddred = x.matches("[\\+\\-]");
boolean ismuldiv = x.matches("[\\*\\/\\%]");
if (isnum) {
s1.push(x);
} else if (s2.empty()) {
s2.push(x);
} else if (isright) {
while (true) {
s1.push(s2.pop());
if (s2.peek().matches("\\(")) {
break;
}
}
s2.pop();//弹出( 左括号
} else if ((isaddred && s2.peek().matches("\\(") != true)) {
while (true) {
s1.push(s2.pop());
if (s2.empty() || s2.peek().matches("\\(")) {
break;
}
}
s2.push(x);
} else if ((ismuldiv && s2.peek().matches("[\\*\\/\\%]"))) {
while (true) {
s1.push(s2.pop());
if (s2.empty() || s2.peek().matches("[\\*\\/\\%]") != true) {
break;
}
}
s2.push(x);
} else {
s2.push(x);
}
}
while (true) {
s1.push(s2.pop());
if (s2.empty()) {
break;
}
}
String[] arr1 = new String[s1.size()];
int index = s1.size() - 1;
while (s1.empty() != true) {
arr1[index--] = s1.pop();
}
return new ArrayList(Arrays.asList(arr1));
}
/*将中缀表达式,转换为前缀表达式*/
public static ArrayList<String> infix2prefix(String expression) {
ArrayList<String> arr = str2arr(expression);
Stack<String> s1 = new Stack();//数字栈
Stack<String> s2 = new Stack();//运算符栈
for (int i = arr.size() - 1; i >= 0; i--) {
String x = arr.get(i);
boolean isnum = x.matches("\\d+");
boolean isleft = x.matches("\\(");
boolean isaddred = x.matches("[\\+\\-]");
if (isnum) {
s1.push(x);
} else if (s2.empty()) {
s2.push(x);
} else if (isleft) {
while (true) {
s1.push(s2.pop());
if (s2.peek().matches("\\)")) {
break;
}
}
s2.pop();//弹出)右括号
} else if (isaddred && s2.peek().matches("[\\*\\/\\%]")) {
while (true) {
s1.push(s2.pop());
if (s2.empty() || s2.peek().matches("[\\*\\/\\%]") != true) {
break;
}
}
s2.push(x);
} else {
s2.push(x);
}
}
while (true) {
s1.push(s2.pop());
if (s2.empty()) {
break;
}
}
ArrayList<String> arr1 = new ArrayList<>();
while (s1.empty() != true) {
arr1.add(s1.pop());
}
return arr1;
}
}
8.中缀表达式实现计算结果
class InfixCalculator {
public static Object getResult(String expression) {
ArrayList arr = UtilClass.str2arr(expression);
Stack<Integer> numstack = new Stack<>();
Stack<String> symstack = new Stack<>();
int s1pop1;
int s1pop2;
String s2pop;
for (int i = 0; i < arr.size(); i++) {
String x = (String) arr.get(i);
boolean isnum = x.matches("\\d+");
boolean isright = x.matches("\\)");
boolean isaddred = x.matches("[\\+\\-]");
boolean ismuldiv = x.matches("[\\*\\/\\%]");
if (isnum) {
numstack.push(Integer.parseInt(x));
} else if (symstack.empty()) {
symstack.push(x);
} else if (isright) {
while (true) {
s1pop1 = numstack.pop();
s1pop2 = numstack.pop();
s2pop = symstack.pop();
switch (s2pop) {
case "+":
numstack.push(s1pop2 + s1pop1);
break;
case "-":
numstack.push(s1pop2 - s1pop1);
break;
case "*":
numstack.push(s1pop2 * s1pop1);
break;
case "/":
numstack.push(s1pop2 / s1pop1);
break;
case "%":
numstack.push(s1pop2 % s1pop1);
break;
}
if (symstack.peek().matches("\\(")) {
break;
}
}
symstack.pop();
} else if (isaddred && symstack.peek().matches("\\(") != true) {
while (true) {
s1pop1 = numstack.pop();
s1pop2 = numstack.pop();
s2pop = symstack.pop();
switch (s2pop) {
case "*":
numstack.push(s1pop2 * s1pop1);
break;
case "/":
numstack.push(s1pop2 / s1pop1);
break;
case "%":
numstack.push(s1pop2 % s1pop1);
break;
case "+":
numstack.push(s1pop2 + s1pop1);
break;
case "-":
numstack.push(s1pop2 - s1pop1);
break;
}
if (symstack.empty() || symstack.peek().matches("\\(")) {
break;
}
}
symstack.push(x);
} else if (ismuldiv && symstack.peek().matches("[\\*\\/\\%]")) {
while (true) {
s1pop1 = numstack.pop();
s1pop2 = numstack.pop();
s2pop = symstack.pop();
switch (s2pop) {
case "*":
numstack.push(s1pop2 * s1pop1);
break;
case "/":
numstack.push(s1pop2 / s1pop1);
break;
case "%":
numstack.push(s1pop2 % s1pop1);
break;
}
if (symstack.empty() || symstack.peek().matches("[\\*\\/\\%]") != true) {
break;
}
}
symstack.push(x);
} else {
symstack.push(x);
}
}
while (symstack.empty() != true) {
s1pop1 = numstack.pop();
s1pop2 = numstack.pop();
s2pop = symstack.pop();
switch (s2pop) {
case "+":
numstack.push(s1pop2 + s1pop1);
break;
case "-":
numstack.push(s1pop2 - s1pop1);
break;
case "*":
numstack.push(s1pop2 * s1pop1);
break;
case "/":
numstack.push(s1pop2 / s1pop1);
break;
case "%":
numstack.push(s1pop2 % s1pop1);
break;
}
}
return numstack.pop();
}
}
9.前缀表达式实现计算结果
class PrefixCalculator {
public static Object getResult(String expression) {
ArrayList<String> arr = UtilClass.infix2prefix(expression);
Stack<Integer> numstack = new Stack<>();
for (int i = arr.size() - 1; i >= 0; i--) {
String x = arr.get(i);
boolean isnum = x.matches("\\d+");
if (isnum) {
numstack.push(Integer.parseInt(x));
} else {
int s1pop1 = numstack.pop();
int s1pop2 = numstack.pop();
switch (x) {
case "+":
numstack.push(s1pop1 + s1pop2);
break;
case "-":
numstack.push(s1pop1 - s1pop2);
break;
case "*":
numstack.push(s1pop1 * s1pop2);
break;
case "/":
numstack.push(s1pop1 / s1pop2);
break;
case "%":
numstack.push(s1pop1 % s1pop2);
break;
}
}
}
return numstack.pop();
}
}
10.后缀表达式实现计算结果
class SuffixCalculator {
public static Object getResult(String expression) {
ArrayList<String> arr = UtilClass.infix2suffix(expression);
Stack<Integer> numstack = new Stack<>();
for (int i = 0; i < arr.size(); i++) {
String x = arr.get(i);
boolean isnum = x.matches("\\d+");
if (isnum) {
numstack.push(Integer.parseInt(x));
} else {
int s1pop1 = numstack.pop();
int s1pop2 = numstack.pop();
switch (x) {
case "+":
numstack.push(s1pop2 + s1pop1);
break;
case "-":
numstack.push(s1pop2 - s1pop1);
break;
case "*":
numstack.push(s1pop2 * s1pop1);
break;
case "/":
numstack.push(s1pop2 / s1pop1);
break;
case "%":
numstack.push(s1pop2 % s1pop1);
break;
}
}
}
return numstack.pop();
}
}
11.测试
public class InfixCalculatorDemo {
public static void main(String[] args) {
String exp = "1-2+4+(2+4*3)*2/6+(5*2+1)";
System.out.println("打印结果:" + (1 - 2 + 4 + (2 + 4 * 3) * 2 / 6 + (5 * 2 + 1)));
System.out.println("中缀表达式结果:" + InfixCalculator.getResult(exp));
System.out.println("中缀2后缀:" + UtilClass.infix2suffix(exp));
System.out.println("中缀2前缀:" + UtilClass.infix2prefix(exp));
System.out.println("后缀表达式结果:" + SuffixCalculator.getResult(exp));
System.out.println("前缀表达式结果:" + PrefixCalculator.getResult(exp));
}
}
测试结果:
打印结果:18
中缀表达式结果:18
中缀2后缀:[1, 2, -, 4, +, 2, 4, 3, *, +, 2, *, 6, /, +, 5, 2, *, 1, +, +]
中缀2前缀:[+, +, +, -, 1, 2, 4, /, *, +, 2, *, 4, 3, 2, 6, +, *, 5, 2, 1]
后缀表达式结果:18
前缀表达式结果:18