Problem B: 大整数的加法运算 升级版

#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];
string jj(string a,string b)
{
    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 s;
}
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 cs=jj(T1.s1,T2.s1);
    return Decimal(cs);
    }
    Decimal &operator ++()
    {
        string t="1";
        string tt=jj(t,s1);
        s1=tt;
        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;
}

  

上一篇:pyc反编译-uncompyle2的安装及使用


下一篇:TPS低,CPU高--记一次storm压测问题排查过程