Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include<bits/stdc++.h>
- #include<iostream>
- #include<fstream>
- #include<string>
- #include<sstream>
- #include<algorithm>
- #include<cmath>
- #define long long
- #define nln '\n'
- using namespace std;
- // Global variables: f1, f2, a, b
- fstream f1, f2;
- inline void openf()
- {
- f1.open("bignum.inp", ios:: in);
- f2.open("bignum.out", ios:: out);
- }
- inline void closef()
- {
- f1.close();
- f2.close();
- }
- string a, b;
- void data()
- {
- //f1.tie(0)->sync_with_stdio(0);
- //f2.tie(0)->sync_with_stdio(0);
- cin.tie(0)->sync_with_stdio(0);
- cin >> a;
- cin >> b;
- }
- void fill(string &a, string &b)
- {
- reverse(a.begin(), a.end());
- reverse(b.begin(), b.end());
- if (a.size() > b.size())
- {
- long leb = b.size();
- for (long i = 0; i != a.size()-leb; ++i)
- b += '0';
- }
- else
- {
- long lea = a.size();
- for (long i = 0; i != b.size()-lea; ++i)
- a += '0';
- }
- }
- string cha2str(char cha)
- {
- string str = "";
- str += cha;
- return str;
- }
- char str2cha(string str)
- {
- char x = str[0];
- return x;
- }
- string num2str(long num)
- {
- string str = "0";
- while (num > 0)
- {
- str += num%10 + '0';
- num = num/10;
- }
- reverse(str.begin(), str.end());
- return str;
- }
- long str2num(string str)
- {
- long num = 0;
- for (long i = 0; i != str.size(); ++i)
- {
- num += (str[i]-'0')*pow(10, str.size()-i-1);
- }
- return num;
- }
- string sum(string a, string b)
- {
- if (a.size() == b.size())
- {
- reverse(a.begin(), a.end());
- reverse(b.begin(), b.end());
- }
- else
- fill(a, b);
- string res = "";
- long rem = 0;
- for (long i = 0; i != a.size(); ++i)
- {
- long tol = str2num(cha2str(a[i])) + str2num(cha2str(b[i])) + rem;
- res += str2cha(num2str(tol%10));
- rem = tol/10;
- }
- if (rem != 0)
- res += str2cha(num2str(rem));
- reverse(res.begin(), res.end());
- return res;
- }
- void swap(string &a, string &b)
- {
- string itm = a;
- a = b;
- b = itm;
- }
- string sub(string a, string b)
- {
- bool neg = 0;
- if (a.size() == b.size())
- {
- reverse(a.begin(), a.end());
- reverse(b.begin(), b.end());
- }
- else
- fill(a, b);
- string tea = a, teb = b;
- reverse(tea.begin(), tea.end());
- reverse(teb.begin(), teb.end());
- if (tea < teb)
- {
- neg = 1;
- swap(a, b);
- }
- long rem = 0;
- string res = "";
- for (long i = 0; i != a.size(); ++i)
- {
- long x = str2num(cha2str(a[i]));
- long y = str2num(cha2str(b[i]));
- long tol = x - y - rem;
- if (tol < 0)
- {
- res += str2cha(num2str((tol+10) % 10));
- rem = 1;
- }
- else
- {
- res += str2cha(num2str(tol % 10));
- rem = 0;
- }
- }
- while (res[res.size()-1] == '0' && res.size() != 1)
- res.erase(res.size()-1, 1);
- if (neg)
- res += '-';
- reverse(res.begin(), res.end());
- return res;
- }
- string mul1(string a, string b)
- {
- long y = str2num(b);
- string res = "";
- long rem = 0;
- for (long i = a.size()-1; i+1 != 0; --i)
- {
- long x = str2num(cha2str(a[i]));
- long tol = x*y+rem;
- res += str2cha(num2str(tol % 10));
- rem = tol / 10;
- }
- if (rem != 0)
- res += str2cha(num2str(rem));
- reverse(res.begin(), res.end());
- return res;
- }
- string mul(string a, string b)
- {
- string res = "";
- for (long i = b.size()-1; i+1 != 0; --i)
- {
- string tem = mul1(a, cha2str(b[i]));
- for (long j = 0; j != b.size()-i-1; ++j)
- tem += '0';
- res = sum(res, tem);
- }
- return res;
- }
- void process()
- {
- }
- void printans(string ans)
- {
- for (long i = 0; i != ans.size(); ++i)
- cout << ans[i];
- cout << nln;
- }
- void view()
- {
- string an1 = sum(a, b);
- string an2 = sub(a, b);
- string an3 = mul(a, b);
- printans(an1);
- printans(an2);
- printans(an3);
- }
- int main()
- {
- //openf();
- data();
- process();
- view();
- //closef();
- }
Advertisement
Add Comment
Please, Sign In to add comment