Problem B: 大整数的加法运算

来源:互联网 发布:迅雷cdn 知乎 编辑:程序博客网 时间:2024/05/01 22:47

Problem B: 大整数的加法运算

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 334  Solved: 226
[Submit][Status][Web Board]

Description

我们知道,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

87654321001234567888912345678998765432111115

Sample Output

a = 876543210012345678889b = 123456789987654321111i = 15a = 876543210012345678890c = 1000000000000000000000d = 876543210012345678890e = 123456789987654321126f = 554433g = 123451 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

HINT

Append Code

append.cc,

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <vector>#include <sstream>#include <string>using namespace std;int a[1001],b[1001];char c[1001];class Decimal{public:    string s1;    Decimal(string a=""):s1(a){}    Decimal(unsigned long long int n)    {    stringstream ss;    string str;    ss<<n;    ss>>str;    s1=str;    }    friend istream &operator >>(istream & it,Decimal &T)    {        it>>T.s1;        return it;    }    friend ostream &operator << (ostream & os,const Decimal &T)    {        os<<T.s1;        return os;    }    friend Decimal operator + ( Decimal T1,Decimal T2)    {    string a = T1.s1;    string b = T2.s1;    int i1 = a.size() - 1;    int i2 = b.size() - 1;    string s;    int carry = 0;    while (i1 >= 0 || i2 >= 0)    {        char ch = carry;        if (i1 >= 0)        {            if (a[i1] < '0' || a[i1] > '9')                continue;            ch += a[i1] - '0';        }        if (i2 >= 0)        {            if (b[i2] < '0' || b[i2] > '9')                continue;            ch += b[i2] - '0';        }        if (ch >= 10)        {            carry = 1;            ch -= 10;        }        else carry = 0;        s.push_back(ch + '0');        i1--;        i2--;    }    if (carry) s.push_back('1');    reverse(s.begin(), s.end());    return Decimal(s);    }    Decimal &operator ++()    {   string &t=s1;        int l=t.size();        l--;        for(int i=l;i>=0;i--)        {            if(t[i]=='9')            t[i]='0';            else            {                t[i]+=1;                break;            }        }        return *this;    }    char operator [](int i)    {        return s1[i];    }    int getLength()    {        return s1.size();    }};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;}

1 0
原创粉丝点击