使用Stack堆栈集合大数据运算
package com.sta.to;
import java.util.Iterator;
import java.util.Stack;
public class DaMax {
public void jiaFa(String value1, String value2) {
/**
* 更多资料欢迎浏览凯哥学堂官网:http://kaige123.com
* @author 小沫
*/
char[] c1 = value1.toCharArray();
char[] c2 = value2.toCharArray();
Stack numvalue1 = new Stack();
Stack numvalue2 = new Stack();
for (char c : c1) {
numvalue1.push(c);
}
for (char c : c2) {
numvalue2.push(c);
}
StringBuffer buffer = new StringBuffer();
int jw = 0;
int count = Math.max(c1.length, c2.length);
for (int i = 0; i < count; i++) {
int num1;
int num2;
try {
num1 = Integer.parseInt(numvalue1.pop().toString());
} catch (Exception e) {
num1 = 0;
}
try {
num2 = Integer.parseInt(numvalue2.pop().toString());
} catch (Exception e) {
num2 = 0;
}
int sum = num1 + num2 + jw;
if (sum >= 10) {
jw = 1;
buffer.append(sum % 10);
} else {
jw = 0;
buffer.append(sum);
}
}
if (jw == 1) {
buffer.append(1);
}
buffer.reverse();
System.out.println(buffer);
}
}
测试类:
package com.sta.to;
import java.nio.Buffer;
public class Test {
public static void main(String[] args) {
DaMax ys = new DaMax();
ys.jiaFa("10", "5");
ys.jiaFa("15", "50");
ys.jiaFa("999999999999999", "1");
ys.jiaFa("99", "1");
ys.jiaFa("80", "80");
ys.jiaFa("8", "7");
}
}
|