JAVA 字符表达式 解析

本文介绍了一个使用JAVA实现的基本表达式解析计算方法,该方法能够处理简单的不含括号的数学表达式。通过两个栈来分别存储操作数和操作符,并逐步计算得到最终结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

尝试着,用JAVA进行字符串表达式解析计算。

居然没有得出个更好的写法,甚是郁闷。

鼓捣出下面一个类,只能求解不带括弧的表达式,看来数据结构中的关于表达式的算法,当初是没好好实现,到现在也没整出个比较好的方案。

package test.java;

import java.util.Stack;

public class MathUtil {

public static final char add = '+';
public static final char substract = '-';
public static final char divide = '/';
public static final char multiply = '*';

public static final char leftParenthesis = '(';
public static final char rightParenthesis = ')';

public static final int isParenthesis = 2;

public static final int isOperator = 1;

public static final int isNotOperator = 0;

public static String exec(String expression) {
Stack<String> operatorStack = new Stack<String>();
Stack<String> operandStack = new Stack<String>();
int i = 0;

StringBuffer operandBuffer = new StringBuffer();
while (i < expression.length()) {
char ch = expression.charAt(i);
int result = checkOperator(ch);

if (Character.isDigit(ch)) {
operandBuffer.append(ch);
// 运算数入栈
if (i + 1 == expression.length()) {
operandStack.push(operandBuffer.toString());
operandBuffer.delete(0, operandBuffer.length());
}

} else if (result == isOperator) {
// 运算数入栈
if (operandBuffer.length() > 0) {
operandStack.push(operandBuffer.toString());
operandBuffer.delete(0, operandBuffer.length());
}

processStack(operatorStack, operandStack);

// 运算符入栈
operatorStack.push(Character.toString(ch));

} else if (result == isParenthesis) {
// 运算数入栈
if (operandBuffer.length() > 0) {
operandStack.push(operandBuffer.toString());
operandBuffer.delete(0, operandBuffer.length());
}
// 括弧入栈
operatorStack.push(Character.toString(ch));
}

i++;
}

// 末尾栈处理
processStack(operatorStack, operandStack);

// 出空栈
while (!operatorStack.isEmpty()) {
processEndStack(operatorStack, operandStack);
}
return operandStack.pop();
}

public static void processEndStack(Stack<String> operatorStack,
Stack<String> operandStack) {

if (operatorStack.isEmpty())
return;

char operator = operatorStack.pop().charAt(0);
int opearand1 = Integer.parseInt(operandStack.pop());
int opearand2 = Integer.parseInt(operandStack.pop());
String previewOperator = "@";
if (!operatorStack.isEmpty()) {
previewOperator = operatorStack.pop();
}
if (previewOperator.charAt(0) == MathUtil.substract) {
opearand2 = -opearand2;
operatorStack.push(Character.toString(MathUtil.add));
} else if (!previewOperator.equals("@")) {
operatorStack.push(previewOperator);
}
if (operator == MathUtil.add) {
opearand1 = opearand2 + opearand1;

} else if (operator == MathUtil.substract) {

opearand1 = opearand2 - opearand1;
} else {
// 恢复栈
operandStack.push(Integer.toString(opearand2));
operatorStack.push(Character.toString(operator));
}
// 结果入栈
operandStack.push(Integer.toString(opearand1));
}

public static void processStack(Stack<String> operatorStack,
Stack<String> operandStack) {
if (operatorStack.isEmpty())
return;

char operator = operatorStack.pop().charAt(0);
int opearand1 = Integer.parseInt(operandStack.pop());
int opearand2 = Integer.parseInt(operandStack.pop());
if (operator == MathUtil.divide) {
opearand1 = opearand2 / opearand1;

} else if (operator == MathUtil.multiply) {
opearand1 = opearand1 * opearand2;

} else {
// 恢复栈
operandStack.push(Integer.toString(opearand2));
operatorStack.push(Character.toString(operator));
}
// 结果入栈
operandStack.push(Integer.toString(opearand1));

}

public static int checkOperator(char operator) {

if (MathUtil.add == operator) {
return MathUtil.isOperator;
} else if (MathUtil.substract == operator) {
return MathUtil.isOperator;
} else if (MathUtil.divide == operator) {
return MathUtil.isOperator;
} else if (MathUtil.multiply == operator) {
return MathUtil.isOperator;
} else if (MathUtil.leftParenthesis == operator
|| MathUtil.rightParenthesis == operator) {
return MathUtil.isParenthesis;
} else {
return MathUtil.isNotOperator;
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值