一个实现长整型数相加减的小程序
2008-01-05 18:49:42 来源:WEB开发网联合国科教文组织要统计人口,显然一般的int不能满足需要,因此要求定义一个BigInteger的新类,实现超长整型数的加减乘除四则运算,我用转化为数组的方法实现了加、减,有点小bug,而且不知道怎么实现乘除:(
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//6.7.11
public class BigInteger//定义长整型的类
{
PRivate static final int add1Max=100;//可计算最长位数
private static final int add2Max=100;//同上
private char[] add1 = new char[add1Max];
private char[] add2 = new char[add1Max];
private int len1;
private int len2;
public void setAdd1() throws IOException
{
int i=0;
String sLine1;
System.out.print("Please input add1 : ");//输入第一个长整数
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
sLine1=in.readLine();
for(;i<sLine1.length();i++)
add1[i]=sLine1.charAt(i);
for(i=0;i<add1Max;i++)//len1=add1.length()
{
if(add1[i]!='\0')
len1++;
}
}
public void setAdd2() throws IOException
{
int i=0;
String sLine2;
System.out.print("Please input add2 : ");//输入第二个长整数
BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
sLine2 = in2.readLine();
for(i=0;i<sLine2.length();i++)
add2[i]=sLine2.charAt(i);
for(i=0;i<add2Max;i++)//len2=add2.length()
{
if(add2[i]!='\0')
len2++;
}
}
public char[] getAdd1()
{
return add1;
}
public char[] getAdd2()
{
return add2;
}
public int getLen1()
{
return len1;
}
public int getLen2()
{
return len2;
}
public void add(char[] add1,char[] add2)//BigInteger相加方法
{
int len = Math.max(len1,len2);
int i;
char[] temp1 = new char[len];
char[] temp2 = new char[len];
char[] result = new char[len+1];
for(i=0;i<len1;i++)
temp1[len-1-i]=add1[len1-1-i];
for(i=0;i<len2;i++)
temp2[len-1-i]=add2[len2-1-i];
int m=0;
for(i=0;i<len;i++)//相加
{
if(temp1[len-1-i]!=0)
temp1[len-1-i]-=48;
if(temp2[len-1-i]!=0)
temp2[len-1-i]-=48;
m=temp1[len-1-i]+temp2[len-1-i];
if(m>=10)
{
m-=10;
result[len-i]+=m;
result[len-1-i]+=1;
}
else result[len-i]+=m;
}
System.out.print("add1 + add2 = ");//输出相加结果
i=0;
if(result[0]==0)
i=1;
for(;i<len+1;i++)
System.out.print(Integer.toString(result[i]));
}
public void sub(char[]sub1,char[]sub2)//BigInteger相减方法
{
int len = Math.max(len1,len2);
int i;
char[] temp1 = new char[len];
char[] temp2 = new char[len];
char[] result = new char[len+1];
if(len1>len2)
{
for(i=0;i<len1;i++)
temp1[len-1-i]=sub1[len1-1-i];
for(i=0;i<len2;i++)
temp2[len-1-i]=sub2[len2-1-i];
}else//保证减数大于被减数
{
for(i=0;i<len1;i++)
temp2[len-1-i]=sub1[len1-1-i];
for(i=0;i<len2;i++)
temp1[len-1-i]=sub2[len2-1-i];
}
int m=0;
for(i=0;i<len;i++)//相减
{
if(temp1[len-1-i]!=0)
temp1[len-1-i]-=48;
if(temp2[len-1-i]!=0)
temp2[len-1-i]-=48;
m=temp1[len-1-i]-temp2[len-1-i];
if(m<0)//假如计算的那一位的减数小于被减数那么相前一位借10
{
m+=10;
result[len-i]+=m;
temp1[len-1-i]-=1;
}
else result[len-i]+=m;
}
System.out.print("add1 - add2 = ");//输出减法结果
i=0;
while(result[i]==0)
i++;
if((len2>len1)(len1==len2&&sub1[0]<sub2[0]))
System.out.print("-");
for(;i<len+1;i++)
System.out.print(Integer.toString(result[i]));
}
public static void main(String[] args) throws IOException//主方法
{
BigInteger big = new BigInteger();//生成一个BigInteger对象
big.setAdd1();//得到数1
big.setAdd2();//得到数2
char[] num1 = big.getAdd1();//相加
char[] num2 = big.getAdd2();//相减
int len1 = big.getLen1();
int len2 = big.getLen2();
System.out.println();
System.out.println("The length of add1 is : " + len1);
System.out.println("The length of add2 is : " + len2);
big.add(num1,num2);
System.out.println();
big.sub(num1,num2);
}
}
更多精彩
赞助商链接