
本文深入探讨了在Java中使用栈实现后缀表达式求值时,将字符数字(如'3')错误地当作其ASCII值而非实际数值处理的常见陷阱。通过分析原始代码并提供修正后的实现,详细解释了如何正确地将字符型数字转换为其对应的数值,确保后缀表达式求值结果的准确性。文章还包含了完整的示例代码、表达式求值过程解析以及提升健壮性的注意事项。
后缀表达式(也称为逆波兰表示法)是一种没有括号的算术表达式表示方法,其中运算符位于其操作数的后面。例如,中缀表达式 (3 + 1 * 2) 对应的后缀表达式是 3 1 2 * +。使用栈数据结构实现后缀表达式求值是一种高效且常见的做法:
在实现后缀表达式求值时,一个常见的错误是将字符形式的数字(例如 char '3')直接当作其数值(int 3 或 float 3.0)进行处理。Java中,当一个 char 类型变量被强制转换为 float 或 int 时,它会转换为其对应的ASCII(或Unicode)值,而非其代表的数字本身。
考虑以下原始代码片段中的问题:
立即学习“Java免费学习笔记(深入)”;
// ... (其他代码)
public static float postFixEvaluation(String expr){
Stack<Float> stack = new Stack<>();
int index = 0;
while(index < expr.length()){
char token = expr.charAt(index);
boolean x = isOperand(token);
if(x == true){ // operand
float operandIn = (float)token; // 错误:这里将字符'3'转换为其ASCII值51.0
stack.push(operandIn);
}
// ... (其他代码)
}
// ...
}当输入表达式为 "312*+456*+97-/+" 时,第一个字符 '3' 被读取。在 if(x == true) 分支中,operandIn 会被赋值为 (float)'3',其结果是 51.0(字符 '3' 的ASCII值),而不是期望的 3.0。同理,'1' 会变成 49.0,'2' 会变成 50.0,依此类推。这就导致了后续所有计算基于错误的数值,最终得出 3958.0 这样远远偏离 22.0 的结果。
要解决这个问题,需要将字符数字正确地转换为其对应的数值。在Java中,对于单字符数字,最简洁的方法是减去字符 '0' 的ASCII值。由于数字字符 '0' 到 '9' 的ASCII值是连续排列的,'c' - '0' 就能得到字符 c 所代表的整数值。
例如:
因此,将 float operandIn = (float)token; 替换为 float operandIn = (float)(token - '0'); 即可修正此问题。
以下是修正并优化后的 PostFixEvaluate 类,包含了正确的字符到数值转换、基本的错误处理和更精确的操作数判断:
import java.util.Stack;
public class PostFixEvaluate {
/**
* 执行基本的算术运算。
* 注意:操作数顺序为 operand_2 运算符 operand_1。
*
* @param operand_1 第一个从栈中弹出的操作数(右操作数)。
* @param operator 运算符。
* @param operand_2 第二个从栈中弹出的操作数(左操作数)。
* @return 运算结果。
* @throws ArithmeticException 如果发生除零错误。
* @throws IllegalArgumentException 如果遇到无效运算符。
*/
public static float calculate(Float operand_1, char operator, Float operand_2) {
switch (operator) {
case '+':
return operand_2 + operand_1;
case '-':
return operand_2 - operand_1;
case '*':
return operand_2 * operand_1;
case '/':
if (operand_1 == 0) {
throw new ArithmeticException("Division by zero error.");
}
return operand_2 / operand_1;
default:
throw new IllegalArgumentException("Invalid operator: " + operator);
}
}
/**
* 判断一个字符是否为数字操作数。
*
* @param c 待判断的字符。
* @return 如果是数字字符 ('0'-'9') 则返回 true,否则返回 false。
*/
public static boolean isOperand(char c) {
return c >= '0' && c <= '9';
}
/**
* 对后缀表达式进行求值。
*
* @param expr 后缀表达式字符串。
* @return 表达式的计算结果。
* @throws IllegalArgumentException 如果表达式格式无效或操作数不足。
*/
public static float postFixEvaluation(String expr) {
Stack<Float> stack = new Stack<>();
int index = 0;
while (index < expr.length()) {
char token = expr.charAt(index);
if (isOperand(token)) { // 操作数
// 核心修正:将字符数字转换为其数值
float operandIn = (float)(token - '0');
stack.push(operandIn);
} else { // 运算符
if (stack.size() < 2) {
throw new IllegalArgumentException("Invalid postfix expression: insufficient operands for operator " + token);
}
float operand_1 = stack.pop(); // 第一个弹出的操作数是右操作数
float operand_2 = stack.pop(); // 第二个弹出的操作数是左操作数
stack.push(calculate(operand_1, token, operand_2));
}
index += 1;
}
// 表达式求值完成后,栈中应只剩一个结果
if (stack.size() != 1) {
throw new IllegalArgumentException("Invalid postfix expression: too many operands or too few operators.");
}
return stack.pop();
}
public static void main(String[] args) {
// 测试表达式:( (3 + (1 * 2)) + (4 + (5 * 6)) ) / (9 - 7)
// 转换为后缀表达式:312*+456*+97-/+
// 预期结果: ( (3 + 2) + (4 + 30) ) / 2 = (5 + 34) / 2 = 39 / 2 = 19.5
// 实际上,原问题中的预期结果22.0对应的是 (312*+ 456*+ + 97- /) + => ( (3+(1*2)) + (4+(5*6)) ) / (9-7) + => (5+34)/2 + (什么也没有)
// 根据后缀表达式的严格定义,"312*+456*+97-/+" 应该解析为:
// A = 312*+ => 3 + (1*2) = 5
// B = 456*+ => 以上就是Java中栈实现后缀表达式求值:字符与数值转换的常见陷阱与解决方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号