我尝试使用两个堆栈(操作数堆栈和运算符堆栈)来计算表达式。现在我只想用加法和减法测试我的程序,但它一直返回 0,我猜它的数学运算不正确。请帮忙
public static void main(String[] args) {
int userChoice;
String expression;
int finalresult;
Scanner keyboard = new Scanner(System.in);
System.out.println(" 1. Keyboard");
System.out.println(" 2. From a file");
System.out.println();
System.out.print("Select (1), (2): ");
// Integer Stack that is used to store the integer operands
GenericStack<Integer> stackForOperands = new GenericStack<Integer>();
// Character Stack that is used to store the operators
GenericStack<Character> stackForOperators = new GenericStack<Character>();
userChoice = keyboard.nextInt();
switch (userChoice) {
case 1:
System.out.print("Please enter an expression seperated by space: ");
keyboard.nextLine();
expression = keyboard.nextLine();
String[] tokens = expression.split("\\s+");
for (int i = 0; i < tokens.length; i++) {
if (tokens[i].matches("-?\\d+")) {
stackForOperands.push(Integer.parseInt(tokens[i]));
} else {
if (tokens[i] == "+" || tokens[i] == "-") {// if the extracted item is a + or – operator
if (stackForOperators.isEmpty())
stackForOperators.push(tokens[i].charAt(0));
else {
if (tokens[i] == "*" || tokens[i] == "/")
stackForOperators.push(tokens[i].charAt(0));
//else
// int top = stackForOperators.getTop();
// while (top != -1) {
//oneOperatorProcess(stackForOperands, stackForOperators);
// top = top - 1;
// }
}
}// end of checking "+", "-"
}
}
oneOperatorProcess(stackForOperands, stackForOperators);
finalresult = stackForOperands.pop();
System.out.println(finalresult);
}
}
我执行操作的方法
public static void oneOperatorProcess(GenericStack val, GenericStack op) {
char operator = (char) op.getTop();
int a = (int) val.pop();
int b = (int) val.pop();
int result = 0;
switch (operator) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
default:
//return 0;
}
val.push((int) result);
//return result;
最佳答案
Run the app. Type some stuff in. Check the result. Repeat.
这是一种非常缓慢的开发方式!这也很难做到,因为测试用例的数量变得越来越大,除非您每次都手动尝试所有内容,否则您将不知道是否破坏了以前的工作用例。
单元测试来救援
你需要研究一下单元测试,这里我使用了JUnit一次添加一个测试,慢慢构建一个可以轻松运行的完整测试工具。阅读TDD了解更多信息。
import static org.junit.Assert.*;
import org.junit.Test;
public class ExpressionTests {
@Test
public void oneNumber() {
assertEquals(123, new ExpressionEval().evaluate("123"));
}
@Test
public void addTwoNumbers() {
assertEquals(3, new ExpressionEval().evaluate("1 + 2"));
}
@Test
public void subtractTwoNumbers() {
assertEquals(2, new ExpressionEval().evaluate("5 - 3"));
}
@Test
public void addThreeNumbers() {
assertEquals(8, new ExpressionEval().evaluate("1 + 2 + 5"));
}
}
该类如下所示:
字符串比较时出现数字或错误,例如 ==
。请参阅How do I compare strings in Java?
此外,没有 while
循环意味着您只能管理一个简单的总和,并且根本无法处理任何运算符。
private class ExpressionEval {
private final Stack<Integer> stackForOperands = new Stack<Integer>();
private final Stack<Character> stackForOperators = new Stack<Character>();
public int evaluate(String expression) {
String[] tokens = expression.split("\\s+");
for (String token : tokens) {
if (token.matches("-?\\d+")) {
stackForOperands.push(Integer.parseInt(token));
} else {
if ("+".equals(token) || "-".equals(token)) {
stackForOperators.push(token.charAt(0));
}
}
}
while (!stackForOperators.isEmpty())
oneOperatorProcess();
return stackForOperands.pop();
}
private void oneOperatorProcess() {
char operator = stackForOperators.pop();
int b = stackForOperands.pop();
int a = stackForOperands.pop();
int result = 0;
switch (operator) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
//Add others only when you have a test case that needs it.
}
stackForOperands.push(result);
}
}
关于java - 使用两个堆栈进行表达式求值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28885858/