大整数演算(Powerful Calculator)

5815 ワード

タイトルの説明:
    Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU now invites you attending such amazing research and development work.     In most business applications, the top three useful calculation operators are Addition (+), Subtraction (-) and Multiplication (×) between two given integers. Normally, you may think it is just a piece of cake. However, since some integers for calculation in business application may be very big, such as the GDP of the whole world, the calculator becomes harder to develop.     For example, if we have two integers 20 000 000 000 000 000 and 4 000 000 000 000 000, the exact results of addition, subtraction and multiplication are:     20000000000000000 + 4000000000000000 = 24 000 000 000 000 000     20000000000000000 - 4000000000000000 = 16 000 000 000 000 000     20000000000000000 × 4000000000000000 = 80 000 000 000 000 000 000 000 000 000 000     Note: SJTU prefers the exact format of the results rather than the float format or scientific remark format. For instance, we need "24000000000000000"rather than 2.4×10^16.     As a programmer in SJTU, your current task is to develop a program to obtain the exact results of the addition (a + b), subtraction (a - b) and multiplication (a × b) between two given integers a and b.
入力:
   Each case consists of two separate lines where the first line gives the integer a and the second gives b (|a| <10^400 and |b| < 10^400).
出力:
    For each case, output three separate lines showing the exact results of addition (a + b), subtraction (a - b) and multiplication (a × b) of that case, one result per lines.
サンプル入力:
20000000000000000
4000000000000000

サンプル出力:
24000000000000000
16000000000000000

80000000000000000000000000000000
 
この問題は古典的な大整数演算の問題だと思います.問題を解く過程で、以下の点に注意しなければなりません.
(1)加算,減算,乗算の3つの演算について,状況に応じて,2正2負,1正1負を考慮する.
(2)減算については、接頭辞0の消去を考慮する.乗算の場合は、キャリーが1人だけではないことに注意してください.
#include 
#include 
#include 

char stra[500];
char strb[500];

struct bign
{
    int d[1000];
    int len;
};

struct bign data1;
struct bign data2;

void init(struct bign* a)
{
    memset((*a).d,0,sizeof((*a).d));
    (*a).len=0;
}

struct bign add(struct bign a,struct bign b)
{
    int i;
    struct bign c;
    init(&c);
    int carry=0;
    for(i=0;i=1&&c.d[c.len-1]==0)
    {
        c.len--;
    }
    return c;
}

struct bign multi(struct bign a,struct bign b)
{
    struct bign c;
    int i,j;
    init(&c);
    int carry=0;
    for(i=0;i=1&&c.d[c.len-1]==0)
    {
        c.len--;
    }
    return c;
}

int compare(struct bign a,struct bign b)
{
    if(a.len>b.len) return 1;
    else if(a.len=0;i--)
        {
            if(a.d[i]>b.d[i]) return 1;
            else if(a.d[i]=0;i--)
    {
        printf("%d",a.d[i]);
    }
    printf("
"); } void f_add(struct bign a,struct bign b,int signa,int signb,int res) { struct bign c; if(signa==1&&signb==1) { c=add(a,b); print(c); } else if(signa==-1&&signb==-1) { c=add(a,b); printf("-"); print(c); } else if(signa==1&&signb==-1) { if(res==-1) { c=minu(b,a); printf("-"); } else { c=minu(a,b); } print(c); } else{ if(res==1) { c=minu(a,b); printf("-"); } else { c=minu(b,a); } print(c); } } void f_minu(struct bign a,struct bign b,int signa,int signb,int res) { struct bign c; if(signa==1&&signb==-1) { c=add(a,b); print(c); } else if(signa==-1&&signb==1) { c=add(a,b); printf("-"); print(c); } else if(signa==1&&signb==1) { if(res==-1) { c=minu(b,a); printf("-"); } else { c=minu(a,b); } print(c); } else{ if(res==1) { c=minu(a,b); printf("-"); } else { c=minu(b,a); } print(c); } } void f_multi(struct bign a,struct bign b,int signa,int signb,int res) { struct bign c; if(signa*signb>=0) { c=multi(a,b); print(c); } else { c=multi(a,b); printf("-"); print(c); } } int main() { int i,j,k; while(scanf("%s %s",stra,strb)!=EOF) { int lena,lenb; int len1,len2; int signa=1; int signb=1; if(stra[0]=='-') signa=-1; if(strb[0]=='-') signb=-1; lena=strlen(stra); len1=lena; if(signa==-1) len1=lena-1; lenb=strlen(strb); len2=lenb; if(signb==-1) len2=lenb-1; data1.len=len1; for(i=0;i