我想为我正在编写的与算术级数有关的应用提供帮助。基本上,用户输入一个公式-包含变量n-和他们想要计算的术语范围。然后,应用程序应计算每种情况,将n替换为正在计算的当前项,从第一个项开始直到到达最后一个项,最后显示完整的序列。例如,如果我输入此作为公式:
An = n + 4
并且在范围内:
从1
到5
输出应为:
A1 = 5.0 //because it is n + 4 and the current term n is 1, so 1 + 2
A2 = 6.0 //n + 4, n = 2 so 3 + 2, and so on...
A3 = 7.0
A4 = 8.0
A5 = 9.0 //n reached 5, so the calculation stops there.
Complete sequence: ( 5.0 , 6.0 , 7.0 , 8.0 , 9.0 )
结果被定义为double
,因为它并不总是整数(例如An = n + 1/2
)。
为了评估数学表达式,我使用的是@Boann's eval
method,这就是这个:
eval
并且为了使这项工作有效(因为上述方法将不接受我的自定义变量n),我正在尝试使用private 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 *= 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("sqrt")) 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();
}
方法将字符replace()
替换为整数[ C0]-表示当前术语-在n
循环中定义:
i
但是只要我输入包含“ n”的公式,应用程序就会崩溃。Logcat:
for
for (i = firstTerm; i < lastTerm; ) {
String strResult = strFormula.replace('n', (char) i); //the Result string, given after replacing the character "n" from the formula with the variable "i" so it can be evaluated
termsAndResults.add("A" + i + " = " + eval(strResult) + "\n"); //add the current term and its result (e.g. "A3 = 8", "A4 = 10") to a list
allResults.add(String.valueOf(eval(strResult))); //add the result to a list so each of them can be displayed later
i++; //move onto the next term
if (i == lastTerm) {
textResults.setText(" " + termsAndResults.toString().replace('[', ' ').replace(']', ' ').replace(',', ' ').trim() + "\nComplete sequence: (" + allResults.toString().replace('[', ' ').replace(']', ' ').trim() + ", ...)"); //display each term, its results and finally the complete sequence. All those replace and trim methods are there just so there won't be any "," or "[" and "]" when the results are displayed
}
是不显示的字符。通过单击错误消息,问题似乎出在com.example.APTesting E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.APTesting, PID: 22158
java.lang.RuntimeException: Unexpected: [and a square character which doesn't show up here as text][2]
at com.example.APTesting.MessageFragment$1.parseFactor(MessageFragment.java:96)
at com.example.APTesting.MessageFragment$1.parseTerm(MessageFragment.java:66)
at com.example.APTesting.MessageFragment$1.parseExpression(MessageFragment.java:57)
at com.example.APTesting.MessageFragment$1.parse(MessageFragment.java:45)
at com.example.APTesting.MessageFragment.eval(MessageFragment.java:103)
at com.example.APTesting.MessageFragment.access$300(MessageFragment.java:20)
at com.example.APTesting.MessageFragment$3.onClick(MessageFragment.java:149)
at android.view.View.performClick(View.java:7161)
at android.view.View.performClickInternal(View.java:7133)
at android.view.View.access$3500(View.java:804)
at android.view.View$PerformClick.run(View.java:27416)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:7617)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
,这可能是由于将This替换为termsAndResults.add("A" + i + " = " + eval(strResult) + "\n")
错误消息来自eval()方法-'n'
。我尝试了类似问题的解决方案,但弹出的错误几乎相同。我是初学者。任何帮助,将不胜感激。
请尝试这个。。>
i
将throw new RuntimeException("Unexpected: " + (char) ch);
的那些值铸造为char将导致无法打印的字符。例如,如果String strResult = strFormula.replace('n','i').trim().replace(" ","")
为1,则结果字符将成为标题的开始