题目1037: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

C++代码:

#include<iostream>
#include<string>

using namespace std;

int a[500];
int b[500];
int sum[1000];
int sub[1000];
int mul[1000];

bool g(int a1[500],int b1[500]){
    for(int i=499;i>=0;i--){
        if(a1[i]>b1[i])
            return true;
        if(a1[i]<b1[i])
            return false;
    }
    return true;
}

void add(){
    int c=0;
    for(int i=0;i<500;i++){
       sum[i]=(a[i]+b[i]+c)%10;
       c=(a[i]+b[i]+c)/10;
    }
}
void dec(){
    int c=0;
    if(g(a,b)){
        for(int i=0;i<500;i++){
            if(a[i]-c>=b[i]){
                sub[i]=a[i]-c-b[i];
                c=0;
            }else{
                sub[i]=a[i]-c+10-b[i];
                c=1;
            }
        }
    }else{
        for(int i=0;i<500;i++){
            if(b[i]-c>=a[i]){
                sub[i]=b[i]-c-a[i];
                c=0;
            }else{
                sub[i]=b[i]-c+10-a[i];
                c=1;
            }
        }
    }
}
void multi(){
    int c=0;
    for(int i=0;i<500;i++)
      for(int j=0;j<500;j++){
        int tmp = mul[i+j];
         mul[i+j]=(a[i]*b[j]+c+mul[i+j])%10;
         c=(a[i]*b[j]+c+tmp)/10;
      }
}
void showsum(string m1,string m2){
    int ind=499;
        if(m1=="+"&&m2=="+"||m1=="-"&&m2=="-"){
          for(;ind>=0;ind--){
           if(sum[ind]!=0)
            break;
          }
        if(ind<0){
            cout<<0<<endl;
        }else{
          if(m1=="-"&&m2=="-")
          cout<<"-";
          for(int i=ind;i>=0;i--){
          cout<<sum[i];
          }
          cout<<endl;
        }
        }else{
            for(;ind>=0;ind--){
           if(sub[ind]!=0)
            break;
          }
          if(ind<0){
            cout<<0<<endl;
          }else{
            if(m1=="+"&&m2=="-"&&!g(a,b)||m1=="-"&&m2=="+"&&g(a,b)){
                cout<<"-";
            }
            for(int i=ind;i>=0;i--){
            cout<<sub[i];
            }
            cout<<endl;
          }
        }
}
void showdec(string m1,string m2){
    int ind=499;
        if(m1=="+"&&m2=="+"||m1=="-"&&m2=="-"){
         for(;ind>=0;ind--){
          if(sub[ind]!=0)
            break;
         }
         if(ind<0){
            cout<<0<<endl;
         }else{
          if(m1=="+"&&m2=="+"&&!g(a,b)||m1=="-"&&m2=="-"&&g(a,b)){
            cout<<"-";
          }
          for(int i=ind;i>=0;i--){
          cout<<sub[i];
          }
          cout<<endl;
         }
        }else{
        for(;ind>=0;ind--){
          if(sum[ind]!=0)
            break;
         }
        if(ind<0){
           cout<<0<<endl;
        }else{
          if(m1=="-"&&m2=="+")
            cout<<"-";
            for(int i=ind;i>=0;i--){
            cout<<sum[i];
            }
            cout<<endl;
          }
        }
}
void showmul(string m1,string m2){
    int ind=999;
    for(;ind>=0;ind--){
        if(mul[ind]!=0)
            break;
    }
    if(ind<0){
        cout<<0<<endl;
    }else{
       if(m1=="+"&&m2=="-"||m1=="-"&&m2=="+"){
          cout<<"-";
        }
        for(int i=ind;i>=0;i--){
        cout<<mul[i];
        }
        cout<<endl;
    }
}
int main(){
    string aa;
    string bb;
    while(cin>>aa>>bb){
        string m1,m2;
        int j=0;
        for(int i=aa.length()-1;i>0;i--){
            a[j++]=aa.at(i)-'0';
        }
        if(aa.at(0)=='-'){
             m1="-";
        }
        else{
             m1="+";
             a[j]=aa.at(0)-'0';
        }
        j=0;
        for(int i=bb.length()-1;i>0;i--){
            b[j++]=bb.at(i)-'0';
        }
        if(bb.at(0)=='-'){
            m2="-";
        }else{
            m2="+";
            b[j]=bb.at(0)-'0';
        }
        add();
        dec();
        multi();
        showsum(m1,m2);
        showdec(m1,m2);
        showmul(m1,m2);
        for(int i=0;i<500;i++){
            a[i]=0;
            b[i]=0;
        }
        for(int i=0;i<1000;i++){
            sum[i]=0;
            sub[i]=0;
            mul[i]=0;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值