我正在开发一个Android计算器应用程序并且已经关闭了所有基本功能,但是当我按下阶乘按钮时,它根本不会返回任何内容。我不知道为什么。请帮忙。
public static double eval(final String str) {
return new Object() {
int pos = -1, ch;
void nextChar() {
ch = (++pos < str.length()) ? str.charAt(pos) : -1;
}
void prevChar() {
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' || ch >= 'A' && ch <= 'Z') { // functions
while (ch >= 'a' && ch <= 'z'|| ch >= 'A' && ch <= 'Z') nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("sin")) x = Math.sin(x);
else if (func.equals("cos")) x = Math.cos(x);
else if (func.equals("tan")) x = Math.tan(x);
else if (func.equals("log")) x = Math.log(x);
else if (func.equals("ln")) x = Math.log10(x);
else if (func.equals("Abs")) x = Math.abs(x);
//else if (func.equals("fact")) x = CombinatoricsUtils.factorial((int) x);
else throw new RuntimeException("Unknown function: " + func);
}
else if (ch == '√') { // functions
while (ch == '√') nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
if (func.equals("√")) x = Math.sqrt(x);
else throw new RuntimeException("Unknown function: " + func);
}
else if (ch == '!') { // functions
int strLength = str.length();
String func = str.substring(strLength - 1);
x = parseFactor();
if (func.equals("!")) x = CombinatoricsUtils.factorial((int) x);
else throw new RuntimeException("Unknown function: " + func);
}
else {
throw new RuntimeException("Unexpected: " + (char)ch);
}
if (eat('^')) x = Math.pow(x, parseFactor());
else if (eat('³')) x = Math.pow(x, 3);
else if (eat('²')) x = Math.pow(x, 2); // exponentiation
return x;
}
}.parse();
答案 0 :(得分:0)
我用一个简单的测试程序尝试了你的代码:
public static void main(String[] args) {
System.out.println(eval("!5"));
}
我收到了StackOverflowError,因为您的代码没有跳过!
更改
else if (ch == '!') { // functions
int strLength = str.length();
String func = str.substring(strLength - 1);
x = parseFactor();
到
else if (ch == '!') { // functions
while (ch == '!') nextChar();
String func = str.substring(startPos, this.pos);
x = parseFactor();
为我纠正了这个问题。
如果你不想在感叹号上面循环,你仍然需要跳过它:
else if (ch == '!') { // functions
eat('!'); // this is missing in your current implementation
x = CombinatoricsUtils.factorial((int) parseFactor());
目前我没有解释为什么“事实”的代码不起作用 - 如果我取消注释它们似乎有效(至少对我而言)。