试题与答案

编写用数组实现不超过200位的大整数的类BigInteger,要求提供大整数的加运算

题型:问答题

题目:

编写用数组实现不超过200位的大整数的类BigInteger,要求提供大整数的加运算。

答案:

参考答案:程序如下(本题解决思路比较多,这是其中之一,代码也可以不用像本程序考虑这么多):
方法一:
class BigInteger
{
final int L=200;//处理不超过200位的大整数运算
char a[]=new char[L],b[]=new char[L],c[]=new char[L];//a,b,c三个字符数组用于存放处理后的加数、被加数及结果
int an,bn;
BigInteger(){}//无参构造函数
BigInteger(char aa[],char bb[])//传两个字符数组作为加数、被加数
{
System. out. println(aa);
System. out. println(bb);
an=aa. length;bn=bb. length;
a=aa;b=bb;//把传来的字符数组分别放入a和b中
int i;
for(i=0;i<a.length/2;i++)//让a数组中的数字逆序,个数在0下标
{
char c=[i];a[i]=a[an-i-1];
a[an-i-1]=c;
}
for(i=0;i<b.length/2;i++)//让b数组中的数字逆序,个数在0下标
{
char c=b[i];b[i]=b[bn-i-1];
b[bn-i-1]=c;
}
}
BigInteger(String aa,String bb)//传两个字符串作为加数、被加数
{
System. out. println(aa+"\n"+bb);
an=aa.length();
bn=bb.length();
int i;//以下是取字符串中内容逆序放入a和b中
for(i=an-1;i>=0;i--)
a[an-1-i]=aa.charAt(i);
for(i=bn-1;i>=0;i--)
b[bn-1-i]=bb.charAt(i);
}
void add(String aa,String bb)//设计了带有两个字符串参数的add成员方法
{
System. out. println(aa+"\n"+ bb);
an=aa.length();
bn=bb.length();
int i;
for(i=an-1;i>=0;i--)
a[an-1-i]=aa. charAt(i);
for(i=bn-1;i>=0;i--)
b[bn-1-i]=bb.charAt(i);
add();
}
void add()//无参的add成员方法,完成大整数加运算
{
int j=bn,i,x,y,z;
if(an<bn)j=an;//找出两个操作数中短的那个数,加运算时以此为准
for(i=0;i<L;i++) c[i]=’0’;//使结果数组c中全为字符0
for(i=0;i<j;i++)//取出a和b中相对应的数字存入x,y中,z=x+y+进位
{
x=(a[i]-48);//字符转数字
y=(b[i]-48);
z=x+y+c[i]-48;
if(z>9)c[i+1]=’1’;//如果需要进位,使c的高下标为字符1
c[i]=(char)(z%10+48);//考虑可能有进位,不能使用c[i]=z+48
}
if(an>bn)//当b中数据运算完时
while(i<an)//把a中剩余的数字全部加到c中
{
c[i]=(char)((int)c[i]+(int)a[i]-48);
if(c[i]>’9’)
{c[i+1]=’1’;c[i]-=10;}
i++;
}
else//当a中数据运算完时
while(i<bn)//把b中剩余的数字全部加到c中
{
c[i]=(char)((int)c[i]+
(int)b[i]-48);
if(c[i]>’9’)
{c[i+1]=’1’;c[i]-=10;}
i++;
}
if(c[i-1]>57)
{c[i]=49;c[i-1]-=10;}//最高位如有进位,需要进位
if(an> bn)
j=an;else j=bn;//找出长的那个数,输出时以此为准
while(c[j]==’0’)j--;//准备逆序输出,去掉多余的前导0
for(i=j;i>=0;i--)System. out. print(c[i]);//逆序输出运算结果
System. out. println(’\n’);
}
}
public class Class38
{
public static void main(String[] args)
{
char a1[]={’9’,’9’,’9’,’2’,’3’,’1’};
char a2[]={’7’,’8’,’1’};
BigInteger B=new BigInteger(a1,a2);//为构造方法传字符数组
B.add();
String s1="9234",s2="789";
(new BigInteger(s1,s2)).add();//为构造方法传字符串
BigInteger C=new BigInteger();
C.add(s1,s2);//为成员方法传字符串
}
}
方法二:
class BigInt
{
final int L=200;
byte a[]=new byte[L],b[]=new byte[L],c[]=new byte[L];
int an,bn;
BigInt()
{
}
BigInt(String aa,String bb)
{
Stringtobyte(aa,bb);
}
void Stringtobyte(String aa,String bb)
{
System. out. print(aa+"+"+bb+"=");
an=aa.length();
bn=bb.length();
byte tempa[]=aa.getBytes();
byte tempb[]=bb.getByLes();
int i;
for(i=an-1;i>=0;i--)
a[an-1-i]=tempa[i];
for(i=bn-1;i>=0;i--)
b[bn-1-i]=tempb[i];
for(i=bn;i<L;i++) b[i]=’0’;
}
void add(String aa,String bb)
{
Stringtobyte(aa,bb);
add();
}
void add()
{
int i,x,y,z;
for(i=0;i<L;i++) c[i]=’0’;
for(i=0;i<L;i++)
{
x=(a[i]-48);
y=(b[i]-48);
z=x+y+(c[i]-48);
if(z>9) c[i+1]=’1’;
c[i]=(byte) (z%10+48);
i=L-1;
while(c[i]==’0’) i--;
for(;i>=0;i--)
System. out. print((char) c[i]);
System. out. println(’\n’);
{
{
public class Class382
}
public static void main(String[] args)
{
(new BigInt("9234","789")).add();
(new BigInt()).add("99999999999999999","222222222222222222222");
}
}
方法二输出结果:
9234+789=111+222=333
99999999999999999+222222222222222222222=222322222222222222221

试题推荐
微信公众账号搜索答案