我尝试使用两个堆栈(操作数堆栈和运算符堆栈)来计算表达式。现在我只想用加法和减法测试我的程序,但它保持返回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;
答案 0 :(得分:1)
运行该应用。输入一些内容。检查结果。重复。
是一种非常缓慢的发展方式!随着测试用例数量的增加,你也很难做到,除非你每次手动尝试一切,否则你不会知道你是否已经破坏了之前的工作案例。
你需要研究单元测试,在这里我使用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);
}
}
答案 1 :(得分:0)
你的逻辑有点搞砸了。例如:
if (tokens[i] == "+" || tokens[i] == "-") {// if the extracted item is a + or – operator
if (stackForOperators.isEmpty())
stackForOperators.push(tokens[i].charAt(0));
else {
//this will always eval to false because you can only get here if tokens[i] is + or -
if (tokens[i] == "*" || tokens[i] == "/")
stackForOperators.push(tokens[i].charAt(0));
}
}
另外,正如@BartKiers在评论中提到的,请tokens[i].equals(...)
比较你的字符串。