これまでのところ、正常に機能する次のものがあります。これを行うにはおそらくもっと簡単な方法があると思いますが、私が変更/変更する必要があるのは、Matheqのトップメソッドです。Mathメソッドは、単一の数学演算を実行します。
+、-、*、および/を使用した単一の操作で機能します。
私の問題は、10-10 / 5 + 3などのより大きな方程式を解くことです。ただし、10/5 + 65*2は正しく解かれます。その理由は、各部分、数値、および演算が文字列配列に分割されるためです。各操作が完了すると、数値と操作がその方程式の結果に置き換えられます。もっと混乱しているように見えるかもしれませんが、私はこれ以上の方法を考えることができませんでした。他の方程式を解けない理由は、文字列を文字列配列にマップし直したためです。
文字列配列の例例 10-10/5 +3文字列={10、-、10、/、5、+、3}の場合、演算が最初に除算され、次に左から右に減算され、次に加算文字列= {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;
}
}