蒟蒻看了一天了,遗留了一些问题qwq标在注释里了QAQ
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <deque>
#include <iterator>
#include <algorithm>
using namespace std;
typedef long long ll;
class Bigint {
public:
Bigint():negative(false) {}
Bigint(const ll);
Bigint(const char*);
Bigint(const string);
Bigint(const Bigint &);
Bigint & operator = (const Bigint &);
friend istream & operator >> (istream &, Bigint &);
friend ostream & operator << (ostream &, const Bigint &);
Bigint operator + (const Bigint &) const;
Bigint operator - (const Bigint &) const;
Bigint operator * (const Bigint &) const;
Bigint operator / (const ll &) const;
ll operator % (const ll &) const;
bool operator > (const Bigint &) const;
bool operator == (const Bigint &) const;
bool operator >= (const Bigint &) const;
friend Bigint abs(const Bigint &);
Bigint operator - () const;
private:
deque<int> num;
bool negative;
};
Bigint::Bigint(const ll x) {
ll t = abs(x);
negative = x >= 0 ? true : false;
while (t > 0) {
num.push_back(t % 10);
t /= 10;
}
}
Bigint::Bigint(const char* str) {
unsigned i = str[0] == '-' ? 1 : 0;
this->negative = str[0] == '-' ? true : false;
for (; i < strlen(str); ++i) num.push_back(str[i] - '0');
}
Bigint::Bigint(const string str) {
unsigned i = str[0] == '-' ? 1 : 0;
this->negative = str[0] == '-' ? true : false;
for (; i < str.size(); ++i) num.push_back(str[i] - '0');
}//为什么要有三种初始化Bigint类中的num?只有string初始化不就够了吗?
Bigint::Bigint(const Bigint &x) {
negative = x.negative;
num = x.num;
}
Bigint & Bigint::operator = (const Bigint &x) {
negative = x.negative;
num = x.num;
return (*this);
}
istream & operator >> (istream &is, Bigint & x) {
string str;
is >> str;
x = str;
return is;
}
ostream & operator << (ostream &os, const Bigint &x) {
if (x.negative) os << '-';
for (unsigned i = 0; i < x.num.size(); ++i)
os << x.num[i];
return os;
}
bool Bigint::operator > (const Bigint & rhs) const {
Bigint x = (*this), y = rhs;
if (!x.negative && y.negative) return true;
if (x.negative && !y.negative) return false;
if (x.negative && y.negative) swap(x, y);
if (x.num.size() > y.num.size()) return true;
if (x.num.size() < y.num.size()) return false;
for (unsigned i = 0; i < x.num.size(); ++i) {
if (x.num[i] > y.num[i]) return true;
if (x.num[i] < y.num[i]) return false;
}
return false;
}
bool Bigint::operator == (const Bigint & rhs) const {
return negative == rhs.negative && num == rhs.num;
}
bool Bigint::operator >= (const Bigint & rhs) const {
return *this > rhs || *this == rhs;
}
Bigint abs(const Bigint & rhs) {
Bigint res;
res.negative = false;
res.num = rhs.num;
return res;
}
//Bigint Bigint::operator - () const {
// Bigint ret = *this;
// ret.negative = !ret.negative;
// return ret;
//}
//把上面五行注释掉,会在123行显示Error:undefined reference to `Bigint::operator-() const',但是重载过后的abs函数返回是Bigint类型,为什么还需要重载-()呢?
Bigint Bigint::operator + (const Bigint & y) const {
if (!this->negative && y.negative) return *this - abs(y);
if (this->negative && !y.negative) return y - abs(*this);
if (this->negative && y.negative) return -abs(*this) - abs(y);
Bigint x = *this, res;
int temp = 0;
for (int i = x.num.size() - 1, j = y.num.size() - 1; i >= 0 || j >= 0; --i, --j) {
int a = i < 0 ? 0 : x.num[i];
int b = j < 0 ? 0 : y.num[j];
res.num.push_front((a + b + temp) % 10);
temp = (a + b + temp) / 10;
}
if (temp != 0) res.num.push_front(temp);
return res;
}
Bigint Bigint::operator * (const Bigint & y) const {
deque<int> a, b, res;
copy(this->num.begin(), this->num.end(), front_inserter(a));
copy(y.num.begin(), y.num.end(), front_inserter(b));
res.resize(a.size() + b.size() + 5);//这里为什么是扩大5呢?
for (unsigned i = 0; i < a.size(); ++i) for (unsigned j = 0; j < b.size(); ++j)//
res[i + j] += a[i] * b[j];
for (unsigned i = 0; i < res.size() - 1; ++i) {//循环变量为什么设置成unsigned呢?后面的循环变量也是这么操作的
res[i + 1] += res[i] / 10;
res[i] %= 10;
}
while (res.size() >= 2 && res.back() == 0)
res.pop_back();
reverse(res.begin(), res.end());
Bigint ret;
ret.negative = this->negative ^ y.negative;
ret.num = res;
return ret;
}
Bigint Bigint::operator - (const Bigint & y) const {
if (!this->negative && y.negative) return *this + abs(y);
if (this->negative && !y.negative) return -abs(*this) - y;
if (this->negative && y.negative) return abs(y) - abs(*this);
deque<int> a, b, res;
Bigint ret;
copy(this->num.begin(), this->num.end(), front_inserter(a));
copy(y.num.begin(), y.num.end(), front_inserter(b));
if (y > *this) swap(a, b), ret.negative = true;
res.resize(max(a.size(), b.size()) + 5);//res的size不应该小于等于max(a.size(), b.size())吗,resize为啥是+5
for (unsigned i = 0, j = 0; i < a.size() || j < b.size(); ++i, ++j) {
int m = i < a.size() ? a[i] : 0;
int n = j < b.size() ? b[j] : 0;
res[i] = m - n;
}
for (unsigned i = 0; i < res.size() - 1; ++i) if (res[i] < 0) {
res[i] += 10;
--res[i + 1];
}
while (res.size() >= 2 && res.back() == 0)
res.pop_back();
reverse(res.begin(), res.end());
ret.num = res;
return ret;
}
Bigint Bigint::operator / (const ll & y) const {
ll temp = 0;
Bigint x = (*this), res;
for (unsigned i = 0; i < x.num.size(); ++i) {
temp = temp * 10 + x.num[i];
res.num.push_back((int)(temp / y));
temp %= y;
}
while (res.num.size() >= 2 && res.num.front() == 0)
res.num.pop_front();
return res;
}
ll Bigint::operator % (const ll & y) const {
ll res = 0;
for (unsigned i = 0; i < this->num.size(); ++i)
res = (res * 10 + this->num[i]) % y;
return res;
}
int main() {
Bigint a, b;
while (cin >> a >> b)
cout << a+b << a-b << endl;
return 0;
}