问题遇到的现象和发生背景
我的解答思路和尝试过的方法 试着改进下去括号的部分和parse依旧不能得到理想的结果
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;
//分割字符串
if(str.contains("*")||str.contains("/")){
String[] b0 = str.split("[*/+-]");//数字串
String b1 =str.replaceAll("\\d","").replace(".","");
int x = 0,y =0,z=0,k =0;
if(str.contains("*")&&str.contains("/")){
x = b1.indexOf("*");
y = b1.indexOf("/");
if(x > y){//除号在前
z = b1.indexOf("/");
k = str.indexOf("/");
}
else if(y > x){//乘号在前
z = b1.indexOf("*");
k = str.indexOf("*");
}
}
else if(str.contains("*")){
z = b1.indexOf("*");
k = str.indexOf("*");
}
else if(str.contains("/")){
z = b1.indexOf("/");
k = str.indexOf("/");
}
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 = str.substring(be, end);
String u = str.replaceAll(equation1, result1);
String u1 =u.toString();
return Chengchu(u1);
}
return a2;
}
public static String Jiajian(String str) {// 处理加减
String a3 = str;
if(str.contains("+")||str.contains("-")){
String[] l0 = str.split("[*/+-]");//数字串
String l1 =str.replaceAll("\\d","").replace(".","");
int l = 0,m =0,n=0,v =0;
if(str.contains("+")&&str.contains("-")){
l = l1.indexOf("+");
m = l1.indexOf("-");
if(l > m){//减号在前
n = m;
v = str.indexOf("-");
}
else if(m > l){//乘号在前
n = l;
v = str.indexOf("+");
}
}
else if(str.contains("+")){
n = l1.indexOf("+");
v = str.indexOf("+");
}
else if(str.contains("-")){
n = l1.indexOf("-");
v = str.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 Chengchu(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 Sanjiao(equation3);
}
}return a4;
}
public static String suanshi(String str){
double c =0.0d;
String[] w0;
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;
}
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 w0 = Sanjiao(wukohao);
String w1 = Chengchu(w0);
String w2 =removekuohao(w1);
String w3 =Jiajian(w2);
String w4 =removekuohao (w3);
double o =Double.parseDouble(w4);
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();
}
}
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;
//分割字符串
int x = 0,y =0,z=0,k =0,x0 =0,y0 =0;
char ai;
String[] b0 = str.split("[*/+-]");//数字串
String b1 =str.replaceAll("\\d","").replace(".","");
if(str.contains("*")||str.contains("/")){
if(str.contains("*")&&str.contains("/")){
x = b1.indexOf("*");
y = b1.indexOf("/");
x0 =str.indexOf("*");
y0 =str.indexOf("/");
if(x > y){//除号在前
ai = str.charAt(y0+1);
if(ai == '-'){
z=b1.indexOf("/");
k =str.indexOf("/");
b0[z+1] ="-"+b0[z+1];
}
else{z = b1.indexOf("/");
k = str.indexOf("/");}
}
else if(y > x){//乘号在前
ai = str.charAt(x0+1);
if(ai == '-'){
z=b1.indexOf("*");
k =str.indexOf("*");
b0[z+1] ="-"+b0[z+1];
}
else{z = b1.indexOf("*");
k = str.indexOf("*"); }
}
}
else if(str.contains("*")){
ai = str.charAt(x0+1);
if(ai == '-'){
z=b1.indexOf("*");
k =str.indexOf("*");
b0[z+1] ="-"+b0[z+1];
}
else{z = b1.indexOf("*");
k = str.indexOf("*");}
}
else if(str.contains("/")){
ai = str.charAt(y0+1);
if(ai == '-'){
z=b1.indexOf("/");
k =str.indexOf("/");
b0[z+1] ="-"+b0[z+1];
}
else{z = b1.indexOf("/");
k = str.indexOf("/");}
}
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 = str.substring(be, end);
String u = str.replaceAll(equation1, result1);
String u1 =u.toString();
return Chengchu(u1);
}
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[] l0 = fuck.split("[*/+-]");//数字串
String l1 =fuck.replaceAll("\\d","").replace(".","");
int l = 0,m =0,n=0,v =0;
if(fuck.contains("+")&&fuck.contains("-")){
l = l1.indexOf("+");
m = l1.indexOf("-");
if(l > m){//减号在前
n = m;
v = fuck.indexOf("-");
}
else if(m > l){//乘号在前
n = l;
v = fuck.indexOf("+");
}
}
else if(fuck.contains("+")){
n = l1.indexOf("+");
v = fuck.indexOf("+");
}
else if(fuck.contains("-")){
n = l1.indexOf("-");
v = fuck.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 Chengchu(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 Sanjiao(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 w0 = Sanjiao(wukohao);
String w1 = Chengchu(w0);
String w2 =removekuohao(w1);
String w3 =Jiajian(w2);
String w4 =removekuohao (w3);
double o =Double.parseDouble(w4);
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();
}
}
```我对上一次出现的问题进行了改进,但同时我发现我忽视了乘除号后可能出现负数的情况,我又做出了更改,然后。。。。。。又报错了。
我需要帮助,我知道这个代码是太长了,所以真的麻烦了(万分感谢)
报错信息
at TestMathExper_122090080.Chengchu(TestMathExper_122090080.java:141)
```