Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- inline int toInt(char s) { return static_cast<int>(s) - 48; }
- inline int toChar(int n) { return static_cast<char>(n + 48); }
- void removeLeadingZeros(string &a)
- {
- int k; for (k = 0; k < a.size(); k++) if (a[k] != '0') break;
- a.erase(0, k);
- if (a.size() == 0) a = "0";
- }
- string add(string &a, string &b)
- {
- string res = "";
- if (a.size() < b.size()) swap(a, b);
- int ad = 0, i, j, r;
- for (i = a.size()-1, j = b.size()-1; j >= 0; i--, j--)
- {
- r = toInt(a[i]) + toInt(b[j]) + ad;
- if (r >= 10) { res.insert(res.begin(), toChar(r%10)); ad = r/10; }
- else { res.insert(res.begin(), toChar(r)); ad = 0; }
- }
- while (ad)
- {
- if (i >= 0) r = toInt(a[i--]) + ad;
- else r = ad;
- if (r >= 10) { res.insert(res.begin(), toChar(r%10)); ad = r/10; }
- else { res.insert(res.begin(), toChar(r)); ad = 0; }
- }
- while (i >= 0) { res.insert(res.begin(), a[i]); i--; }
- return res;
- }
- string sub(string &a, string &b)
- {
- string res = "";
- int su = 0, i, j, r;
- for (i = a.size()-1, j = b.size()-1; j >= 0; i--, j--)
- {
- r = toInt(a[i]) - toInt(b[j]) - su;
- if (r < 0) { r += 10; res.insert(res.begin(), toChar(r)); su = 1; }
- else { res.insert(res.begin(), toChar(r)); su = 0; }
- }
- while (su)
- {
- r = toInt(a[i--]) - su;
- if (r < 0) { r += 10; res.insert(res.begin(), toChar(r)); su = 1; }
- else { res.insert(res.begin(), toChar(r)); su = 0; }
- }
- while (i >= 0) { res.insert(res.begin(), a[i]); i--; }
- removeLeadingZeros(res);
- return res;
- }
- string mul(string &a, string &b)
- {
- vector<string> res(b.size());
- string resultat;
- int ad;
- for (int i = b.size()-1, k = 0; i >= 0; i--, k++)
- {
- ad = 0;
- for (int j = a.size()-1; j >= 0; j--)
- {
- int r = toInt(b[i])*toInt(a[j]) + ad;
- if (r >= 10) { res[k].insert(res[k].begin(), toChar(r%10)); ad = r/10; }
- else { res[k].insert(res[k].begin(), toChar(r)); ad = 0; }
- }
- res[k].insert(res[k].begin(), toChar(ad));
- removeLeadingZeros(res[k]);
- string tmp = res[k];
- for (int l = 0; l < k; l++) tmp.push_back('0');
- resultat = add(resultat, tmp);
- removeLeadingZeros(resultat);
- }
- return resultat;
- }
- int main()
- {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement