九度oj-1037

题目描述:

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

代码

#include <iostream>  
#include <stdio.h>  
#include <string>  
#include <algorithm>  
using namespace std;  

static const int  maxn =  200;  
static const int ten[4] = {1,10,100,1000};   

typedef struct bigint{   
    int d[maxn];   
    bigint(string s){   
        int len = (int) s.length();      
        int i,j,k;  
        for (i = 0; i < maxn; ++i) d[i] = 0;   
        d[0] = (len-1)/4 + 1;   
        for (i = len-1; i >=0; --i) {   
            j = (len-1-i)/4 + 1;  
            k = (len-1-i)%4;   
            d[j] += ten[k]*(s[(unsigned) i]-'0');   
        }   
        while(d[0]>1&&d[d[0]]==0) --d[0];   
    }   
    bigint(){  
        *this = bigint("0");   
    }    
    string toString(){   
        string s="";  
        int i,j,temp;   
        for (i = 3; i >=0; --i)    
            if(d[d[0]] >= ten[i]) break;    
        temp = d[d[0]];  
        for (j = i;  j>=0; --j) {   
            s+= (char)(temp/ten[j] + '0');   
            temp %= ten[j];  
        }   
        for (i = d[0]-1; i >0; --i) {   
            temp = d[i];   
            for (j=3; j>=0; --j) {   
                s += (char)(temp/ten[j]+'0');     
                temp %=ten[j];  
            }   
        }   
        if(s=="") s="0";   
        return s;  
    }   
}BigInt;   

bool operator < (BigInt &a, BigInt &b){   
    if(a.d[0] != b.d[0]) return a.d[0] < b.d[0];    
    for (int i = a.d[0]; i>=1  ; --i) {   
        if(a.d[i] != b.d[i]) return a.d[i] < b.d[i];    
    }   
    return false;  
}    

BigInt operator + (BigInt &a, BigInt &b){   
    BigInt c;   
    c.d[0] = max(a.d[0],b.d[0]);    
    int i,x=0;  
    for (i = 1; i <=c.d[0]; ++i) {   
        x = (a.d[i] + b.d[i] + x);    
        c.d[i] = x%10000;  
        x /=10000;  
    }   
    while(x!=0){   
       c.d[++c.d[0]] = x%10000;   
       x /=10000;  
    }    
    return c;  
}    

BigInt operator - (BigInt &a, BigInt &b){   
    BigInt c;   
    c.d[0] = a.d[0];  
    int i,x=0;  
    for (i = 1; i <=c.d[0]; ++i) {   
        x = (10000 + a.d[i] -b.d[i] + x);    
        c.d[i] = x%10000;  
        x = x/10000 - 1;  
    }   
    while(c.d[0] > 1 && c.d[c.d[0]] == 0) --c.d[0];   
    return c;  
}    

BigInt operator * (BigInt &a, BigInt &b){   
    BigInt c;   
    c.d[0] = a.d[0]+b.d[0];  
    int i,j,x=0;  
    for (i = 1; i <=a.d[0]; ++i) {   
        x = 0;  
        for (j=1; j<=b.d[0]; ++j) {   
            x = (x + a.d[i]*b.d[j] + c.d[i+j-1]);    
            c.d[i+j-1] = x%10000;  
            x/=10000;  
        }    
        c.d[i+b.d[0]] = x;  
    }   
    while(c.d[0]>1 && c.d[c.d[0]] == 0) --c.d[0];   
    return c;  
}    

int main(){   
    //freopen("in/1037.in","r",stdin);   
    string a,b;   

    bool symbol_a = true,symbol_b = true ;  
    while(cin>>a>>b){   
        symbol_a = true;  
        symbol_b = true;  
        if(a[0] == '-'){   
            symbol_a = false;   
            a.erase(0,1);  
        }     
        if(b[0] == '-'){   
            symbol_b = false;   
            b.erase(0,1);   
        }     
        BigInt bigA = BigInt(a);  
        BigInt bigB = BigInt(b);  
        BigInt bigC;   

        if(symbol_b == symbol_a){  
            cout<<(symbol_a?"":"-")<<(bigA+bigB).toString()<<endl;  

            if(bigA < bigB){  
                cout<<(symbol_a?"-":"")<<(bigB-bigA).toString()<<endl;  
            }  
            else{  
                cout<<(symbol_a?"":"-")<<(bigA-bigB).toString()<<endl;  
            }  
            cout<<(bigA*bigB).toString()<<endl;  
        }else{  
            if(bigA <bigB){  

                cout<<(symbol_b?"":"-")<<(bigB-bigA).toString()<<endl;  
            }else{  
                cout<<(symbol_a?"":"-")<<(bigA-bigB).toString()<<endl;  
            }  

            cout<<(symbol_a?"":"-")<<(bigA+bigB).toString()<<endl;  
            cout<<"-"<<(bigA*bigB).toString()<<endl;  
        }  

    }    
}