1. 程式人生 > >大數加法 (a,b可能為負數)

大數加法 (a,b可能為負數)


        if(c[lc-1]>=0)                             //A,B相加為正;
        {
            flag=1;
            for(int i=0;i<lc;i++)
            {
                while(c[i]<0)
                {
                    c[i+1]--;
                    c[i]+=10;
                }
            }
        }
        else
        {
            flag=-1;
            for(int i=0;i<lc;i++)
            {
                while(c[i]>0)
                {
                    c[i+1]++;
                    c[i]-=10;
                }
            }
        }
        if(flag<0)
            printf("-");
        while(lc>1&&c[lc-1]==0)
            lc--;
        for(int i=lc-1;i>=0;i--)
            printf("%d",abs(c[i]));
        printf("\n");
}