Java大数相加

java

如果带‘+’,‘-’ 操作符

/**

* 求超大整数的和

* @param num1

* @param num2

* @return

*/

public static String bigNumberSumBetter(String num1, String num2) {

char sign = '+';

char sign1 = num1.charAt(0);

char sign2 = num2.charAt(0);

String number1 = "";

String number2 = "";

// 去符号位操作

if (sign1 == '-' || sign1 == '+') {

number1 = num1.substring(1);

} else {

sign1 = '+';

number1 = num1;

}

// 去符号位操作

if (sign2 == '-' || sign2 == '+') {

number2 = num2.substring(1);

} else {

sign2 = '+';

number2 = num2;

}

boolean isDig1 = number1.matches("[1-9][0-9]*");

boolean isDig2 = number2.matches("[1-9][0-9]*");

if (!isDig1 || !isDig2) {

throw new NumberFormatException("输入的数据不是正确的格式的整数");

}

//两个数的长度

int length1 = number1.length();

int length2 = number2.length();

int len = length1>=length2? length1+1:length2+1;

StringBuffer number1Buffer = new StringBuffer();

StringBuffer number2Buffer = new StringBuffer();

//扩展数据的长度,使它们的长度一样

if(length1>length2){

for(int i=0; i<length1-length2; i++){

number2Buffer.append("0");

}

}else if(length1<length2){

for(int i=0; i<length2-length1; i++){

number1Buffer.append("0");

}

}

number1Buffer.append(number1);

number2Buffer.append(number2);

char[] chars1 = number1Buffer.reverse().toString().toCharArray();

char[] chars2 = number2Buffer.reverse().toString().toCharArray();

//存储每位相加的结果

int[] result = new int[len];

//同号相加

if(sign1==sign2){

sign = sign1;

for(int i=0; i<len-1; i++){

result[i] = (chars1[i]-'0')+(chars2[i]-'0');

}

// 处理进位

for (int i = 0; i < len; i++) {

if (result[i] >= 10) {

result[i + 1] += result[i] / 10;

result[i] = result[i] % 10;

}

}

}else {

// 拿大的数减去小的数

boolean lager = number1.compareTo(number2)>0 ? true : false;

if (lager) {

sign = sign1;

for (int i = 0; i < len-1; i++) {

result[i] = (chars1[i] - '0') - (chars2[i] - '0');

}

} else {

sign = sign2;

for (int i = 0; i < len-1; i++) {

result[i] = (chars2[i] - '0') - (chars1[i] - '0');

}

}

// 处理借位

for (int i = 0; i < len; i++) {

if (result[i] < 0) {

result[i] += 10;

result[i + 1]--;

}

}

}

// 结果没有进位时的0处理

boolean flag = true;

StringBuffer resultStr = new StringBuffer();

for (int i = result.length - 1; i >= 0; i--) {

if (result[i] == 0 && flag) {

continue;

}

flag = false;

resultStr.append(result[i]);

}

// 符号处理

if (sign == '-') {

return "-" + resultStr.toString();

} else {

return resultStr.toString();

}

}

本题是CVTE二面编程题,首先考虑返回值肯定是一个字符串(int会有溢出可能),并且两个字符串只含数字,不含”+“、”-“这种。

代码如下:

 1 public static String add(String str1, String str2) {

2 if(str1 == null)

3 return str2;

4 if(str2 == null)

5 return str1;

6 StringBuffer s1 = new StringBuffer(str1).reverse();

7 StringBuffer s2 = new StringBuffer(str2).reverse();

8 StringBuffer res = new StringBuffer();

9 int len1 = s1.length();

10 int len2 = s2.length();

11 int len;

12 if(len1 < len2) {

13 len = len2;

14 int count = len2 - len1;

15 while(count-- > 0)

16 s1.append('0');

17 } else {

18 len = len1;

19 int count = len1 - len2;

20 while(count-- > 0)

21 s2.append('0');

22 }

23 int overflow = 0;

24 int num;

25 for(int i = 0; i < len; i++) {

26 num = s1.charAt(i) - '0' + s2.charAt(i) - '0' + overflow;

27 if(num >= 10) {

28 overflow = 1;

29 num -= 10;

30 } else {

31 overflow = 0;

32 }

33 res.append(String.valueOf(num));

34 }

35 if(overflow == 1)

36 res.append(1);

37

38 return res.reverse().toString();

39 }

以上是 Java大数相加 的全部内容, 来源链接: utcz.com/z/393305.html

回到顶部