一个实现长整型数相加减的小程序

import java.io.*;

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            add1[i]=sLine1.charAt(i);
       
        for(i=0;i        {
            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            add2[i]=sLine2.charAt(i);
       
        for(i=0;i        {
            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            temp1[len-1-i]=add1[len1-1-i];
        for(i=0;i            temp2[len-1-i]=add2[len2-1-i];
       
        int m=0;
        for(i=0;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                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                temp1[len-1-i]=sub1[len1-1-i];
            for(i=0;i                temp2[len-1-i]=sub2[len2-1-i];
        }else//保证减数大于被减数
        {
            for(i=0;i                temp2[len-1-i]=sub1[len1-1-i];
            for(i=0;i                temp1[len-1-i]=sub2[len2-1-i];
        }
       
        int m=0;
        for(i=0;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]            System.out.print("-");
            for(;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);
    }
}


你可能感兴趣的:(j2se)