Java RPN计算器空指针问题
嗨,我已经设法解决了空指针问题 现在我几乎让代码工作了,除了在计算时,我得到的每个方程的答案是15 我的TestClass如下所示:Java RPN计算器空指针问题,java,calculator,rpn,Java,Calculator,Rpn,嗨,我已经设法解决了空指针问题 现在我几乎让代码工作了,除了在计算时,我得到的每个方程的答案是15 我的TestClass如下所示: import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.StringToken
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class TestClass {
private static final int LEFT_ASSOC = 0;
private static final int RIGHT_ASSOC = 1;
static String OPERATORS1 = "+-*/()";
// Operators
private static final Map<String, int[]> OPERATORS = new HashMap<String, int[]>();
static {
// Map<"token", []{precedence, associativity}>
OPERATORS.put("+", new int[] { 2, LEFT_ASSOC });
OPERATORS.put("-", new int[] { 2, LEFT_ASSOC });
OPERATORS.put("*", new int[] { 3, LEFT_ASSOC });
OPERATORS.put("/", new int[] { 3, LEFT_ASSOC });
OPERATORS.put("(", new int[] {1, RIGHT_ASSOC});
OPERATORS.put(")", new int[] {1, LEFT_ASSOC});
}
private static boolean isOperator(String token) {
return OPERATORS.containsKey(token);
}
// Test associativity of operator token
private static boolean isAssociative(String token, int type) {
if (!isOperator(token)) {
throw new IllegalArgumentException("Invalid token: " + token);
}
if (OPERATORS.get(token)[1] == type) {
return true;
}
return false;
}
// Compare precedence of operators.
private static final int cmpPrecedence(String token1, String token2) {
if (!isOperator(token1) || !isOperator(token2)) {
throw new IllegalArgumentException("Invalid tokens: " + token1
+ " " + token2);
}
return OPERATORS.get(token1)[0] - OPERATORS.get(token2)[0];
}
public static String[] infixToRPN(String[] inputTokens) {
myArrayList<String> out = new myArrayList<String>();
myStack<String> stack = new myStack<String>();
// For each token
for (String token : inputTokens) {
StringTokenizer tokens = new StringTokenizer(token,OPERATORS1,true);
while (tokens.hasMoreTokens()) {
token = tokens.nextToken();
}
// If token is an operator
if (isOperator(token)) {
// While stack not empty AND stack top element
// is an operator
while (!stack.isEmpty() && isOperator(stack.peek())) {
if ((isAssociative(token, LEFT_ASSOC) && cmpPrecedence(
token, stack.peek()) <= 0)
|| (isAssociative(token, RIGHT_ASSOC) && cmpPrecedence(
token, stack.peek()) < 0)) {
out.add(stack.pop());
continue;
}
break;
}
// Push the new operator on the stack
stack.push(token);
}
// If token is a left bracket '('
else if (token.equals("(")) {
stack.push(token);
}
// If token is a right bracket ')'
else if (token.equals(")")) {
while (!stack.isEmpty() && !stack.peek().equals("(")) {
out.add(stack.pop());
}
stack.pop();
}
// If token is a number
else {
out.add(token);
}
}
while (!stack.isEmpty()) {
out.add(stack.pop());
}
String[] output = new String[out.size()];
return out.toArray(output);
}
public static double RPNtoDouble(String[] tokens) {
myStack<String> stack = new myStack<String>();
// For each token
for (String token : tokens) {
// If the token is a value push it onto the stack
if (!isOperator(token)) {
stack.push(token);
} else {
// Token is an operator: pop top two entries
Double d2 = Double.valueOf(stack.pop());
Double d1 = Double.valueOf(stack.pop());
// Get the result
Double result = token.compareTo("+") == 0 ? d1 + d2 : token
.compareTo("-") == 0 ? d1 - d2
: token.compareTo("*") == 0 ? d1 * d2 : d1 / d2;
// Push result onto stack
stack.push(String.valueOf(result));
}
}
return Double.valueOf(stack.pop());
}
@SuppressWarnings("unused")
static public void main(String[] args) throws IOException {
File file = new File("testEquations.txt");
String[] lines = new String[10];
try {
FileReader reader = new FileReader(file);
@SuppressWarnings("resource")
BufferedReader buffReader = new BufferedReader(reader);
int x = 0;
String s;
while ((s = buffReader.readLine()) != null) {
lines[x] = s;
x++;
}
} catch (IOException e) {
System.exit(0);
}
// test printing string array
for (String s : lines) {
System.out.println("" + s);
String[] output =infixToRPN(lines);
System.out.println(RPNtoDouble(output));
}
for (String st : lines) {
if (st != null) {
StringTokenizer tokens = new StringTokenizer(st,OPERATORS1,true);
while (tokens.hasMoreTokens()) {
String token = tokens.nextToken();
}
}
}
}
}
这来自以下输入文件:
49+62*61-36
4/64
(53+26)
0*72
21-85+75-85
90*76-50+67
46*89-15
34/83-38
20/76/14+92-15
5*10/3-1
任何帮助都将不胜感激
Than用于阅读。如果您的行号正确,则这是行: else if (token.equals("(")) { else if(标记为等于(“”){
很明显,token是空的。=>检查这是否是事实,然后看看为什么是这样。我在这里没有看到任何RPN表达式。在第一行中,我希望看到类似49、62、61、*、36、,+的内容,为什么不把代码放在问题中呢?当然,这对你来说需要更多的工作,但是想要帮助你的人不得不使用c结果是3个不同的pastebins。这并不好笑-请参阅为什么在pastebin上转储代码不是一个好主意的背景。HI@mrab是的,我知道它们不是RPN。我已经编写了一个方法将它们解析为RPN,然后是另一个方法来评估该方法传递的RPN方程。HI@fvu现在将编辑并添加我假设这是因为第一个方程没有任何括号。所以我想知道如何让它跳过它没有检测到任何括号的那部分?嗨,我已经尝试过调试,我看不出堆栈应该为空的任何原因?我假设我需要将调用infixToRPN(行)放在它将使用存储在行中的数组值的某个地方? else if (token.equals("(")) {