1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| #include <cstdio> #include <iostream> #include <cstring> #include <sstream> #include <algorithm> using namespace std;
class number { public: string num; bool neg = false; number(string &n) { num = n; } number(long long n) { num = to_string(n); }
number(int n) { num = to_string(n); }
string get() { return num; } friend ostream &operator<<(ostream&, const number&); friend number operator+(const number&, const number&); friend bool operator<(const number&, const number&); friend number operator-(const number&, const number&); friend number operator*(const number&, const int); friend number operator*(const number&, const number&); friend number operator/(const number&, const number&); };
ostream &operator<<(ostream &out, const number &a) { if (a.neg) out << '-'; out << a.num; return out; }
number operator+(const number &a, const number &b) { stringstream ss; auto x = a.num.rbegin(), y = b.num.rbegin(); int c = 0; while (x != a.num.rend() || y != b.num.rend() || c > 0) { if (x != a.num.rend()) { c += (*x) - '0'; ++x; } if (y != b.num.rend()) { c += (*y) - '0'; ++y; } ss << c % 10; c /= 10; } string ans = ss.str(); reverse(ans.begin(), ans.end()); return number(ans); } bool operator<(const number &a, const number &b) { int n1 = a.num.size(), n2 = b.num.size(); if (n1 != n2) return n1 < n2; for (auto x = a.num.begin(), y = b.num.begin(); x != a.num.end(); ++x, ++y) { if ((*x) != (*y)) return (*x) < (*y); } return false; } bool operator>=(const number &a, const number &b) { int n1 = a.num.size(), n2 = b.num.size(); if (n1 != n2) return n1 > n2; for (auto x = a.num.begin(), y = b.num.begin(); x != a.num.end(); ++x, ++y) { if ((*x) != (*y)) return (*x) > (*y); } return true; }
number operator-(const number &a, const number &b) { if (a < b) { auto ans = b - a; ans.neg = true; return ans; } stringstream ss; auto x = a.num.rbegin(), y = b.num.rbegin(); for (int c = 0; x != a.num.rend(); ++x) { c = (*x) - '0' - c; if (y != b.num.rend()) { c -= (*y) - '0'; ++y; } ss << (c + 10) % 10; if (c < 0) c = 1; else c = 0; } string ans = ss.str(); while (ans.back() == '0') ans.pop_back(); reverse(ans.begin(), ans.end()); if (ans.size() == 0) return number(0); return number(ans); }
number operator*(const number &a, const int b) { if (b == 0) return number(0); stringstream ss; int c = 0; for (auto x = a.num.rbegin(); x != a.num.rend() || c != 0; c /= 10) { if (x != a.num.rend()){ c = (*x - '0') * b + c; ++x; } ss << c % 10; } string ans = ss.str(); reverse(ans.begin(), ans.end()); return number(ans); }
number operator*(const number &a, const number &b) { number ans(0); int cnt = 0; for (auto x = b.num.rbegin(); x != b.num.rend(); ++x, ++cnt) { auto tmp = a * (*x - '0'); if (tmp.num == "0") continue; for (int i = 0; i < cnt; ++i) tmp.num.push_back('0'); ans = ans + tmp; } return ans; }
number operator/(const number &a, const number &b) { if (a < b) return number(0); string tmp = a.num.substr(0, b.num.size()); number base(tmp); stringstream ss; for (auto x = a.num.begin() + b.num.size(); ; x++) { int cnt = 0; while (base >= b * (cnt + 1)) ++cnt; ss << cnt; if (x == a.num.end()) break; base = base - (b * cnt); if (base.num == "0") base.num = *x; else base.num.push_back(*x); } string ans = ss.str(); reverse(ans.begin(), ans.end()); while (ans.back() == '0') ans.pop_back(); reverse(ans.begin(), ans.end()); return number(ans); }
int main() { string a, b; cin >> a >> b; cout << number(a) + number(b) << '\n'; cout << number(a) - number(b) << '\n'; cout << number(a) * number(b) << '\n'; cout << number(a) / number(b) << '\n'; return 0; }
|