推荐使用JAVA提供的BigDecimal类
学习,练习一下可以
老师布置的作业编写一个求大整数的类:
public class BigNumber{
private String value;
private char[] str,str2;
private int[] num;
private int len;
public BigNumber(String value){
this.value=value;
len=value.length();
}
public void add(BigNumber b){
int leng=b.value.length();
if(len>=leng){
str=new char[len];
str2=new char[leng];
num=new int[len+1];
for(int i=0,j=0;i<len||j<leng;i++,j++){
str[i]=this.value.charAt(i);
if(j>=leng){}
else str2[j]=b.value.charAt(j);
}
}
else {
str=new char[leng];
str2=new char[len];
num=new int[leng+1];
for(int i=0,j=0;i<leng||j<len;i++,j++){
str[i]=b.value.charAt(i);
if(j>=len){}
else str2[j]=this.value.charAt(j);
}
}
int count=0;
for(int i=str.length-1,j=str2.length-1,k=str.length;j>=0||i>=0;i--,j--,k--){
if(j<0){
num[k]=(int)(str[i]-'0')+count;
num[0]=1;
}
else{
num[k]=(int)(str[i]-'0')+(int)(str2[j]-'0')+count;
if((i==j)&&(num[k]<10)) num[0]=0;
else num[0]=1;
}
if(num[k]>=10){
num[k]=num[k]-10;
count=1;
}else count=0;
}
for(int i=0;i<=str.length;i++)
System.out.print(num[i]);
}
public static void main(String[] args){
BigNumber a=new BigNumber("2222");
BigNumber b=new BigNumber("8888");
a.add(b);
}
}