Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <iomanip>
- #include <cassert>
- using namespace std;
- class BigInt
- {
- private:
- vector<unsigned> Data; //ιεΊεζΎ
- //BigInt(vector<unsigned>&& data): Data(data) {}
- //BigInt(vector<unsigned>::size_type size, unsigned val):Data(size, val) {}
- public:
- BigInt(): Data(1, 0) {};
- BigInt(unsigned Num): Data(1, Num % 1000000000)
- {
- if (Num > 1000000000)
- Data.push_back(Num / 1000000000);
- }
- BigInt(const string& Num)
- {
- string::size_type len=Num.size();
- Data.reserve(len / 9);
- while (len>9)
- {
- len-=9;
- Data.push_back(atol(Num.substr(len, 9).c_str()));
- }
- Data.push_back(atol((Num.substr(0, len)).c_str()));
- }
- /*
- BigInt& operator *=(unsigned rhs)
- {
- unsigned carry=0;
- for (vector<unsigned>::iterator iter=Data.begin(); iter!=Data.end(); ++iter)
- {
- unsigned long long tmp=(unsigned long long)(*iter)*rhs+carry;
- *iter=unsigned(tmp % 1000000000);
- carry=unsigned(tmp / 1000000000);
- }
- if (carry)
- Data.push_back(carry);
- return *this;
- }
- BigInt& operator /=(unsigned rhs)
- {
- unsigned long long carry=0;
- for (vector<unsigned>::reverse_iterator rit=Data.rbegin(); rit!=Data.rend(); ++rit)
- {
- unsigned long long tmp=*rit+carry*1000000000;
- *rit =unsigned(tmp / rhs);
- carry=unsigned(tmp % rhs);
- }
- if (Data.back()==0)
- Data.erase(Data.end()-1);
- return *this;
- }
- */
- BigInt& operator +=(const BigInt& rhs)
- {
- bool carry=0;
- if (Data.size() < rhs.Data.size())
- Data.resize(rhs.Data.size());
- vector<unsigned>::iterator it1=Data.begin();
- for (vector<unsigned>::const_iterator it2=rhs.Data.begin(); it2!=rhs.Data.end(); ++it1, ++it2)
- {
- unsigned tmp=*it1 + *it2 + carry;
- *it1 =tmp % 1000000000;
- carry=tmp > 999999999;
- }
- for (; carry && it1!=Data.end(); ++it1)
- {
- unsigned tmp=*it1 + carry;
- *it1 =tmp % 1000000000;
- carry=tmp > 999999999;
- }
- if (carry)
- Data.push_back(1);
- return *this;
- }
- /*
- BigInt& operator *=(const BigInt& rhs)
- {
- assert(this!=&rhs);
- BigInt result;
- for (vector<unsigned>::size_type i=0; i<rhs.Data.size(); ++i)
- {
- BigInt temp=*this;
- temp*=rhs.Data[i];
- temp.Data.insert(temp.Data.begin(), i, 0);
- result+=temp;
- }
- return *this=move(result);
- }
- string toString() const
- {
- ostringstream oss;
- oss << *this;
- return oss.str();
- }
- bool operator ==(const BigInt& rhs) const
- {
- return Data==rhs.Data;
- }
- */
- friend ostream& operator <<(ostream& os, const BigInt& rhs)
- {
- vector<unsigned>::const_reverse_iterator riter=rhs.Data.rbegin();
- os << *riter;
- for (++riter; riter!=rhs.Data.rend(); ++riter)
- os << setw(9) << setfill('0') << *riter;
- return os;
- }
- friend istream& operator >>(istream& is, BigInt& lhs)
- {
- string s;
- cin >> s;
- lhs=BigInt(s);
- return is;
- }
- };
- int main()
- {
- unsigned n;
- BigInt a, b;
- cin >> n;
- for (unsigned i=1; i<=n; ++i)
- {
- cin >> a >> b;
- cout << "Case " << i << ":\n" << a << " + " << b << " = ";
- a+=b;
- cout << a << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement