大整数运算(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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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<a.len||i<b.len;i++)
    {
        int temp=a.d[i]+b.d[i]+carry;
        c.d[c.len++]=temp%10;
        carry=temp/10;
    }
    if(carry!=0)
    {
        c.d[c.len++]=carry;
    }
    return c;
}

struct bign minu(struct bign a,struct bign b)
{
    int i;
    struct bign c;
    init(&c);
    for(i=0;i<a.len||i<b.len;i++)
    {
        if(a.d[i]<b.d[i])
        {
            a.d[i+1]--;
            a.d[i]+=10;
        }
        c.d[c.len++]=a.d[i]-b.d[i];
    }
    while(c.len-1>=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<a.len;i++)
    {
        for(j=0;j<b.len;j++)
        {
            int temp=c.d[i+j];
            c.d[i+j]=(c.d[i+j]+carry+a.d[i]*b.d[j])%10;
            carry=(a.d[i]*b.d[j]+carry+temp)/10;
        }
        int m=i+j;
        while(carry!=0)
        {
            c.d[m++]=carry%10;
            carry/=10;
        }
    }
    c.len=a.len+b.len+1;
    while(c.len-1>=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<b.len) return -1;
    else
    {
        int i;
        for(i=a.len-1;i>=0;i--)
        {
            if(a.d[i]>b.d[i]) return 1;
            else if(a.d[i]<b.d[i]) return -1;
        }
        return 0;
    }
}

void print(struct bign a)
{
    int i;
    for(i=a.len-1;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<len1;i++)
        {
            data1.d[i]=stra[lena-i-1]-'0';
        }
        data2.len=len2;
        for(i=0;i<len2;i++)
        {
            data2.d[i]=strb[lenb-i-1]-'0';
        }
        int res=compare(data1,data2);
        f_add(data1,data2,signa,signb,res);
        f_minu(data1,data2,signa,signb,res);
        f_multi(data1,data2,signa,signb,res);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值