大数(大数的四则运算)

/*
    大数计算(重点)
*/

//大数加法



struct bigInteger
{
    int digit[1000];
    int size;
    int sign;//标识正负  1为正,-1为负

    void init()
    {
        for(int i=0; i<1000; i++)
            digit[i] = 0;
        size = 0;
    }

    void set(char str[])
    {
        init();
        int L = strlen(str);
        for(int i = L-1,j=0,temp=0,weight=1; i>=0; i--)
        {

            //j记录当前转化字符的个数
            //wight表示权重从1,10,100,1000依次增加
            //
            temp+=(str[i]-'0')*weight;
            j++;
            weight*=10;

            if(j%4==0||i==0) //若已经连续转了4个字符或者已经全部转完了
            {
                digit[size++] = temp;


                //重置
                j = 0;
                temp = 0;
                weight = 1;
            }

        }
    }//end set


    void output(FILE *fp)
    {
        char s[1000];//临时存放
        char stemp[1000];
        int j,n,t;//标记当前需要填充的0的个数


        printf("output size = %d\n",size);

        for(int i =size-1; i>=0; i--)
        {

            j = 0;
            if(i!=size-1)
            {
                itoa(digit[i],s,10);
                //fputs(s,fp);

                t = digit[i];
                while(t)
                {
                    j++;
                    t/=10;
                }

                n = 4-j;//需要填充0的个数


                for(int i=0; i=0; i--)
        {
            int t = (remainder*10000 + digit[i])/x;
            int r = (remainder*10000 + digit[i])%x;

            ret.digit[i] = t;
            remainder = r;


            printf("digit[i] = %d,remainder = %d,t = %d,r = %d\n",digit[i],remainder,t,r);

        }
        ret.size = 0;
        for(int i=0; i<100; i++)
            if(digit[i]!=0)
                ret.size = i;

        printf("i = %d\n",ret.size);

        ret.size++;


        for(int i=size-1; i>=0; i--)
            printf("reuslt = %d\n",ret.digit[i]);

        return ret;
    }


    bigInteger operator - (bigInteger A)
    {
        bigInteger ret;//返回的结果
        ret.init();

        int borrow = 0;//借位

        for(int i=0; i=0; i--)
        {
            long long t = (remainder*10000+digit[i])/x;
            long long r = (remainder*10000+digit[i])%x;

            remainder = r;

            printf("digit[i] = %d,t = %d,r = %d,remiander = %d\n",digit[i],t,r,remainder);
        }

        return remainder;

    }

    int operator >(bigInteger A)
    {
        /*
            > 1
            < -1
            = 0
        */
        if(size == A.size)
        {
            for(int i=size-1; i>=0; i--)
            {
                if(digit[i]>A.digit[i])
                    return 1;

                else if(digit[i]A.size)
                return 1;

            else
                return -1;
        }
    }

} a,b,c;

 

你可能感兴趣的:(算法,考研)