作业——大整数的加法运算

Problem B: 大整数的加法运算
我们知道,C++中的整数类型,如short、int、long和long long等都有确定的表示范围,超大的整数是不能表示的。请定义一个类Decimal,用于表示大整数,并实现如下方法:
1.根据给出的main函数定义的构造函数。
2. 重载加法(“+”)运算符,可以实现一个Decimal对象与另一个Decimal对象求和、与一个int类型的数值求和。
3. 重载前缀自增运算符。
4. 重载下标运算符,用于求当前对象的指定下标位置的数字。
5. 重载输入、输出运算符。
Input
输入3个数,前2个可能会超出unsigned long long的表示范围,最后1个是一个int类型的非负整数。
不考虑负数。
Output
见样例。
Sample Input
876543210012345678889
123456789987654321111
15
Sample Output
a = 876543210012345678889
b = 123456789987654321111
i = 15
a = 876543210012345678890
c = 1000000000000000000000
d = 876543210012345678890
e = 123456789987654321126
f = 554433
g = 12345
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
HINT

int main()
{
    Decimal a, b, c, d, e, f("554433"), g(12345);
    int i;
    cin>>a>>b>>i;
    cout<<"a = "<<a<<endl;
    cout<<"b = "<<b<<endl;
    cout<<"i = "<<i<<endl;
    c = a + b;
    d = ++a;
    e = b + i;
    cout<<"a = "<<a<<endl;
    cout<<"c = "<<c<<endl;
    cout<<"d = "<<d<<endl;
    cout<<"e = "<<e<<endl;
    cout<<"f = "<<f<<endl;
    cout<<"g = "<<g<<endl;
 
    cout<<c[0];
    for (i = 1; i < c.getLength(); i++)
    {
        cout<<" "<<c[i];
    }
    cout<<endl;
    return 0;
}

AC代码



#include <bits/stdc++.h>
 
using namespace std;
static int flag=0;
string to_string(int a)//OJ不支持c++11中的to_string所以这里重写
{
    string s;
    while(a>=10)
    {
        char c=a%10+'0';
        a=a/10;
        s.push_back(c);
    }
     char c=a+'0';
     s.push_back(c);
    reverse(s.begin(),s.end());//倒置
    return s;
 
}
class Decimal
{
    string s;
    friend istream &operator>>(istream &is,Decimal &k);
    friend ostream &operator<<(ostream &os,Decimal &k);
    friend Decimal operator+(Decimal &a ,Decimal &b);
    friend Decimal operator+(Decimal &a ,int b);
 
public:
    Decimal(string s_){s=s_;}
     Decimal(){}
    Decimal(int s_){s=to_string(s_);}
    Decimal &operator++()
    {
        int m=s.size();
        string temp;
        flag=1;
        for(int i=1;i<=m;i++)
           {
           int f=s[m-i]-'0'+flag;
           flag=0;
           if(f>=10)
              flag=1;
              char c=f%10+'0';
            temp.push_back(c);
            }
           if(flag==1)
            temp.push_back('1');
             reverse(temp.begin(),temp.end());
             s=temp;
         return *this;
 
    }
    int getLength()
    {
        return s.size();
    }
    char operator[](int i)
    {
        return s[i];
    }
};
Decimal operator+(Decimal &a ,Decimal &b)
    {
        Decimal t;
       int m=a.s.size();
       int n=b.s.size();
       int max=m>n?m:n;
       int min=m<n?m:n;
       for(int i=1;i<=min;i++)
       {
           int f=a.s[m-i]-'0'+b.s[n-i]-'0'+flag;
           flag=0;
           if(f>=10)
              flag=1;
            char c=f%10+'0';
           t.s.push_back(c);
       }
       if(m>=n)
       {
           for(int i=min+1;i<=max;i++)
           {
           int f=a.s[m-i]-'0'+flag;
           flag=0;
           if(f>=10)
              flag=1;
            char c=f%10+'0';
            t.s.push_back(c);//尾部插入字符
            }
           if(flag==1)
              t.s.push_back('1');
       }
       if(m<n)
       {
          for(int i=min+1;i<=max;i++)
          {
           int f=b.s[n-i]-'0'+flag;
           flag=0;
           if(f>=10)
              flag=1;
              char c=f%10+'0';
            t.s.push_back(c);
           }
            if(flag==1)
           t.s.push_back('1');
       }
        reverse(t.s.begin(),t.s.end());
        return t;
    }
    Decimal operator+(Decimal &a ,int k)
    {
        Decimal t,b;
        b.s=to_string(k);
       int m=a.s.size();
       int n=b.s.size();
       int max=m>n?m:n;
       int min=m<n?m:n;
       for(int i=1;i<=min;i++)
       {
           int f=a.s[m-i]-'0'+b.s[n-i]-'0'+flag;
           flag=0;
           if(f>=10)
              flag=1;
            char c=f%10+'0';
           t.s.push_back(c);
       }
       if(m>=n)
       {
           for(int i=min+1;i<=max;i++)
           {
           int f=a.s[m-i]-'0'+flag;
           flag=0;
           if(f>=10)
              flag=1;
            char c=f%10+'0';
            t.s.push_back(c);
            }
           if(flag==1)
              t.s.push_back('1');
       }
       if(m<n)
       {
          for(int i=min+1;i<=max;i++)
          {
           int f=b.s[n-i]-'0'+flag;
           flag=0;
           if(f>=10)
              flag=1;
              char c=f%10+'0';
            t.s.push_back(c);
           }
            if(flag==1)
           t.s.push_back('1');
       }
        reverse(t.s.begin(),t.s.end());
        return t;
    }
 ostream &operator<<(ostream &os,Decimal &k)
 {
     os<<k.s;
     return os;
 }
 istream &operator>>(istream &is,Decimal &k)
 {
     is>>k.s;
     return is;
 }
int main()
{
    Decimal a, b, c, d, e, f("554433"), g(12345);
    int i;
    cin>>a>>b>>i;
    cout<<"a = "<<a<<endl;
    cout<<"b = "<<b<<endl;
    cout<<"i = "<<i<<endl;
    c = a + b;
    d = ++a;
    e = b + i;
    cout<<"a = "<<a<<endl;
    cout<<"c = "<<c<<endl;
    cout<<"d = "<<d<<endl;
    cout<<"e = "<<e<<endl;
    cout<<"f = "<<f<<endl;
    cout<<"g = "<<g<<endl;
 
    cout<<c[0];
    for (i = 1; i < c.getLength(); i++)
    {
        cout<<" "<<c[i];
    }
    cout<<endl;
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值