大整数运算(Powerful Calculator)

题目描述:

    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)对于加法、减法、乘法三种运算,分情况考虑:两正两负、一正一负。

(2)对于减法运算,考虑消除前缀0;对于乘法运算,要注意进位可能不止一位。

#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("\n");
}

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

 

你可能感兴趣的:(算法(数学问题))