今天时10月14号,星期三
特别尝试了一下四则运算中分数和小数的计算……先说结论——果然不行啊,字符串读入读小数会报一个叫NullPointerException的奇妙异常
解决了好一阵也没能彻底解决……啊,放弃!
欸……话说下次的四则运算会不会就是小数了啊……
另外虽然是失败作品……不过也把代码贴上来吧
public static String getzongde(String str){ String kuohao=getkuohao(str); String chengchu=getchengchu(kuohao); String jiajian=getjiajian(chengchu); return jiajian; } public static String getkuohao(String str){ String newstr=str; if(newstr.contains("(")){ int a=str.lastIndexOf("("); String str1=str.substring(a,str.length()); int b =str1.indexOf(")"); String str2=str1.substring(1,b); String chengchu=getchengchu(str2); String jiajian=getjiajian(chengchu); StringBuffer s=new StringBuffer(str); newstr=s.replace(a, a+b+1, jiajian).toString(); return getkuohao(newstr); } return newstr; } public static String getchengchu(String str){ String newstr=str; if(str.contains("*")||str.contains("/")){ String str2[]=str.split("[*/+-]");// String str3=str.replaceAll("\\d+","").replaceAll("\\.",""); int a=0,b=0,c=0,star=0; if(str3.contains("*")&&str3.contains("/")){ a=str3.indexOf("*"); b=str3.indexOf("/"); if(a>b){ c=b; star=str.indexOf("/"); }else{ c=a; star=str.indexOf("*"); } }else if(str3.contains("*")){ c=str3.indexOf("*"); star=str.indexOf("*"); }else{ c=str3.indexOf("/"); star=str.indexOf("/"); } String num=getsuanshi(str2[c]+str3.charAt(c)+str2[c+1]); StringBuffer s=new StringBuffer(str); int kaishi=star-str2[c].length(),jieshu=star+str2[c+1].length()+1; StringBuffer strs=s.replace(kaishi,jieshu ,num); newstr=strs.toString(); return getchengchu(newstr); } return newstr; } public static String getjiajian(String str){ String newstr=str; if(str.contains("+")||str.contains("-")){ String str2[]=str.split("[*/+-]"); String str3=str.replaceAll("\\d+","").replaceAll("\\.",""); int a=0,b=0,c=0,star=0; if(str3.contains("+")&&str3.contains("-")){ a=str3.indexOf("+"); b=str3.indexOf("-"); if(a>b){ c=b; star=str.indexOf("-"); }else{ c=a; star=str.indexOf("+"); } }else if(str3.contains("+")){ c=str3.indexOf("+"); star=str.indexOf("+"); }else{ c=str3.indexOf("-"); star=str.indexOf("-"); } String num=getsuanshi(str2[c]+str3.charAt(c)+str2[c+1]); StringBuffer s=new StringBuffer(str); int kaishi=star-str2[c].length(),jieshu=star+str2[c+1].length()+1; StringBuffer strs=s.replace(kaishi,jieshu ,num); newstr=strs.toString(); return getjiajian(newstr); } return newstr; } public static String getsuanshi(String str){ double c=0.0; String str2[]; if(str.contains("+")){ str2=str.split("[+]"); c=getjia(Double.parseDouble(str2[0]),Double.parseDouble(str2[1])); }else if(str.contains("-")){ str2=str.split("[-]"); c=getjian(Double.parseDouble(str2[0]),Double.parseDouble(str2[1])); }else if(str.contains("*")){ str2=str.split("[*]"); c=getcheng(Double.parseDouble(str2[0]),Double.parseDouble(str2[1])); }else if(str.contains("/")){ str2=str.split("[/]"); c=getchu(Double.parseDouble(str2[0]),Double.parseDouble(str2[1])); } NumberFormat format=NumberFormat.getInstance(); format.setMaximumFractionDigits(30); format.setMinimumFractionDigits(0); format.setGroupingUsed(false); return format.format(c); } public static Double getjia(double a,double b){ double c=a+b; return c; } public static Double getjian(double a,double b){ double c=a-b; return c; } public static Double getcheng(double a,double b){ double c=a*b; return c; } public static Double getchu(double a,double b){ double c=a/b; return c; }
好,今天就是这样啦