问题遇到的现象和发生背景 我在试着写一个可以将字符串转化为一个计算时,程序遇到如下问题
但我发现输入不同数据会产生不同的问题
用代码块功能插入代码,请勿粘贴截图
import java.util.*;
public class TestMathExper_122090080 {
public static String A(String str) {// 预处理字符串
String str0 = str.replace(" ", "");
String str1 = str0.toString();
char a = str1.charAt(1);
if (a == '-') {
String k = "0" + str1;
return k;
} else {
return str1;
}
}
public static String removekuohao(String str){//去括号
String a1 = str;
if(str.contains("[(]")){
int x0 =str.lastIndexOf("(");
String y0 = str.substring(x0+1,a1.length());
int x1 =y0.indexOf(")");
String y1 = y0.substring(1, x1);
String p0 = Sanjiao(y1);
String final1;
if(p0.contains("[(]")){
String p1 =p0.replace("(", "").replace(")","");
String p2 = Chengchu(p1);
if(p2.contains("[(]")){
String p3 =p2.replace("(", "").replace(")","");
String p4 = Jiajian(p3);
if(p4.contains("[(]")){
final1 =p4.replace("(", "").replace(")","");
}
else{
final1 =p4;
}}
else{
String p3 =Jiajian(p2);
if(p3.contains("[(]")){
final1 =p3.replace("(", "").replace(")","");
}
else{
final1 =p3;
}
}
}
else {
String p1 =Chengchu(p0);
if(p1.contains("[(]")){
String p2 =p1.replace("(", "").replace(")","");
String p3 = Jiajian(p2);
if(p3.contains("[(]")){
final1 =p3.replace("(", "").replace(")","");
}
else{
final1 =p3;
}}
else{
String p2 =Jiajian(p1);
if(p2.contains("[(]")){
final1 =p2.replace("(", "").replace(")","");
}
else{
final1 =p2;
}
}
}
int Midlength = x0 + y1.length() +2;
String Mid = str.substring(x0,Midlength);
String str2 =str.replaceAll(Mid, final1);
String begin =str2.toString();
return removekuohao(begin) ;
}
return a1;
}
public static String Chengchu(String str) {// 处理乘除号
String a2 = str;
//分割字符串
String re =A(str);
int x = 0,y =0,z=0,k =0,x0 =0,y0 =0;
char ai;
String[] b0 = re.split("[*/+-]");//数字串
String b1 =re.replaceAll("\\d","").replace(".","");
if(re.contains("*")||re.contains("/")){
if(re.contains("*")&&re.contains("/")){
x = b1.indexOf("*");
y = b1.indexOf("/");
x0 =re.indexOf("*");
y0 =re.indexOf("/");
if(x > y){//除号在前
ai = re.charAt(y0+1);
if(ai == '-'){
z=b1.indexOf("/");
k =re.indexOf("/");
b0[z+1] ="-"+b0[z+1];
}
else{z = b1.indexOf("/");
k = re.indexOf("/");}
}
else if(y > x){//乘号在前
ai = re.charAt(x0+1);
if(ai == '-'){
z=b1.indexOf("*");
k =re.indexOf("*");
b0[z+1] ="-"+b0[z+1];
}
else{z = b1.indexOf("*");
k = re.indexOf("*");
b0[z+1] =b0[z+1];
}
}
}
else if(re.contains("*")){
ai = re.charAt(x0+1);
if(ai == '-'){
z=b1.indexOf("*");
k =re.indexOf("*");
b0[z+1] ="-"+b0[z+1];
}
else{z = b1.indexOf("*");
k = re.indexOf("*");
b0[z+1] =b0[z+1];}
}
else if(re.contains("/")){
ai = re.charAt(y0+1);
if(ai == '-'){
z=b1.indexOf("/");
k =re.indexOf("/");
b0[z+1] ="-"+b0[z+1];
}
else{z = b1.indexOf("/");
k = re.indexOf("/");
b0[z+1] =b0[z+1];}
}
String smallequation = b0[z]+b1.charAt(z)+b0[z+1];
String result1 = suanshi(smallequation);//获得结果
int be = k - b0[z].length(), end =k+b0[z+1].length()+1;
String equation1 = re.substring(be, end);
String u = re.replaceAll(equation1, result1);
return u;
}
return a2;
}
public static String Jiajian(String str) {// 处理加减
String a3 = str;
if(str.contains("+")||str.contains("-")){
String fuck;
if (str.contains("+-")||str.contains("--")){
fuck =str.replaceAll("+-","-").replaceAll("--","+");
}
else{
fuck =str;
}
String fuck1 =A(fuck);
String[] l0 = fuck1.split("[*/+-]");//数字串
String l1 =fuck1.replaceAll("\\d","").replace(".","");
int l = 0,m =0,n=0,v =0;
if(fuck1.contains("+")&&fuck1.contains("-")){
l = l1.indexOf("+");
m = l1.indexOf("-");
if(l > m){//减号在前
n = m;
v = fuck1.indexOf("-");
}
else if(m > l){//乘号在前
n = l;
v = fuck1.indexOf("+");
}
}
else if(fuck.contains("+")){
n = l1.indexOf("+");
v = fuck1.indexOf("+");
}
else if(fuck.contains("-")){
n = l1.indexOf("-");
v = fuck1.indexOf("-");
}
String smallequation1 = l0[n]+l1.charAt(n)+l0[n+1];
String result2 = suanshi(smallequation1);//获得结果
int be1 = v - l0[n].length(), end1 =v+l0[n+1].length();
String equation2 = str.substring(be1, end1+1);
String u1 = str.replaceAll(equation2, result2);
return u1;
}
return a3;
}
public static String Sanjiao(String str) {// 处理三角函数
String a4 = str;
if(str.contains("sin")||str.contains("cos")||str.contains("tan")||str.contains("sqrt")){//判断是否含有
String[] r0 = str.split("[*/+-]");
String r1 =str.replaceAll("\\d","").replace(".","");
int j=0;
while(j<r1.length()+1){
double value =0.0d;
if(r0[j].contains("sin")){
String t0 =r0[j].substring(3);
double s0 = Double.parseDouble(t0);
value = Math.sin(s0);
}
else if(r0[j].contains("cos")){
String t0 =r0[j].substring(3);
double s0 = Double.parseDouble(t0);
value = Math.cos(s0);
}
else if(r0[j].contains("tan")){
String t0 =r0[j].substring(3);
double s0 = Double.parseDouble(t0);
value = Math.tan(s0);
}
else if(r0[j].contains("sqrt")){
String t0 =r0[j].substring(4);
double s0 = Double.parseDouble(t0);
value = Math.sqrt(s0);
}
j++;
String f ="("+ value+")";
String equation3 =str.replaceAll(r0[j], f);
return equation3;
}
}return a4;
}
public static String suanshi(String str){
double c =0.0d;
String[] w0;
String ko;
if(str.contains("+")){
w0 =str.split("+");
double g1 =Double.parseDouble(w0[0]);
double g2 =Double.parseDouble(w0[1]);
c =g1 +g2;
}
else if(str.contains("-")){
char first = str.charAt(0);
if(first == '-'){
ko = "0" + str;
w0 = ko.split("-");
double g0 =Double.parseDouble(w0[0]);
double g1 =Double.parseDouble(w0[1]);
double g2 =Double.parseDouble(w0[2]);
c =g0 - g1 - g2;
}
else{
w0 =str.split("-");
double g1 =Double.parseDouble(w0[0]);
double g2 =Double.parseDouble(w0[1]);
c =g1 -g2;}
}
else if(str.contains("*")){
w0 =str.split("[*]");
double g1 =Double.parseDouble(w0[0]);
double g2 =Double.parseDouble(w0[1]);
c =g1 *g2;
}
else if(str.contains("/")){
w0 =str.split("/");
double g1 =Double.parseDouble(w0[0]);
double g2 =Double.parseDouble(w0[1]);
c =g1 /g2;
}
String d = "(" + c + ")";
return d;
}
public static double parse(String str) {// 得到结果
String yuchuli =A(str);
String wukohao = removekuohao(yuchuli);
String w = Sanjiao(wukohao);
String w1 = removekuohao(w);
String w2 = Chengchu(w1);
String w3 =removekuohao(w2);
String w4 =Jiajian(w3);
String w5 =removekuohao (w4);
double o =Double.parseDouble(w5);
return o;
}
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
while (input.hasNextLine()) {
double result = parse(input.nextLine());
System.out.println(String.valueOf(Math.round(result)));
}
input.close();
}
}
运行结果及报错内容
1.2-3.5*5.2-13.7
Exception in thread "main" java.lang.NumberFormatException: For input string: "2.3)"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:651)
at TestMathExper_122090080.suanshi(TestMathExper_122090080.java:254)
at TestMathExper_122090080.Jiajian(TestMathExper_122090080.java:185)
at TestMathExper_122090080.Jiajian(TestMathExper_122090080.java:189)
at TestMathExper_122090080.parse(TestMathExper_122090080.java:282)
at TestMathExper_122090080.main(TestMathExper_122090080.java:293)
-3+4/ 2.5+3.7
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
at TestMathExper_122090080.suanshi(TestMathExper_122090080.java:250)
at TestMathExper_122090080.Jiajian(TestMathExper_122090080.java:186)
at TestMathExper_122090080.parse(TestMathExper_122090080.java:283)
at TestMathExper_122090080.main(TestMathExper_122090080.java:294)