我正在尝试从arraylist中读取符号,然后对它之前和之后的两个数字进行表达式(例如i-1 * i + 1)。但是,生成的结果始终是5、6或12! 我真的找不到代码中的问题所在。
我之所以使用arraylist是因为它比数组更具动态性,从某种意义上说,我可以删除元素而不必关心'array'的长度。
String replaceStr = replacer.replace("+", " + ");
我正在尝试添加空间以将整数用作元素,而不是使用charAt,这将不允许我将大于一位数的数字带入方程式。
Double Computing(String equation) {
double result = 0.0;
double multiplResult = 1.0; //
String replacer = equation;
String replaceStr = replacer.replace("+", " + ");
String replaceStr1 = replaceStr.replace("x", " x ");
String replaceStr2 = replaceStr1.replace("-", " - ");
String replaceStr3 = replaceStr2.replace("/", " / ");
String[] items = replaceStr3.split("\\s*");
ArrayList<String> list = new ArrayList<String>((Arrays.asList(items)));
for (int i = 0; i < list.size(); i++ ) {
String NewNum;
if (list.get(i) == "x" || list.get(i) == "/"){
if(list.get(i) == "x") {
NewNum = String.valueOf(Double.parseDouble(list.get(i - 1)) * Double.parseDouble(list.get(i + 1)));
list.set(i, NewNum);
list.remove(i-1);
list.remove(i+1);
}
else if (list.get(i) == "/"){
NewNum = String.valueOf(Double.parseDouble(list.get(i - 1)) / Double.parseDouble(list.get(i + 1)));
list.set(i, NewNum);
list.remove(i-1);
list.remove(i+1);
}
multiplResult *= Double.parseDouble(list.get(0));
}
if (list.get(i) == "+" || list.get(i) == "-"){
if(list.get(i) == "+") {
NewNum = String.valueOf(Double.parseDouble(list.get(i - 1)) + Double.parseDouble(list.get(i + 1)));
list.set(i, NewNum);
list.remove(i-1);
list.remove(i+1);
}
else if (list.get(i) == "-"){
NewNum = String.valueOf(Double.parseDouble(list.get(i - 1)) - Double.parseDouble(list.get(i + 1)));
list.set(i, NewNum);
list.remove(i-1);
list.remove(i+1);
}
multiplResult *= Double.parseDouble(list.get(0));
}
result += multiplResult;
}
return result;
}
答案 0 :(得分:0)
如果仅使用+,-,x和/且不使用方括号,并且等式的语法正确,则可以这样做。但是您必须先评估x和/:
Double Computing(String equation) {
String replace = equation.replace("+", " + ").replace("x", " x ").replace("-", " - ").replace("/", " / ");
List<Object> list = new ArrayList<>(Arrays.asList(replace.split("\\s+")));
for (int i = 0; i < list.size(); i++) {
try {
list.set(i, Double.parseDouble(list.get(i).toString()));
} catch (NumberFormatException e) {
}
}
while (list.contains("x")) {
int i = list.indexOf("x");
double mult = (Double) list.get(i - 1) * (Double) list.get(i + 1);
list.remove(i + 1);
list.set(i, mult);
list.remove(i - 1);
}
while (list.contains("/")) {
int i = list.indexOf("/");
double div = (Double) list.get(i - 1) / (Double) list.get(i + 1);
list.remove(i + 1);
list.set(i, div);
list.remove(i - 1);
}
double sum = (Double) list.get(0);
for (int i = 1; i < (list.size() - 1); i += 2) {
if (list.get(i).equals("+")) {
sum += (Double) list.get(i + 1);
} else {
sum -= (Double) list.get(i + 1);
}
}
return sum;
}
答案 1 :(得分:0)
这是一个简单的评估器:
public class Evaluator {
private final char[] chars;
private int pos;
public static double evaluate(String expr) {
return new Evaluator(expr).expression();
}
private Evaluator(String expr) {
chars = expr.toCharArray();
}
private double expression() {
double value = term();
char op;
while (skipSpaces() && ((op = chars[pos]) == '+' || op == '-')) {
++pos;
double other = term();
switch (op) {
case '+':
value += other;
break;
case '-':
value -= other;
break;
}
}
return value;
}
private double term() {
double value = factor();
char op;
while (skipSpaces() && ((op = chars[pos]) == 'x' || op == '/')) {
++pos;
double other = factor();
switch (op) {
case 'x':
value *= other;
break;
case '/':
value /= other;
break;
}
}
return value;
}
private double factor() {
if (skipSpaces() && chars[pos] == '(') {
++pos;
double result = expression();
if (skipSpaces() && chars[pos] == ')') {
++pos;
}
return result;
}
return number();
}
private double number() {
if (!skipSpaces()) {
return 0;
}
int start = pos;
if (chars[pos] == '+' || chars[pos] == '-') {
++pos;
}
if (pos >= chars.length || !Character.isDigit(chars[pos])) {
return 0;
}
do {
++pos;
} while (pos < chars.length
&& (Character.isDigit(chars[pos]) || chars[pos] == '.'));
return Double.parseDouble(new String(chars, start, pos-start));
}
private boolean skipSpaces() {
while (pos < chars.length && Character.isWhitespace(chars[pos])) {
++pos;
}
return pos < chars.length;
}
}