使用字符串实现大数的相加,包含负数,下面直接上代码,转载请注明出处:
1 /** 2 * sum 3 */ 4 public class sum { 5 6 public static String add(String n1, String n2) { 7 String res ; 8 if(n1.charAt(0) != '-' && n2.charAt(0) != '-') { 9 res = getSum(n1,n2); 10 }else if(n1.charAt(0) == '-' && n2.charAt(0) == '-') 11 res ="-"+ getSum(n1.substring(1), n2.substring(1)); 12 else { 13 res = getMinus(n1,n2); 14 } 15 //System.out.println(res); 16 return res; 17 } 18 19 public static String getSum(String n1, String n2){ 20 int l1 = n1.length(); 21 int l2 = n2.length(); 22 char[] res; 23 boolean flag = false; 24 25 //根据长度创建存储结果的字符数组,因为可能存在最高位想加进位的情况,长度+1 26 if(l1 >= l2) { 27 res = new char[l1+1]; 28 flag = true; 29 } else{ 30 res = new char[l2+1]; 31 } 32 33 int pos = res.length - 1; 34 int takeOver = 0; 35 l1 --; 36 l2 --; 37 //从最低位开始相加,直到其中一个已经求和结束 38 while(l1 >= 0 && l2 >= 0){ 39 int sum = n1.charAt(l1) - '0' + n2.charAt(l2) - '0' + takeOver; 40 takeOver = 0; 41 if(sum >= 10){ 42 res[pos] = (char) (sum - 10+ '0'); 43 takeOver = 1; 44 } else { 45 res[pos] = (char) (sum+ '0'); 46 } 47 l1 --; 48 l2 --; 49 pos --; 50 } 51 52 //把剩下的字符串复制到结果res,注意可能出现的tackOver进位存在 53 if (flag) { 54 while (l1 >= 0 && pos >= 0) { 55 int sum = n1.charAt(l1) - '0' + takeOver; 56 takeOver = 0; 57 if(sum >= 10){ 58 res[pos] = (char) (sum - 10 + '0'); 59 takeOver = 1; 60 } else { 61 res[pos] = (char) (sum + '0'); 62 } 63 l1 --; 64 pos --; 65 } 66 } else { 67 while (l2 >= 0 && pos >= 0) { 68 int sum = n2.charAt(l1) - '0' + takeOver; 69 takeOver = 0; 70 if(sum >= 10){ 71 res[pos] = (char) (sum - 10 + '0'); 72 takeOver = 1; 73 } else { 74 res[pos] = (char) (sum+ '0'); 75 } 76 l2 --; 77 pos --; 78 } 79 } 80 //如果两个字符串的长度相等,那么就需要考虑向最高位进位的情况,这里要加上进位 81 res[pos] = (char) ('0' + takeOver); 82 83 String str = new String(res); 84 //对开始为0的情况需要处理 85 if (str.charAt(0) == '0') { 86 str = str.substring(1); 87 } 88 return str; 89 } 90 91 public static String getMinus( String n1, String n2){ 92 boolean n1Nagative = false; // n1 is - 93 boolean n2Nagative = false; // n2 is - 94 boolean isLarger = false; 95 boolean isNagative = false; 96 String res = null; 97 //判断n1是否为负数,并取出数值部分 98 if(n1.charAt(0) == '-') { 99 n1Nagative = true; 100 n1 = n1.substring(1); 101 } 102 //判断n2是否为负数,并取出数值部分 103 if (n2.charAt(0) == '-') { 104 n2Nagative = true; 105 n2 = n2.substring(1); 106 } 107 if(n1Nagative){ 108 //判断n1是否大于n2,如果大于,那么结果为负值 109 isLarger = isLarger(n1,n2); 110 if (isLarger) { 111 isNagative = true; 112 res = bigDigitalSub(n1, n2);//实现n1 - n2 113 } else { 114 isNagative = false; //此时结果为正,n2 > n1 115 res = bigDigitalSub(n2, n1); //实现n2 - n1 116 } 117 } 118 if(n2Nagative) { 119 //判断n2是否大于n1,如果大于,那么结果为负值 120 isLarger = isLarger(n2,n1); 121 if (isLarger) { //n2 > n1; 122 isNagative = true; 123 res = bigDigitalSub(n2, n1);//实现n2 - n1 124 } else { 125 isNagative = false;//此时结果为正,n1 > n2 126 res = bigDigitalSub(n1, n2);//实现n1 - n2 127 } 128 } 129 return isNagative?"-" + res:res; 130 } 131 public static String bigDigitalSub(String a, String b){ 132 //翻转字符串并转化成数组 133 char[] aArray = new StringBuilder(a).reverse().toString().toCharArray(); 134 char[] bArray = new StringBuilder(b).reverse().toString().toCharArray(); 135 int aLength = aArray.length; 136 int bLength = bArray.length; 137 //找到最大的位数,两个整数的差的位数小于等于两个整数中的最大位数 138 int maxLength = aLength > bLength ? aLength : bLength; 139 int[] result = new int[maxLength]; 140 //开始计算结果集 141 for (int i = 0; i < maxLength; i++) { 142 int aInt = i < aLength ? aArray[i] - '0' : 0; 143 int bInt = i < bLength ? bArray[i] - '0' : 0; 144 // if (sign == '-') result[i] = bInt - aInt; 145 // else result[i] = aInt - bInt; 146 result[i] = aInt - bInt; 147 } 148 //处理结果集,如果结果集中的某一位小于0,则向高位借位,然后将本位加10 149 for (int i = 0; i < maxLength - 1; i++) { 150 if (result[i] < 0) { 151 result[i + 1] -= 1; 152 result[i] += 10; 153 } 154 } 155 //处理结果集,转化成真正结果 156 StringBuffer realResult = new StringBuffer(); 157 boolean isBeginning = true; 158 for (int i = maxLength - 1; i >= 0; i--) { 159 if (result[i] == 0 && isBeginning) continue; 160 else isBeginning = false; 161 realResult.append(result[i]); 162 } 163 if (realResult.toString().equals("")) realResult.append('0'); 164 return realResult.toString(); 165 } 166 167 public static boolean isLarger(String n1, String n2){ 168 boolean res= false; 169 if (n1.length() > n2.length()) { 170 res = true; // n1 > n2 171 } else if (n1.length() == n2.length()) { 172 for(int i = 0; i < n1.length(); i++){ 173 if (n1.charAt(i) < n2.charAt(i)) { 174 res = false; //n2 > n1 175 break; 176 } 177 } 178 res = true; 179 } else if(n1.length() < n2.length()) { 180 res = false; // n2 > n1; 181 } 182 return res; 183 } 184 public static void main(String[] args) { 185 String n1 = "12345"; 186 String n2 = "-112300"; 187 System.out.println(add(n1, n2)); 188 } 189 }