精华内容
下载资源
问答
  • HugeInteger

    2018-05-09 18:51:51
    创建一个大整数类HugeInteger,该类用一个40个元素的数组来存放一个大整数(最多不超过40位)。 构造函数原型: HugeInteger(String); (1)定义几个大整数算术运算的成员函数,包括input、output、add和sub, add, ...
  • Huge Integer

    2010-07-18 09:56:54
    实现1~40为的整数(超过int、long等类型的范围)的加减乘除
  • HugeInteger Class) Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and subtract. For ...
  • 创建一个大整数类HugeInteger实现大整数的各类运算 程序文件名:HugeInteger.java package pack2; import java.math.BigInteger; import java.util.Scanner; public class HugeInteger { public static void main...

    创建一个大整数类HugeInteger实现大整数的各类运算

    程序文件名:HugeInteger.java

    package pack2;
    import java.math.BigInteger;
    import java.util.Scanner;
    public class HugeInteger {
    
    	public static void main(String[] args) {
    		System.out.println("请输入大整数a,b:");
            Scanner sc = new Scanner(System.in);
            BigInteger a, b;
            while(sc.hasNext()) {
                a = sc.nextBigInteger();
                b = sc.nextBigInteger();
                System.out.println("1.大整数运算:");
                System.out.println("a+b="+a.add(b));   //大整数加法
                System.out.println("a-b="+a.subtract(b));  //减法
                System.out.println("a*b="+a.multiply(b));  //乘法
                System.out.println("a/b="+a.divide(b));    //除法
                System.out.println("(a)mod(b)="+a.remainder(b)); //取模
                
                //大整数的比较
                System.out.println("2.大整数比较:");
                if(a.compareTo(b) == 0)
                    System.out.println("a == b");
                else if(a.compareTo(b) > 0)
                    System.out.println("a > b");
                else if(a.compareTo(b) < 0)
                    System.out.println("a < b");
                
                //大整数的绝对值
                System.out.println("绝对值:"+a.abs());
                
                //计算大整数的幂次方
                int exp = 10;
                System.out.println("幂次方:"+a.pow(exp));
                
                //返回大整数十进制的字符串表示
                System.out.println("字符串表示:"+a.toString());
                
                //返回大整数p进制的字符串表示
                int p = 2;
                System.out.println("p进制的字符串表示:"+a.toString(p));
            }
            sc.close();
        }
    
    }
    

    输出:
    blog.csdnimg.cn/20191115172100122.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyMzYyNDQw,size_16,color_FFFFFF,t_70)

    展开全文
  • package shuzuxiangcheng;import java.util....public class Hugeinteger { int a[]=new int[40]; int b[]=new int[40]; int cc[]=new int[40]; int c[]=new int[40]; int d[]=new int[40]; int e[]=new int[40...
    package shuzuxiangcheng;
    import java.util.Scanner;
    public class Hugeinteger {


    int a[]=new int[40];
    int b[]=new int[40];
    int cc[]=new int[40];
    int c[]=new int[40];
    int d[]=new int[40];
    int e[]=new int[40];
    int aa[]=new int[40];
    int ss[]=new int[40];
    int dd[]=new int[40];
    public Hugeinteger(){
    for(int i=0;i<40;i++){
    a[i]=0;
    }
    }
    public void StringToInt(String str){

    for(int i=0;i<str.length();i++){
    char t=str.charAt(i);
    b[i]=t-48;
    }
    for(int i=0;i<str.length();i++){
    System.out.print(b[i]);
    }
    System.out.println();
       
    }
    public String toString(){

    cc[0]=2;
    cc[1]=3;
    cc[2]=5;
    cc[3]=4;
    String s[]=new String[1];
    s[0]=Integer.toString(cc[0]);
    for(int i=1;i<4;i++){
    s[0]=s[0]+Integer.toString(cc[i]);

    }
    // for(int i=0;i<4;i++){
    // System.out.print(s[i]);
    // System.out.println();
    // }
    return s[0];



    }



    public void Add(){


    for(int i=3;i>=0;i--){
    c[i]=2*i+2;
    d[i]=2*i+2;
    }
    for(int i=3;i>=0;i--)
    System.out.print(c[i]);
    System.out.print("+");
    for(int i=3;i>=0;i--)
    System.out.print(d[i]);
    System.out.print("=");
    // c[0]=2;
    // c[1]=4;
    // c[2]=6;
    // c[3]=8;
    // d[0]=2;
    // d[1]=4;
    // d[2]=6;
    // d[3]=8;

    for(int i=0;i<=4;i++){
    e[i]=(c[i]+d[i])%10;
    if(i>=1&&i<=4)
    e[i]=e[i]+(c[i-1]+d[i-1])/10;
    }
    for(int i=4;i>=0;i--)
    System.out.print(e[i]);
    }


    public void multiply(){
    System.out.println();
    System.out.println("第一个数组位数N:");

    Scanner sc = new Scanner(System.in);
    Scanner scc = new Scanner(System.in);
    int n1= scc.nextInt();
    System.out.println("请输入这"+n1+"位");
    for(int i=n1;i>=1;i--)
    aa[i]=sc.nextInt();

    System.out.println("第二个数组位数N:");
    Scanner sc1 = new Scanner(System.in);
    Scanner scc1 = new Scanner(System.in);
    int n2= scc1.nextInt();
    System.out.println("请输入这"+n2+"位");
    for(int i=n2;i>=1;i--)
    ss[i]=sc1.nextInt();


    for(int i=n1;i>=1;i--)
    System.out.print(aa[i]);
    System.out.print("*");
    for(int i=n2;i>=1;i--)
    System.out.print(ss[i]);
    System.out.print("=");


    for(int i=1;i<=n1;i++){
    for(int j=1;j<=n2;j++){
    dd[i+j-1]=dd[i+j-1]+(aa[i]*ss[j])%10;
    if(dd[i+j-1]>=10){
    dd[i+j]=dd[i+j]+dd[i+j-1]/10;
    dd[i+j-1]=dd[i+j-1]%10;
    }
    dd[i+j]=dd[i+j]+(aa[i]*ss[j])/10;
    }


    }




    if(dd[n1+n2]==0){
    for(int j=n1+n2-1;j>=1;j--){
    System.out.print(dd[j]);
    }
    }
    else{
    for(int j=n1+n2;j>=1;j--){
    System.out.print(dd[j]);
    }
    }

    }
    }



    package shuzuxiangcheng;
    import java.util.Scanner;
    public class Test {
    public static void main(String orgs[]){
    Hugeinteger n=new Hugeinteger();
    n.StringToInt("5523254");

    System.out.println(n.toString());
    n.Add();
    System.out.println();
    n.multiply();
    }

    }





    展开全文
  • <p>This big integer object is created just to detect the valid number type and it is discarded. For a boolean parsing, can we not be more strict and say the length of the digits should be only 1. Then...
  • 功能: 1. 使用字符串解析构建一个40位...自定义40位大整数类及测试类(Define a huge integer and implement its arithmetic operation), https://blog.csdn.net/hpdlzu80100/article/details/2318646 测试结...

    2020/02/19补记:

    使用Java BigInteger类测试通过。(仅限两个大整数都为40位的情况,且第一个数字大于第二个数字。现在发现,自定义的这个类的设计更像是面向过程设计而不是面向对象设计。)

    测试结果如下:

    b1: 3456789012345678901234567890123456789012, b2:2345678901234567890123456789012345678901

    加法操作:5802467913580246791358024679135802467913

    减法操作:1111110111111111011111111101111111110111

    乘法操作:8108517052278739222374662673952145142565060698363359281084621249233290657035812

    除法操作:0

    最大数:3456789012345678901234567890123456789012

    最小数:2345678901234567890123456789012345678901

    商是:1;余数是:1111110111111111011111111101111111110111

    等价性是:false

    比较操作: b1>b2

    参考文章:

    Java核心技术——Java BigInteger类(大整数类) 

    -------------------------------------------------------------------------------------------------------------------

    以下为正文:

    功能:

    1. 使用字符串解析构建一个40位的整数

    2. 实现加减运算及其他一些方法(比较大小等)

    参考文章:

    自定义40位大整数类及测试类(Define a huge integer and implement its arithmetic operation),

    https://blog.csdn.net/hpdlzu80100/article/details/2318646

    测试结果:

    长整数33456789012345678901234567890123456789012

    长整数42345678901234567890123456789012345678901

    长整数3和长整数4不相等(使用isEqualTo方法)

    长整数3和长整数4不相等(使用isNotEqualTo方法)

    长整数3大于长整数4(使用isGreaterThan方法)

    长整数3大于长整数4(使用isLessThan方法)

    长整数3大于等于长整数4(使用isGreaterThanOrEqualTo方法)

    长整数3大于长整数4(使用isLessThanOrEqualTo方法)

    长整数3和长整数4的和为:

    Huge Integer: 5802467913580246791358024679135802467913

    长整数3和长整数4的差为:

    Huge Integer: 1111110111111111011111111101111111110111

     

    长整数32345678901234567890123456789012345678901

    长整数42345678901234567890123456789012345678901

    长整数3和长整数4相等(使用isEqualTo方法)

    长整数3和长整数4相等(使用isNotEqualTo方法)

    长整数3小于等于长整数4(使用isGreaterThan方法)

    长整数3大于等于长整数4(使用isLessThan方法)

    长整数3大于等于长整数4(使用isGreaterThanOrEqualTo方法)

    长整数3小于等于长整数4(使用isLessThanOrEqualTo方法)

    长整数3和长整数4的和为:

     

    长整数32345678901234567890123456789012345678901

    长整数3和长整数4的和为:4691357802469135780246913578024691357802

    长整数3和长整数4的差为:

    Huge Integer: 0000000000000000000000000000000000000000

    长整数3等于零

     

    代码:

    1. 实体类HugeInteger

    //JHTP Exercise 8.16: Huge Integer Class
    //by pandenghuang@163.com
    /**
     * 8.16 (Huge Integer Class) Create a class HugeInteger which uses a 40-element
     * array of digits to store integers as large as 40 digits each. Provide methods
     * parse, toString, add and subtract. Method parse should receive a String,
     * extract each digit using method charAt and place the integer equivalent of
     * each digit into the integer array. For comparing HugeInteger objects, provide
     * the following methods: isEqualTo, isNotEqualTo, isGreaterThan, isLessThan,
     * isGreaterThanOrEqualTo and isLessThanOrEqualTo. Each of these is a predicate
     * method that returns true if the relationship holds between the two
     * HugeInteger objects and returns false if the relationship does not hold.
     * Provide a predicate method isZero. If you feel ambitious, also provide
     * methods multiply, divide and remainder. [Note: Primitive boolean values can
     * be output as the word “true” or the word “false” with format specifier %b.]
     * 
     * @author pandenghuang@163.com
     *
     */
    public class HugeInteger {
    	final static int SIZE = 40;	//自定义长整数的位数
        private int[ ] singleDigits=new int[SIZE];
        private int[ ] carry=new int[SIZE];
        
        /**
         * 无参构造函数(所有位初始化为0)
         */
        public HugeInteger()
        {
            for (int i=0;i<singleDigits.length;i++)
                singleDigits[i]=0;
        }
        /**
         * 使用HugeInteger构造一个HugeInteger(复制)
         * @param hugeI
         */
        public HugeInteger(int[] hugeI)
        {
            for (int i=0;i<singleDigits.length;i++)
                singleDigits[i]=hugeI[i];
        }
        
        /**
         * 解析一个字符串为HugeInteger
         * @param hugeI
         */
        public static HugeInteger parse(String hugeIntegerText)
        {
        	int[ ] singleDigits=new int[SIZE];
            for (int i=0;i<SIZE;i++)
                singleDigits[i]=Integer.parseInt(String.valueOf(hugeIntegerText.charAt(i)));
            
            return new HugeInteger(singleDigits);
        }
        
        
        /**
         * 重置(所有位初始化为0)
         * @param hugeI
         */
        public void reset()
        {
            for (int i=0;i<singleDigits.length;i++)
                singleDigits[i]=0;
        }
        
        
        /**
         * 返回大整数的格式化输出字符串
         */
        public String toString()
        {    
        	String s="";
            for (int i=0;i<singleDigits.length;i++)
            	s += singleDigits[i];
            
            return String.format("%s",s);
        }
        
        public void output()
        {    System.out.print("Huge Integer: ");
            for (int i=0;i<singleDigits.length;i++)
                System.out.print(singleDigits[i]);
                System.out.println();
        }
        
        public void add(HugeInteger HI)
        {  int[] repository=new int[41];
            for (int i=singleDigits.length-1;i>=0;i--) 
                {
                if (i<singleDigits.length-1)
                carry[i]=(singleDigits[i]+HI.singleDigits[i]+carry[i+1])/10;
                else
                    carry[i]=(singleDigits[i]+HI.singleDigits[i])/10;
                if (i<singleDigits.length-1)
                repository[i+1]=(singleDigits[i]+HI.singleDigits[i]+carry[i+1])%10;
                else 
                    repository[i+1]=(singleDigits[i]+HI.singleDigits[i])%10;
                }
            repository[0]=(carry[1]+singleDigits[0]+HI.singleDigits[0])/10;
               if (repository[0]==1)
                   {
                   System.out.print("Overflow Error!!! ");
                   reset();
                   }
               else
                   for (int i=0;i<singleDigits.length;i++ )
                      singleDigits[i]=repository[i+1];
               
        }
        
        /**
         * 静态方法(类方法),2019年1月2日重写
         * @param hi1 hugeInteger1
         * @param hi2 hugeInteger2
         * @return
         */
        public static HugeInteger add(HugeInteger hi1, HugeInteger hi2)
        {  int[] repository=new int[SIZE+1];
           int[] carry = new int[SIZE];  //进位
           int[] result = new int[SIZE];
            for (int i=SIZE-1;i>=0;i--)   //从低位(右侧)开始计算
                {
                if (i<SIZE-1) //最低位以外的位上的进位要考虑上一位上的进位
                carry[i]=(hi1.singleDigits[i]+hi2.singleDigits[i]+carry[i+1])/10;
                else //最低位上的进位(i=SIZE-1)
                    carry[i]=(hi1.singleDigits[i]+hi2.singleDigits[i])/10;
                
                if (i<SIZE-1) //最低位以外的位上的求和的结果(要考虑上一位上的进位)
                repository[i+1]=(hi1.singleDigits[i]+hi2.singleDigits[i]+carry[i+1])%10;
                else //最低位求和的结果(i=SIZE-1)
                    repository[i+1]=(hi1.singleDigits[i]+hi2.singleDigits[i])%10;
                }
            repository[0]=(carry[1]+hi1.singleDigits[0]+hi2.singleDigits[0])/10;
               if (repository[0]==1)
                   {
                   System.out.print("Overflow Error!!! ");
                   return new HugeInteger();
                   }
               else {
                   for (int i=0;i<SIZE;i++) 
                      result[i]=repository[i+1];  //将计算结果由高位(对应数组低位)赋值给结果数组
                      return new HugeInteger(result);}
        }
        
        public boolean substract(HugeInteger HI)
        {
            boolean underflowFlag=false;
            int[] repository=new int[41];
            for (int i=singleDigits.length-1;i>=1;i--)
            {
            if (singleDigits[i]-HI.singleDigits[i]>=0)
                repository[i+1]=singleDigits[i]-HI.singleDigits[i];
            else
            {
                repository[i+1]=10+singleDigits[i]-HI.singleDigits[i];
                singleDigits[i-1]-=1;
            }
            }
            
            repository[0]=singleDigits[0]-HI.singleDigits[0];
             if (repository[0]<0)
               {
               System.out.print("Overflow Error!!! ");
               reset();
               return(underflowFlag=true);
               }
           else
           {   repository[1]=singleDigits[0]-HI.singleDigits[0];
               for (int i=0;i<singleDigits.length;i++ )
                   singleDigits[i]=repository[i+1];
               return(underflowFlag);
             }
        }
        
        public boolean isEqualTo(HugeInteger HI){
            boolean equalFlag=true;
            for (int i=0;i<singleDigits.length;i++)
                if (singleDigits[i]!=HI.singleDigits[i])
                {equalFlag=false;
                break;
                }
            return equalFlag;
        }
        
        public boolean isNotEqualTo(HugeInteger HI){
            
            
            return !isEqualTo(HI);
        }
        
        public boolean isGreaterThan(HugeInteger hi){
            boolean greaterThanFlag=false;
            for (int i=0;i<singleDigits.length;i++)
                {
                if (singleDigits[i]>hi.singleDigits[i])
                    {greaterThanFlag=true;
                    break;}
                else if (singleDigits[i]<hi.singleDigits[i])
                        {greaterThanFlag=false;
                        break;}
                }
        return greaterThanFlag;
        }
        
        public boolean isLessThan(HugeInteger HI){
            boolean lessThanFlag=false;
            for (int i=0;i<singleDigits.length;i++)
            { if (singleDigits[i]<HI.singleDigits[i])
                {lessThanFlag=true;
                break;}
                else if (singleDigits[i]>HI.singleDigits[i])
                {lessThanFlag=false;
                break;}
                }
            return lessThanFlag;
        }
        
        public boolean isGreaterThanOrEqualTo(HugeInteger HI){
            
            return !isLessThan(HI);
        }
        
        public boolean isLessThanOrEqualTo(HugeInteger HI){
        
            return !isGreaterThan(HI);
            
        }
        
        public boolean isZero(){
            boolean zeroFlag=false;
            int tempSum=0;
            for (int i=0;i<singleDigits.length;i++)
                tempSum+=singleDigits[i];
                if (tempSum==0)
                    zeroFlag=true;
            return zeroFlag;
        }
    }

    2. 测试类

    //JHTP Exercise 8.15: Rational Numbers
    //by pandenghuang@163.com
    /**
    * 8.16 (Huge Integer Class) Create a class HugeInteger which uses a 40-element
    * array of digits to store integers as large as 40 digits each. Provide methods
    * parse, toString, add and subtract. Method parse should receive a String,
    * extract each digit using method charAt and place the integer equivalent of
    * each digit into the integer array. For comparing HugeInteger objects, provide
    * the following methods: isEqualTo, isNotEqualTo, isGreaterThan, isLessThan,
    * isGreaterThanOrEqualTo and isLessThanOrEqualTo. Each of these is a predicate
    * method that returns true if the relationship holds between the two
    * HugeInteger objects and returns false if the relationship does not hold.
    * Provide a predicate method isZero. If you feel ambitious, also provide
    * methods multiply, divide and remainder. [Note: Primitive boolean values can
    * be output as the word “true” or the word “false” with format specifier %b.]
    * 
    * @author pandenghuang@163.com
    *
    */
    
    public class HugeIntegerTest{
    	
        public static void main(String[] args)
        {
        	
    //
    //        int[] initializer1=new int[40];
    //        for (int i=0;i<40;i++)
    //            initializer1[i]=(int)(10*Math.random());
    //           
    //        int[] initializer2=new int[40];
    //        for (int j=0;j<40;j++)
    //            initializer2[j]=(int)(10*Math.random());
    //        
    //        
    //        
    //        HugeInteger HI1=new HugeInteger(initializer1);
    //        HugeInteger HI2=new HugeInteger(initializer2);
    //        
    //        
    //        HI1.output();
    //        HI2.output();
    //        System.out.println();
    //        
    //
    //        
    //        
    //        System.out.println("These two huge integers are equal: "+HI1.isEqualTo(HI2));
    //        System.out.println("These two huge integers are not equal: "+HI1.isNotEqualTo(HI2));
    //        System.out.println("1st huge integer is larger than 2nd: "+HI1.isGreaterThan(HI2));
    //        System.out.println("1st huge integer is less than 2nd: "+HI1.isLessThan(HI2));
    //        System.out.println("1st huge integer is larger than or equal to 2nd: "+
    //                HI1.isGreaterThanOrEqualTo(HI2));
    //        System.out.println("1st huge integer is less than or equal to 2nd: "+
    //                HI1.isLessThanOrEqualTo(HI2));
    //        HI1.add(HI2);
    //        System.out.print("The sum is: ");
    //        HI1.output();
    //        HI1.substract(HI2);
    //        HI1.substract(HI2);
    //        System.out.print("The difference is: ");
    //        HI1.output();
    //        HI1.add(HI2);
            
        	HugeInteger hi3 = HugeInteger.parse("3456789012345678901234567890123456789012");
        	HugeInteger hi4 = HugeInteger.parse("2345678901234567890123456789012345678901");
            System.out.printf("长整数3:%s%n",hi3);
            System.out.printf("长整数4:%s%n",hi4);
            System.out.printf("长整数3和长整数4%s相等(使用isEqualTo方法)%n",hi3.isEqualTo(hi4)?"":"不");
            System.out.printf("长整数3和长整数4%s相等(使用isNotEqualTo方法)%n",hi3.isNotEqualTo(hi4)?"不":"");
            System.out.printf("长整数3%s长整数4(使用isGreaterThan方法)%n",hi3.isGreaterThan(hi4)?"大于":"小于");
            System.out.printf("长整数3%s长整数4(使用isLessThan方法) %n",hi3.isLessThan(hi4)?"小于":"大于");
            System.out.printf("长整数3%s长整数4(使用isGreaterThanOrEqualTo方法)%n",
                    hi3.isGreaterThanOrEqualTo(hi4)?"大于等于":"小于");
            System.out.printf("长整数3%s长整数4(使用isLessThanOrEqualTo方法)%n",
                    hi3.isLessThanOrEqualTo(hi4)?"小于等于":"大于");
            System.out.printf("长整数3和长整数4的和为:%n");
            hi3.add(hi4);
            hi3.output();
            hi3 = HugeInteger.parse("3456789012345678901234567890123456789012");
            System.out.printf("长整数3和长整数4的差为:%n");
            hi3.substract(hi4);
            hi3.output();
            
        	hi3 = HugeInteger.parse("2345678901234567890123456789012345678901");
        	hi4 = HugeInteger.parse("2345678901234567890123456789012345678901");
            System.out.printf("%n长整数3:%s%n",hi3);
            System.out.printf("长整数4:%s%n",hi4);
            System.out.printf("长整数3和长整数4%s相等(使用isEqualTo方法)%n",hi3.isEqualTo(hi4)?"":"不");
            System.out.printf("长整数3和长整数4%s相等(使用isNotEqualTo方法)%n",hi3.isNotEqualTo(hi4)?"不":"");
            System.out.printf("长整数3%s长整数4(使用isGreaterThan方法)%n",hi3.isGreaterThan(hi4)?"大于":"小于等于");
            System.out.printf("长整数3%s长整数4(使用isLessThan方法) %n",hi3.isLessThan(hi4)?"小于":"大于等于");
            System.out.printf("长整数3%s长整数4(使用isGreaterThanOrEqualTo方法)%n",
                    hi3.isGreaterThanOrEqualTo(hi4)?"大于等于":"小于");
            System.out.printf("长整数3%s长整数4(使用isLessThanOrEqualTo方法)%n",
                    hi3.isLessThanOrEqualTo(hi4)?"小于等于":"大于");
            System.out.printf("长整数3和长整数4的和为:%n",HugeInteger.add(hi3, hi4));
    //        hi3.add(hi4);
    //        hi3.output();
    //        hi3 = HugeInteger.parse("2345678901234567890123456789012345678901");
            System.out.printf("%n长整数3:%s%n",hi3);
            System.out.printf("长整数3和长整数4的和为:%s%n",HugeInteger.add(hi3, hi4));
            hi3 = HugeInteger.parse("2345678901234567890123456789012345678901");
            System.out.printf("长整数3和长整数4的差为:%n");
            hi3.substract(hi4);
            hi3.output();
            System.out.printf("长整数3%s零%n",hi3.isZero()?"等于":"不等于");
    
        }
        
    }

     

    展开全文
  • huge integer 加减运算 参考代码

    千次阅读 2013-10-09 23:10:46
    头文件:huge.h #include #include #define M 40 using namespace std; class huge { public: huge(); void set(); void add(huge &); //相加 void sub(huge &); //相减 void compare(hu
    头文件:huge.h
    #include<iostream>
    #include<cstdio>
    #define M 40
    using namespace std;

    class huge
    {
    public:
    huge();
    void set();
    void add(huge &);           //相加
    void sub(huge &);          //相减
    void compare(huge &);     //关系比较,相等或大小


    private:
    short a[M];

    };



    cpp文件:huge.cpp 
    #include"huge.h"
    //--------------------------------------------------------------
    huge::huge()            //初始化数组
    {
    int i;
    for(i=0;i<M;i++)
    {
    a[i]=0;
    }

    }
    //--------------------------------------------------------------
    void huge::set()               //给数组赋值
    {
    char ch;
    short b[M];

    int i=0;
    for(i=0;i<M;i++)
    {
    b[i]=0;
    }
    i=0;
    cout<<"please input a hugeinteger:";
    scanf("%c",&ch);
    while(ch>='0'&&ch<='9')
    {
    b[i]=ch-'0';                 //将字符转化为整形数
    i++;
    scanf("%c",&ch);
    }
    i=i-1;

    int k;
    for(k=0;k<=i;k++)              //将字符在数组中右对齐赋值
    {
    a[M-1-k]=b[i-k];
    }





    }
    //----------------------------------------------------------
    void huge::add(huge &p)               //两个对象做加法
    {
    cout<<"addtion:"<<endl;
    int i=0,k=0,x=0;

    short b[M];
    for(i=0;i<M;i++)
    {
    b[i]=0;
    }


    for(i=M-1;i>=0;i--)
    {
    b[i]=b[i]+a[i]+p.a[i];
    if(b[i]>9)                //满10进1
    {
    b[i]=b[i]-10;
    b[i-1]=b[i-1]+1;
    }

    }
    cout<<"result:";
    for(i=0;i<M;i++)
    {
    k=b[i];
    if(k==x) //第一个非零字符前所有的0,不显示       
    {
    continue;
    }
    else
    {
    cout<<b[i];
    x=100;
    }
    }
    cout<<endl;

    }
    //----------------------------------------------------------
    void huge::sub(huge &p)              //两对象做减法
    {
    cout<<"substraction:"<<endl;
    int i=0,k=0,m=0,x=0;
    short c[M];


    for(i=0;i<M;i++)
    {
    c[i]=0;
    }

    for(i=0;i<M;i++)                     //判断两个对象的大小
    {


    k=a[i]-p.a[i];

    if(k==0)
    {
    continue;
    }
    else if(k>0)
    {
    m=1;
    break;
    }
    else
    {
    m=-1;
    break;
    }

    }
    if(i==M-1)
    {
    cout<<"result:0"<<endl;
    }

    k=0;
    if(m>0)                            //第一个对象大
    {
    for(i=M-1;i>=0;i--)
    {
    c[i]=c[i]+a[i]-p.a[i];

    if(c[i]<0)                 
    {
    c[i]=k+a[i]+10-p.a[i];   //从前一位借10来完成正整数减法
    c[i-1]-=1;
    k=-1;
    }
    }
    cout<<"result:";
    }
    else if(m<0)                  //第二个对象大
    {
    for(i=M-1;i>=0;i--)
    {
    c[i]=c[i]+p.a[i]-a[i];

    if(c[i]<0)
    {
    c[i]=k+p.a[i]+10-a[i];
    c[i-1]-=1;
    k=-1;
    }
    }
    cout<<"result:-";
    }

    for(i=0;i<M;i++)
    {
    k=c[i];
    if(k==x) //第一个非零字符前所有的0,不显示       
    {
    continue;
    }
    else
    {
    cout<<c[i];
    x=100;
    }
    cout<<endl;
    }
    }
    //---------------------------------------------------------------------
    void huge::compare(huge &p)                   //比较两个对象的关系
    {
    cout<<"relations:";
    int i;
    for(i=0;i<M;i++)
    {
    if(a[i]==p.a[i])
    {
    continue;
    }
    else
    {
    cout<<"Not equal!"<<endl;
    break;
    }
    }
    if(i==M)                  //相等,退出,不继续运行
    {
    cout<<"Equal!"<<endl;
    getchar();
    getchar();
    exit (0);
    }
    for(i=0;i<M;i++)            //不相等,比较大小
    {
    if(a[i]==0&&p.a[i]==0)
    {
    continue;
    }
    else if(a[i]>p.a[i])
    {
    cout<<"The first integer is bigger than the second one!"<<endl;
    break;
    }
    else
    {
    cout<<"The first integer is smaller than the second one!"<<endl;
    break;
    }
    }

    }




    cpp文件 主函数.cpp
     
     #include"huge.h"

    void main()
    {
    huge x,y;               //定义两个对象
    x.set();
    y.set();
    x.add(y);
    x.sub(y);
    x.compare(y);

    system("pause");
    }



    代码仅供参考 
    展开全文
  • //Define a huge integer and implement its arithmetic calculation//Java how to program, 5/e, Exercise 8.10import ...public class HugeInteger { private int[ ] singleDigits=new int[40]
  • Computing with huge integer

    2005-07-06 18:17:00
    This article is came from a discussion in forum, it is a problem to computing f(n) that f(n) = fibonacci[i-1]*fibonacci[i]*fibonacci[i+1]. Apparently the vaule of f(n) is too huge to represented by p
  • Huge And Integer

    2019-04-24 19:20:06
    Huge And Integer Time Limit: 1000/1000MS (C++/Others) Memory Limit: 262144/262144KB (C++/Others) Problem Description Huge 非常喜欢收集数学问题。 有一道题是这样的,给定正整数aa,bb并且aa与bb互质且满足a...
  • <div><p>Closes #3852. <p>This change is <a href="https://reviewable.io/reviews/robotlocomotion/drake/3877"><img src=...该提问来源于开源项目:RobotLocomotion/drake</p></div>
  • .rw-r--r-- 9.2E freaky freaky 29 Jun 21:48 huge1 .rw-r--r-- 9.2E freaky freaky 29 Jun 21:49 huge2 .rw-r--r-- 9.2E freaky freaky 29 Jun 21:49 huge3 .rw-r--r-- 9.2E freaky freaky 29 Jun 21:49 huge4 ...
  • Codeforce 1251C. Minimize The Integer

    千次阅读 2019-10-25 08:11:16
    C. Minimize The Integer time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...You are given a huge integer a consisting of n digits (n is between 1 ...
  • <div><p>What is going on here? I couldn't understand what this line (and the concept of "natural scale") is for: ...<p>When you have a scale set, it wrongly adds zeroes for some reason: ...
  • 【CodeForces 1251C --- Minimize The Integer】 ...You are given a huge integer a consisting of n digits (n is between 1 and 3⋅105, inclusive). It may contain leading zeros. You can swap two digi...
  • You are given a huge integer a consisting of n digits (n is between 1 and 3⋅105, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping ...
  • You are given a huge integer a consisting of n digits (n is between 1 and 3⋅105, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) pos...
  • You are given a huge integer ???? consisting of ???? digits (???? is between 1 and 3⋅105, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the ...
  • <div><p>Also add row/column information to error message when exception is thrown during value insert into column. <p>With this and the quoting/escaping PR, <code>importing all rows of a file (miss ...
  • You are given a huge integer a consisting of n digits (n is between 1 and 3⋅105, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping ...
  • //HugeInteger.h #include<bits/stdc++.h> using namespace std; class HugeInteger { private: int num[1205]; public: HugeInteger(); void setInteger(string s); void setInteger(int n); ...
  • <div><p>This is a feature request, no bug report. <p>I am using the software ENVI and SARscape. Both rely on the GDAL lib to read GeoTiff files. During my work with large GeoTiff files (satellite ...
  • 高精加减乘除

    2020-03-23 22:53:59
    神奇的高精加减乘除 ...#ifndef HUGEINTEGER_H #define HUGEINTEGER_H #include<iostream> using namespace std; class HugeInteger{ public: HugeInteger(); friend ostream & operator<<(...
  • LENGTH data type is integer

    2020-12-25 18:34:40
    <div><p>According to: http://fixprotocol.org/FIXimate3.0/en/FIX.5.0SP2/fix_datatypes.html, LENGTH is an integer datatype, just fixing this in the generated code, probably not a huge show stopper but ...
  • 【C++】C++大数的运算

    千次阅读 2017-08-17 22:43:52
    一、实验项目内容(HugeInteger Class) Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. 1.Provide member functions input, output, add and ...
  • 1280x800 on a 1920x1080 monitor which has a huge letterbox. So I'd like to suggest a new, separate variable, enabling screen scaling by semi-integer factors. To prevent a conflict of <code>...
  • <div><p>There is a huge number of warnings like this: <p>/…/vienna-rss/src/StringExtensions.m:266:16: Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') ...
  • uploading huge files

    2020-12-28 07:15:56
    <div><p>Jan Stürtz reported a problem with the uploading of large files - when uploading a large file of 1.4 GB Morepath gave a memory error, and with 4.3 GB we got an Overflow error due to integer ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 552
精华内容 220
关键字:

hugeinteger