class text{
public static int mixingOperation(String infixExpression) {
System.out.println("原始中缀表达式为: " + infixExpression);
// 1. 处理中括号和小括号
String newInfixExpression = handlerParentheses(infixExpression);
System.out.println("处理括号后的新的中缀表达式为: " + newInfixExpression);
// 2. 中缀表达式转换为后缀表达式
String[] postfixExpressionArray = transformExpression(newInfixExpression);
StringBuffer postfixExpressionBuffer = new StringBuffer();
for (int i = 0; i < postfixExpressionArray.length; i++) {
if (null == postfixExpressionArray[i] || postfixExpressionArray[i].isEmpty()) {
break;
}
postfixExpressionBuffer.append(postfixExpressionArray[i]).append(" ");
}
System.out.println("后缀表达式为: " + postfixExpressionBuffer.toString());
// 后缀表达式求值
int result = calculateResultByPostfixExpression(postfixExpressionArray);
System.out.println("表达式: " + infixExpression + " = " + result);
return result;
}
private static int calculateResultByPostfixExpression(String[] postfixExpressionArray) {
// 运算符和运算数栈
Stack<String> calculateStack = new Stack<>();
// 遍历中缀表达式数组
for (int i = 0; i < postfixExpressionArray.length; i++) {
if (null == postfixExpressionArray[i] || postfixExpressionArray[i].isEmpty()) {
break;
}
String currentStr = postfixExpressionArra