用 int 替换 String 中的字符,以便对其进行评估 - Java
Replace a character from a String with an int so it can be evaluated - Java
我正在编写的一个与算术级数有关的应用程序需要一些帮助。
基本上,用户输入一个公式 - 其中包含一个变量 n - 以及他们想要计算的术语范围。然后应用程序应该计算每个案例,将 n 替换为正在计算的当前项,从第一项开始直到最后一项,最后显示完整序列。
例如,如果我将其输入为公式:
安=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,就是这个:
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();
}
为了完成这项工作(因为上面的方法不接受我的自定义变量 n),我正在尝试使用 replace()
方法将字符 n
替换为整数 i
- 表示当前项 - 在 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
}
但是每当我输入包含 "n" 的公式时,应用程序就会崩溃。
Logcat:
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.parseFactor(MessageFragment.java:96)
at com.example.APTesting.MessageFragment.parseTerm(MessageFragment.java:66)
at com.example.APTesting.MessageFragment.parseExpression(MessageFragment.java:57)
at com.example.APTesting.MessageFragment.parse(MessageFragment.java:45)
at com.example.APTesting.MessageFragment.eval(MessageFragment.java:103)
at com.example.APTesting.MessageFragment.access0(MessageFragment.java:20)
at com.example.APTesting.MessageFragment.onClick(MessageFragment.java:149)
at android.view.View.performClick(View.java:7161)
at android.view.View.performClickInternal(View.java:7133)
at android.view.View.access00(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")
,这可能是由于将 'n'
替换为 i
错误消息来自 eval() 方法 - throw new RuntimeException("Unexpected: " + (char) ch);
。
我尝试了类似问题的解决方案,但弹出了几乎相同的错误。
我是初学者。任何帮助将不胜感激。
请试试这个..
String strResult = strFormula.replace('n','i').trim().replace(" ","")
将 i
的值转换为 char 会产生不可打印的字符。例如,如果 i
为 1,则结果字符将成为 header 控制字符的开始。这就是您看到带有问号的框的原因。
您需要整数 i
的字符串值。只需使用
strFormula.replace("n", String.valueOf(i))
我正在编写的一个与算术级数有关的应用程序需要一些帮助。 基本上,用户输入一个公式 - 其中包含一个变量 n - 以及他们想要计算的术语范围。然后应用程序应该计算每个案例,将 n 替换为正在计算的当前项,从第一项开始直到最后一项,最后显示完整序列。 例如,如果我将其输入为公式:
安=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,就是这个:
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();
}
为了完成这项工作(因为上面的方法不接受我的自定义变量 n),我正在尝试使用 replace()
方法将字符 n
替换为整数 i
- 表示当前项 - 在 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
}
但是每当我输入包含 "n" 的公式时,应用程序就会崩溃。 Logcat:
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.parseFactor(MessageFragment.java:96)
at com.example.APTesting.MessageFragment.parseTerm(MessageFragment.java:66)
at com.example.APTesting.MessageFragment.parseExpression(MessageFragment.java:57)
at com.example.APTesting.MessageFragment.parse(MessageFragment.java:45)
at com.example.APTesting.MessageFragment.eval(MessageFragment.java:103)
at com.example.APTesting.MessageFragment.access0(MessageFragment.java:20)
at com.example.APTesting.MessageFragment.onClick(MessageFragment.java:149)
at android.view.View.performClick(View.java:7161)
at android.view.View.performClickInternal(View.java:7133)
at android.view.View.access00(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")
,这可能是由于将 'n'
替换为 i
错误消息来自 eval() 方法 - throw new RuntimeException("Unexpected: " + (char) ch);
。
我尝试了类似问题的解决方案,但弹出了几乎相同的错误。
我是初学者。任何帮助将不胜感激。
请试试这个..
String strResult = strFormula.replace('n','i').trim().replace(" ","")
将 i
的值转换为 char 会产生不可打印的字符。例如,如果 i
为 1,则结果字符将成为 header 控制字符的开始。这就是您看到带有问号的框的原因。
您需要整数 i
的字符串值。只需使用
strFormula.replace("n", String.valueOf(i))