我正在开发一个计算器应用程序,我已经完成了所有基本功能。唯一的问题是,当我使用平方根函数时,它会返回错误Unexpected: √
。请帮忙。我附上了我认为导致问题的代码部分。
public static double eval(final String str) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
boolean eat(int charToEat) {
while (ch == ' ') nextChar();
if (ch == charToEat) {
nextChar();
return true;
}
return false;
}
double parse() {
nextChar();
double x = parseExpression();
if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
return x;
}
// Grammar:
// expression = term | expression `+` term | expression `-` term
// term = factor | term `*` factor | term `/` factor
// factor = `+` factor | `-` factor | `(` expression `)`
// | number | functionName factor | factor `^` factor
double parseExpression() {
double x = parseTerm();
for (;;) {
if (eat('+')) x += parseTerm(); // addition
else if (eat('-')) x -= parseTerm(); // subtraction
else return x;
}
}
double parseTerm() {
double x = parseFactor();
for (;;) {
if (eat('x')) x *= parseFactor(); // multiplication
else if (eat('÷')) x /= parseFactor(); // division
else return x;
}
}
double parseFactor() {
if (eat('+')) return parseFactor(); // unary plus
if (eat('-')) return -parseFactor(); // unary minus
double x;
int startPos = this.pos;
if (eat('(')) { // parentheses
x = parseExpression();
eat(')');
} else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
x = Double.parseDouble(str.substring(startPos, this.pos));
} else if (ch >= 'a' && ch <= 'z') { // functions
while (ch >= 'a' && ch <= 'z') nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("√")) x = Math.sqrt(x);
else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
else throw new RuntimeException("Unknown function: " + func);
} else {
throw new RuntimeException("Unexpected: " + (char)ch);
}
if (eat('^')) x = Math.pow(x, parseFactor()); // exponentiation
return x;
}
}.parse();
}
06-09 00:51:17.906 8128-8128/com.arc.lumen.calculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.arc.lumen.calculator, PID: 8128 java.lang.RuntimeException: Unexpected: √
at com.arc.lumen.calculator.ScientificCalculator$20.parseFactor(ScientificCalculator.java:287)
at com.arc.lumen.calculator.ScientificCalculator$20.parseTerm(ScientificCalculator.java:257)
at com.arc.lumen.calculator.ScientificCalculator$20.parseExpression(ScientificCalculator.java:248)
at com.arc.lumen.calculator.ScientificCalculator$20.parse(ScientificCalculator.java:236)
at com.arc.lumen.calculator.ScientificCalculator.eval(ScientificCalculator.java:294)
at com.arc.lumen.calculator.ScientificCalculator.getResult(ScientificCalculator.java:206)
at com.arc.lumen.calculator.ScientificCalculator.onClickEquals(ScientificCalculator.java:182)
at com.arc.lumen.calculator.ScientificCalculator$18.onClick(ScientificCalculator.java:166)
at android.view.View.performClick(View.java:4461)
at android.view.View$PerformClick.run(View.java:18525)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)