Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <cassert>
- #include <cctype>
- using namespace std;
- const int BIGINT_SIZE(100);
- typedef unsigned int BIGINT[BIGINT_SIZE];
- istream & operator >>(istream & is, BIGINT & b)
- {
- string s;
- is >> s;
- for (int i = 0; i < BIGINT_SIZE; ++i)
- b[i] = 0;
- for (int i = 0; i < s.length(); ++i)
- {
- assert(isdigit(s[i]));
- b[s.length() - 1 - i] = s[i] - '0';
- }
- return is;
- }
- ostream & operator <<(ostream & os, const BIGINT b)
- {
- int i;
- for (i = BIGINT_SIZE - 1; i >= 0 && b[i] == 0; --i)
- ;
- if (i < 0) // number zero
- os << "0";
- else
- while (i >= 0)
- os << b[i--];
- return os;
- }
- void add(const BIGINT x, const BIGINT y, BIGINT answer)
- {
- for (int i = 0; i < BIGINT_SIZE; ++i)
- answer[i] = 0;
- unsigned int carry(0);
- for (int i = 0; i < BIGINT_SIZE; ++i)
- {
- int sum = x[i] + y[i] + carry;
- answer[i] = sum % 10;
- carry = sum / 10;
- }
- }
- int main()
- {
- BIGINT b, c, a;
- cout << "Enter two bigint: ";
- cin >> b >> c;
- cout << "Read bigint: " << b << ", " << c << endl;
- add(b, c, a);
- cout << "Addition: " << a << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement