到目前为止,我有以下工作正常。我确信可能有一种更简单的方法可以做到这一点,但我需要改变/改变的是 Matheq 的顶级方法。Math 方法执行单个数学运算。
它适用于 +、-、* 和 / 的任何单个操作。
我的问题是求解一个更大的方程,例如 10 - 10 / 5 + 3。但是它确实可以正确求解 10 / 5 + 65 * 2。原因是每个部分,数字和操作,都被拆分为一个字符串数组。每次操作完成后,数字和操作将替换为该等式的结果。可能看起来更混乱,但我想不出更好的方法。它无法解决其他方程的原因是因为我如何将字符串映射回字符串数组。
字符串数组示例 Ex. 与 10 - 10 / 5 + 3 String = { 10, -, 10, /, 5, +, 3 } 运算后先除法然后从左到右减法然后加法 String = { 8, 8, 2, 2, 2, 5, 5 }
这是我的代码,请有人帮助我:
REVISED 已修改,现在它可以与上面一起使用,但对于 LONG 方程仍然存在一些问题。一个简短的例子是它可以很好地解决 2 * 2 * 2 * 2 除以 5 但如果将其更改为 10 - 2 * 2 * 2 * 2 除以 5 我会得到错误的答案。
public class Matheq {
String fnum = null;
String lnum = null;
String total = null;
public String Matheq(String mathoperation) {
String mathoperation= "6 * 3 - 4 * 2";
mathoperation = mathoperation.replaceAll(",", "");
mathoperation = mathoperation.replaceAll("plus", "+");
mathoperation = mathoperation.replaceAll("minus", "-");
mathoperation = mathoperation.replaceAll("times", "*");
mathoperation = mathoperation.replaceAll("divided by", "dividedby");
mathoperation = mathoperation.replaceAll("percent of", "percentof");
String[] splitstr = mathoperation.split(" ");
while(splitstr.length>1){
for(int i=0; i<splitstr.length; i++) {
System.out.println("Get value: " + splitstr[i]);
if(splitstr[i].indexOf("percentof") >= 0) {
String buildit = splitstr[i-1] + " percent of " + splitstr[i+1];
String done = math(buildit);
System.out.println("Percentage operation: " + splitstr[i-1] + " percent of " + splitstr[i+1] + "=" + done);
splitstr[i] = done;
splitstr[i-1] = "";
splitstr[i+1] = "";
ArrayList<String> list = new ArrayList<String>();
for(String s : splitstr){
if(!s.equals("")){
list.add(s);
}
}
splitstr = list.toArray(new String[list.size()]);
}
}
for(int i=0; i<splitstr.length; i++) {
System.out.println("Get value: " + splitstr[i]);
if(splitstr[i].indexOf("dividedby") >= 0) {
String buildit = splitstr[i-1] + " divided by " + splitstr[i+1];
String done = math(buildit);
System.out.println("Division operation: " + splitstr[i-1] + " divided by " + splitstr[i+1] + "=" + done);
splitstr[i] = done;
splitstr[i-1] = "";
splitstr[i+1] = "";
ArrayList<String> list = new ArrayList<String>();
for(String s : splitstr){
if(!s.equals("")){
list.add(s);
}
}
splitstr = list.toArray(new String[list.size()]);
}
}
for(int i=0; i<splitstr.length; i++) {
System.out.println("Get value: " + splitstr[i]);
if(splitstr[i].indexOf("*") >= 0) {
String buildit = splitstr[i-1] + " * " + splitstr[i+1];
String done = math(buildit);
System.out.println("Multiplication operation: "+ splitstr[i-1] + " * " + splitstr[i+1] + "=" + done);
splitstr[i] = done;
splitstr[i-1] = "";
splitstr[i+1] = "";
ArrayList<String> list = new ArrayList<String>();
for(String s : splitstr){
if(!s.equals("")){
list.add(s);
}
}
splitstr = list.toArray(new String[list.size()]);
}
}
for(int i=0; i<splitstr.length; i++) {
System.out.println("Get value: " + splitstr[i]);
if(splitstr[i].indexOf("+") >= 0) {
String buildit = splitstr[i-1] + " + " + splitstr[i+1];
String done = math(buildit);
System.out.println("Addition operation: " + splitstr[i-1] + " + " + splitstr[i+1] + "=" + done);
splitstr[i] = done;
splitstr[i-1] = "";
splitstr[i+1] = "";
ArrayList<String> list = new ArrayList<String>();
for(String s : splitstr){
if(!s.equals("")){
list.add(s);
}
}
splitstr = list.toArray(new String[list.size()]);
}
}
for(int i=0; i<splitstr.length; i++) {
System.out.println("Get value: " + splitstr[i]);
if(splitstr[i].indexOf("-") >= 0) {
String buildit = splitstr[i-1] + " - " + splitstr[i+1];
String done = math(buildit);
System.out.println("Subtraction operation: " + splitstr[i-1] + " - " + splitstr[i+1] + "=" + done);
splitstr[i] = done;
splitstr[i-1] = "";
splitstr[i+1] = "";
ArrayList<String> list = new ArrayList<String>();
for(String s : splitstr){
if(!s.equals("")){
list.add(s);
}
}
splitstr = list.toArray(new String[list.size()]);
}
}
for(int i=0; i<splitstr.length; i++) {
System.out.println("Final operation: " + total + " " + splitstr[i]);
}
}
return total;
}
private String math(String mathoperation) {
// TODO Auto-generated method stub
if(mathoperation.contains("percent of")){
mathoperation = mathoperation.replaceAll("percent of", "%");
int str = mathoperation.indexOf("%");
System.out.println(str);
fnum = mathoperation.substring(0, str-1);
fnum = fnum.replaceAll(" ", "");
fnum = "." + fnum;
System.out.println(fnum);
double intfnum = Double.parseDouble(fnum);
System.out.println(intfnum);
int lastind = mathoperation.length();
System.out.println(lastind);
lnum = mathoperation.substring(str+1, lastind);
lnum = lnum.replaceAll(" ", "");
System.out.println(lnum);
double intlnum = Double.parseDouble(lnum);
System.out.println(intlnum);
double tot = intlnum * intfnum;
System.out.println(tot);
total = Double.toString(tot);
if(total.length() == 3){
total = total + "0";
}
if(total.length() > 5){
total = total.substring(0, 4);
}
total = total.replace("0.", "");
System.out.println("Total:" + total);
} else
if(mathoperation.contains("-")){
int str = mathoperation.indexOf("-");
System.out.println(str);
fnum = mathoperation.substring(0, str-1);
fnum = fnum.replaceAll(" ", "");
System.out.println(fnum);
double intfnum = Double.parseDouble(fnum);
System.out.println(intfnum);
int lastind = mathoperation.length();
System.out.println(lastind);
lnum = mathoperation.substring(str+1, lastind);
lnum = lnum.replaceAll(" ", "");
System.out.println(lnum);
double intlnum = Double.parseDouble(lnum);
System.out.println(intlnum);
double tot = intfnum - intlnum;
System.out.println(tot);
total = Double.toString(tot);
System.out.println(total);
} else
if(mathoperation.contains("+")){
int str = mathoperation.indexOf("+");
System.out.println(str);
fnum = mathoperation.substring(0, str-1);
fnum = fnum.replaceAll(" ", "");
System.out.println(fnum);
double intfnum = Double.parseDouble(fnum);
System.out.println(intfnum);
int lastind = mathoperation.length();
System.out.println(lastind);
lnum = mathoperation.substring(str+1, lastind);
lnum = lnum.replaceAll(" ", "");
System.out.println(lnum);
double intlnum = Double.parseDouble(lnum);
System.out.println(intlnum);
double tot = intfnum + intlnum;
System.out.println(tot);
total = Double.toString(tot);
System.out.println(total);
} else
if(mathoperation.contains("*")){
int str = mathoperation.indexOf("*");
System.out.println(str);
fnum = mathoperation.substring(0, str-1);
fnum = fnum.replaceAll(" ", "");
System.out.println(fnum);
double intfnum = Double.parseDouble(fnum);
System.out.println(intfnum);
int lastind = mathoperation.length();
System.out.println(lastind);
lnum = mathoperation.substring(str+1, lastind);
lnum = lnum.replaceAll(" ", "");
System.out.println(lnum);
double intlnum = Double.parseDouble(lnum);
System.out.println(intlnum);
double tot = intfnum * intlnum;
System.out.println(tot);
total = Double.toString(tot);
System.out.println(total);
} else
if(mathoperation.contains("divided by")){
mathoperation = mathoperation.replaceAll("divided by", "/");
int str = mathoperation.indexOf("/");
System.out.println(str);
fnum = mathoperation.substring(0, str-1);
fnum = fnum.replaceAll(" ", "");
System.out.println(fnum);
double intfnum = Double.parseDouble(fnum);
System.out.println(intfnum);
int lastind = mathoperation.length();
System.out.println(lastind);
lnum = mathoperation.substring(str+1, lastind);
lnum = lnum.replaceAll(" ", "");
System.out.println(lnum);
double intlnum = Double.parseDouble(lnum);
System.out.println(intlnum);
double tot = intfnum / intlnum;
System.out.println(tot);
total = Double.toString(tot);
System.out.println(total);
} else {
total = null;
}
return total;
}
}