以下这段代码是我在进行四则运算程序编写中所学到的处理四则运算判断的方法,内容如下
import java.util.Stack;
public class ArithmeticExpressionEvaluator {
public static void main(String[] args) {
String expression = "11 / (5 * 2)"; // 替换为你的题目字符串
double result = evaluateArithmeticExpression(expression);
System.out.println("表达式计算结果: " + result);
}
public static double evaluateArithmeticExpression(String expression) {
// 移除空格
expression = expression.replaceAll("\\s", "");
// 定义操作数栈和运算符栈
Stack<Double> operandStack = new Stack<>();
Stack<Character> operatorStack = new Stack<>();
// 定义运算符优先级
int[] precedence = new int[256];
precedence['+'] = 1;
precedence['-'] = 1;
precedence['*'] = 2;
precedence['/'] = 2;
for (int i = 0; i < expression.length(); i++) {
char ch = expression.charAt(i);
if (Character.isDigit(ch)) {
// 处理操作数
StringBuilder sb = new StringBuilder();
while (i < expression.length() && Character.isDigit(expression.charAt(i))) {
sb.append(expression.charAt(i));
i++;
}
i--;
double operand = Double.parseDouble(sb.toString());
operandStack.push(operand);
} else if (ch == '(') {
// 左括号入栈
operatorStack.push(ch);
} else if (ch == ')') {
// 右括号,弹出括号内的运算符进行计算
while (!operatorStack.isEmpty() && operatorStack.peek() != '(') {
evaluateTopOperator(operandStack, operatorStack);
}
if (!operatorStack.isEmpty() && operatorStack.peek() == '(') {
operatorStack.pop(); // 弹出左括号
} else {
throw new IllegalArgumentException("括号不匹配");
}
} else if (isOperator(ch)) {
// 处理运算符
while (!operatorStack.isEmpty() && operatorStack.peek() != '(' &&
precedence[ch] <= precedence[operatorStack.peek()]) {
evaluateTopOperator(operandStack, operatorStack);
}
operatorStack.push(ch);
} else {
throw new IllegalArgumentException("无效的字符: " + ch);
}
}
// 处理剩余的运算符
while (!operatorStack.isEmpty()) {
evaluateTopOperator(operandStack, operatorStack);
}
// 返回最终结果
if (operandStack.size() == 1 && operatorStack.isEmpty()) {
return operandStack.pop();
} else {
throw new IllegalArgumentException("无效的表达式");
}
}
private static boolean isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}
private static void evaluateTopOperator(Stack<Double> operandStack, Stack<Character> operatorStack) {
if (operandStack.size() < 2 || operatorStack.isEmpty()) {
throw new IllegalArgumentException("无效的表达式");
}
double operand2 = operandStack.pop();
double operand1 = operandStack.pop();
char operator = operatorStack.pop();
double result = performOperation(operand1, operator, operand2);
operandStack.push(result);
}
private static double performOperation(double operand1, char operator, double operand2) {
switch (operator) {
case '+':
return operand1 + operand2;
case '-':
return operand1 - operand2;
case '*':
return operand1 * operand2;
case '/':
if (operand2 != 0) {
return operand1 / operand2;
} else {
throw new ArithmeticException("除零错误");
}
default:
throw new IllegalArgumentException("无效的操作符");
}
}
}
这段代码是一个简单的算术表达式求值程序,使用栈来处理操作数和运算符。它遵循标准的算术优先级规则,即乘法和除法优先于加法和减法。
程序的工作流程如下:
从左到右遍历输入的算术表达式。
如果字符是数字,则将其解析为一个double类型的操作数,并将其压入操作数栈。
如果字符是左括号,则将其压入运算符栈。
如果字符是右括号,则从运算符栈中弹出运算符,并计算其对应的操作数,直到遇到左括号为止。
如果字符是运算符(加号、减号、乘号或除号),则从运算符栈中弹出所有优先级高于或等于当前运算符的运算符,并计算其对应的操作数。然后将当前运算符压入运算符栈。
如果字符既不是数字、括号也不是运算符,则抛出异常。
最终,当遍历完整个表达式后,操作数栈中应该只剩下一个元素,即表达式的计算结果。
代码中的evaluateTopOperator方法用于计算栈顶运算符对应的操作数。它首先从操作数栈中弹出两个操作数,然后根据栈顶运算符进行相应的计算,并将结果压入操作数栈。