gpt4 book ai didi

具有多种操作的Java计算器

转载 作者:搜寻专家 更新时间:2023-11-01 01:13:44 25 4
gpt4 key购买 nike

到目前为止,我有以下工作正常。我确信可能有一种更简单的方法可以做到这一点,但我需要改变/改变的是 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 }

这是我的代码,请有人帮助我:

修订修改后,现在它适用于上面的内容,但在使用 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;
}

}

最佳答案

数组是表示已解析等式的错误结构。您需要有一个可以表示运算符优先级的结构。处理此类问题的典型机制是 abstract syntax tree。 .对于您的 10 - 10/5 + 3 示例,您可能希望构建一棵如下所示的树:

 <result>
/ \
'-' '+'
/ \ / \
10 '/' 3
/ \
10 5

使用这种具有高优先级运算符的结构,可以降低树的下部,然后您可以执行自下而上的评估以获得正确的结果。

关于具有多种操作的Java计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12448417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com