Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef vector<int> num;
- int const base = 1000 * 1000 * 1000;
- class Num
- {
- public:
- vector<int>m;
- read()
- {
- num a;
- string s;
- cin >> s;
- int size = s.size();
- for (int i=(int)s.length(); i>0; i-=9)
- if (i < 9)
- this->m.push_back (atoi (s.substr (0, i).c_str()));
- else
- this->m.push_back (atoi (s.substr (i-9, 9).c_str()));
- //this->show();
- }
- show()
- {
- cout << (this->m.empty() ? 0 : this->m.back());
- int size = this->m.size();
- for (int i = size - 2; i > -1; --i)
- {
- cout << this->m[i];
- }
- }
- vector<int> substract(vector<int> a, vector<int> b)
- {
- // a>=b
- int carry = 0;
- for (int i = 0; i < b.size() || carry; ++i)
- {
- a[i] -= carry + (i < b.size() ? b[i] : 0);
- carry = a[i] < 0;
- if (carry){
- a[i] += base;
- }
- }
- while (a.size() > 1 && a.back() == 0)a.pop_back();
- return a;
- }
- Num operator-(const Num& b)
- {
- Num s;
- if (this->m.size() > b.m.size())
- {
- s.m = substract(this->m, b.m);
- return s;
- }
- if (this->m.size() == b.m.size())
- {
- bool f = 0;
- for (int i = this->m.size() - 1; i > -1; --i)
- {
- if (this->m[i] < b.m[i])
- {
- f = 1;
- break;
- }
- }
- if (!f)
- {
- s.m = substract(this->m, b.m);
- return s;
- }
- s.m = substract(b.m, this->m);
- s.m[s.m.size() - 1] *= -1;
- return s;
- }
- if (this->m.size() < b.m.size())
- {
- s.m = substract(b.m, this->m);
- s.m[s.m.size() - 1] *= -1;
- return s;
- }
- }
- };
- int main()
- {
- Num a;
- a.read();
- Num b;
- b.read();
- Num c = a - b;
- c.show();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement