赞
踩
通过实现字符串的加减乘除方法,可以方便地对字符串进行各种数学运算操作,提高了字符串处理的灵活性和效率。同时,能够实现大数字和精度高的计算。
public class EString { //大整数相加 public static String add(String a,String b){ int base=10; int i=a.length()-1,j=b.length()-1; //处理进位 int add=0; //通过StringBuilder处理,主要它有反转的这个函数 StringBuilder ans=new StringBuilder(); //此处是个大条件,只有一方不满足才会退出 while(i>=0||j>=0||add!=0){ //转换为字母通过-‘0’ int x=i>=0?a.charAt(i)-'0':0; int y=j>=0?b.charAt(j)-'0':0; int result=x+y+add; //添加值到每个字符串的最后一个 ans.append(result%base); //添加进位给予到下一个值 add=result/base; i--; j--; } //字符串的反转 ans.reverse(); return ans.toString(); } //大整数相减 public static String del(String a,String b){ int i=a.length()-1,j=b.length()-1; //处理进位 int add=0; //通过StringBuilder处理,主要它有反转的这个函数 StringBuilder ans=new StringBuilder(); //此处是个大条件,只有一方不满足才会退出 while(i>=0||j>=0||add!=0){ //转换为字母通过-‘0’ int x=i>=0?a.charAt(i)-'0':0; int y=j>=0?b.charAt(j)-'0':0; int result=x-y-add; if(result<0) { result+=10; add = 1; }else{ add=0; } //添加值到每个字符串的最后一个 ans.append(result); //添加进位给予到下一个值 i--; j--; if(i<0&&j<0&&add!=0) { return "-"+del(b,a); } } //字符串的反转 ans.reverse(); return ans.toString(); } //大整数相乘 public static String mmm(String a,String b){ int base=10; int i=a.length()-1,j=b.length()-1; //处理进位 int add=0; //通过StringBuilder处理,主要它有反转的这个函数 String he; String he1="0"; String he2=""; String z=""; int kl = j; int kh = i; //此处是个大条件,只有一方不满足才会退出 while(i>=0||j>=0||add!=0){ //转换为字母通过-‘0’ int x=i>=0?a.charAt(i)-'0':0; int y=j>=0?b.charAt(j)-'0':0; int result=x*y+add; //添加值到每个字符串的最后一个 he2=String.valueOf(result%base)+he2; //添加进位给予到下一个值 add=result/base; i--; if(i<0&&j>=0&&add==0) { i=kh; for (int j1=j;j1<kl;j1++) { z=z+"0"; } he=he2; he = he+z; z=""; he1=addx(he1,he); j--; he2=""; } } return he1; } //大整数相除 public static String ccc(String a,String b){ if (sw2(b).equals(addx(b, b))) { return b; } int i=a.length()-1,j=b.length()-1; //处理进位 //通过StringBuilder处理,主要它有反转的这个函数 String he=""; String he1=""; String he2=""; int c=0; int q=0; //此处是个大条件,只有一方不满足才会退出 boolean zz = false; boolean zz1 = true; boolean d3 = false; int d = i - j; while (true) { if (d < 0) { he = "0."; for (int k = i; k < j; k++) { he1 = a + "0"; he = he + "0"; zz = true; } if (zz) { he = he.substring(0, he.length() - 1); } } else { he1 = a.substring(0, j+1); } while (true) { while (true) { String z1 = delx(he1, b); if (z1.indexOf("-") == -1) { break; } int x = d > 0 ? a.charAt(j+1) - '0' : 0; if (d >= 0) { d--; j++; d3=true; } String tt1 = String.valueOf(x); he1 = he1 + x; if(d==-1&&d3) { if(he.length()==0) { he=he+"0"; } he=he+"."; d--; } if (dd(he) > 20) { return he; } he = he + "0"; if(!zz) { zz1 = false; } } if (!zz1) { he = he.substring(0, he.length() - 1); zz = false; } while (true) { he2 = he1; he1 = delx(he1, b); q = he1.indexOf("-"); if (q == -1) { c++; } else { he = he + String.valueOf(c); he1 = sw(he2); c = 0; if (dd(he) > 20) { return he; } break; } } } } } //大数相加 public static String addx(String a,String b){ if(a.indexOf("-")!=-1) { String aj =a.replace("-",""); return sw2(delx(b,aj)); } if(b.indexOf("-")!=-1) { String bj =b.replace("-",""); return sw2(delx(a,bj)); } int i=a.length()-1,j=b.length()-1; int i1=a.indexOf("."); int j1=b.indexOf("."); String a1; String b1; if(i1==-1) { i1=i; } if(j1==-1) { j1=j; } int l = i-i1 >j-j1 ? i-i1:j-j1; if(i-i1==j-j1) { a1=a.replace(".", ""); b1=b.replace(".", ""); }else { if (l == i - i1) { a1=a.replace(".", ""); int km = i - i1 - j + j1; for (int k = 0; k < km; k++) { b = b + "0"; } b1=b.replace(".", ""); } else { b1=b.replace(".", ""); int km = -i + i1 +j - j1; for (int k = 0; k < km; k++) { a = a + "0"; } a1=a.replace(".", ""); } } String c = add(a1,b1); StringBuffer c1=new StringBuffer(c); if(l!=0) { c1.insert(c.length() - l, "."); } return sw2(c1.toString()); } //大数相减 public static String delx(String a,String b){ if(a.indexOf("-")!=-1) { String aj =a.replace("-",""); return sw2(delx("0",addx(aj,b))); } if(b.indexOf("-")!=-1) { String bj =b.replace("-",""); return sw2(addx(a,bj)); } int i=a.length()-1,j=b.length()-1; int i1=a.indexOf("."); int j1=b.indexOf("."); String a1; String b1; if(i1==-1) { i1=i; } if(j1==-1) { j1=j; } int l = i-i1 >j-j1 ? i-i1:j-j1; if(i-i1==j-j1) { a1=a.replace(".", ""); b1=b.replace(".", ""); }else { if (l == i - i1) { a1=a.replace(".", ""); int km = i - i1 - j + j1; for (int k = 0; k < km; k++) { b = b + "0"; } b1=b.replace(".", ""); } else { b1=b.replace(".", ""); int km = -i + i1 +j - j1; for (int k = 0; k < km; k++) { a = a + "0"; } a1=a.replace(".", ""); } } String c = del(a1,b1); StringBuffer c1=new StringBuffer(c); if(l!=0) { c1.insert(c.length() - l, "."); } return sw2(c1.toString()); } //大数相乘 public static String mmmx(String a,String b){ if(a.indexOf("-")!=-1) { String aj =a.replace("-",""); return sw2(fu("-"+mmmx(aj,b))); } if(b.indexOf("-")!=-1) { String bj =b.replace("-",""); return sw2(fu("-"+mmmx(a,bj))); } int base=10; int i=a.length()-1,j=b.length()-1; int i1=a.indexOf("."); int j1=b.indexOf("."); String a1; String b1; if(i1==-1) { i1=i; } if(j1==-1) { j1=j; } int l = i-i1 >j-j1 ? i-i1:j-j1; if(i-i1==j-j1) { a1=a.replace(".", ""); b1=b.replace(".", ""); }else { if (l == i - i1) { a1=a.replace(".", ""); int km = i - i1 - j + j1; for (int k = 0; k < km; k++) { b = b + "0"; } b1=b.replace(".", ""); } else { b1=b.replace(".", ""); int km = -i + i1 +j - j1; for (int k = 0; k < km; k++) { a = a + "0"; } a1=a.replace(".", ""); } } String c = mmm(a1,b1); StringBuffer c1=new StringBuffer(c); if(l!=0) { c1.insert(c.length() - 2*l, "."); } return sw2(fu(c1.toString())); } //大数相除 public static String cccx(String a,String b){ if(a.indexOf("-")!=-1) { String aj =a.replace("-",""); return sw2(fu("-"+cccx(aj,b))); } if(b.indexOf("-")!=-1) { String bj =b.replace("-",""); return sw2(fu("-"+cccx(a,bj))); } int base=10; int i=a.length()-1,j=b.length()-1; int i1=a.indexOf("."); int j1=b.indexOf("."); String a1; String b1; if(i1==-1) { i1=i; } if(j1==-1) { j1=j; } int l = i-i1 >j-j1 ? i-i1:j-j1; if(i-i1==j-j1) { a1=a.replace(".", ""); b1=b.replace(".", ""); }else { if (l == i - i1) { a1=a.replace(".", ""); int km = i - i1 - j + j1; for (int k = 0; k < km; k++) { b = b + "0"; } b1=b.replace(".", ""); } else { b1=b.replace(".", ""); int km = -i + i1 +j - j1; for (int k = 0; k < km; k++) { a = a + "0"; } a1=a.replace(".", ""); } } String c = ccc(a1,b1); return sw2(fu(c)); } //符号显示 public static String fu(String a){ int k = findStrCount(a,"-",0); if(k%2==0) { return a.replace("-",""); }else{ return "-"+a.replace("-",""); } } //小数字点位置 public static int dd(String a){ int l=a.length()-1; int k = a.indexOf("."); if(k==-1) { k=l; } int l1=l-k; return l1; } //包含字符串数 public static int findStrCount(String oriStr, String findStr, int count) { if (oriStr.contains(findStr)) { count++; count = findStrCount(oriStr.substring(oriStr.indexOf(findStr) + findStr.length()), findStr, count); } return count; } //规范1 public static String sw(String a) { if(a.indexOf("-")!=-1){ return "-"+sw(a.replace("-","")); } int i = a.length() - 1; StringBuilder ans = new StringBuilder(); int k = 0; int ha = 0; while (k <= i) { //转换为字母通过-‘0’ char x = k <= i ? a.charAt(k) : '0'; if (x != '0' || ha == 1) { ans.append(x); ha = 1; } k++; } String q=ans.toString(); if(q.length()!=0) { char t = q.charAt(0); if (t == '.') { q = "0" + q; } } if(q.length()==0||q.equals("-0")||q.equals("-")) { q="0"; } return q; } //规范2 public static String sw1(String a) { if(a.indexOf("-")!=-1){ return "-"+sw1(a.replace("-","")); } if(a.indexOf(".")==-1){ return a; }else { int i = a.length() - 1; StringBuilder ans = new StringBuilder(); int ha = 0; while (i >= 0) { //转换为字母通过-‘0’ char x = i >= 0 ? a.charAt(i): '0'; if (x != '0' || ha == 1) { ans.append(x); ha = 1; } i--; } String q=ans.reverse().toString(); if(q.length()!=0) { char t = q.charAt(q.length() - 1); if (t == '.') { q = q.substring(0, q.length() - 1); } } if(q.length()==0||q.equals("-0")||q.equals("-")) { q="0"; } return q; } } //规范3 public static String sw2(String a) { String q= sw(sw1(a)); if(q.length()==0||q.equals("-0")||q.equals("-")) { q="0"; } return q; } }
public static void main(String[] args) {
System.out.println("数字:");
System.out.println(addx("12.345678","12.345678"));
System.out.println(delx("99.123456","12.345678"));
System.out.println(mmmx("12.345678","12.345678"));
System.out.println(cccx("99.123456","12.345678"));
}
输出:
数字:
24.691356
86.777778
152.415765279684
8.029000594378048738999
本方法经过本人多次分析与测试,如有更好的方法或者发现错误的地方欢迎大家评论与指正!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。