#include #include using namespace std; class bigint: public vector { // I am definitng a new class called "bigint", but // it borrow and contains the definitions of everything // that is inside a vector // called INHERITANCE public: bool negative; bigint() { negative = false; } void become(string s) { clear(); int end = 0; if (s[0] == '-') { end = 1; negative = true; } else if (s[0] == '+') end = 1; for (int i = s.length()-1; i >= end; i -= 1) { int digval = s[i] - '0'; if (digval < 0 || digval > 9) { cerr << "invalid digit\n"; exit(1); } push_back(digval); } } void debug() { for (int i = 0; i < size(); i += 1) cout << i << ": " << at(i) << ", "; if (negative) cout << "(negative)"; cout << "\n"; } void print() { if (negative) cout << "-"; for (int i = size()-1; i >= 0; i -= 1) cout << at(i); cout << "\n"; } int get_digit(int pos) { if (pos >= size()) return 0; return at(pos); } void set_digit(int pos, int newval) { if (pos == size()) push_back(newval); else at(pos) = newval; } void increment() { int pos = 0; while (get_digit(pos) == 9) { set_digit(pos, 0); pos += 1; } set_digit(pos, get_digit(pos)+1); } void nines_comp() { for (int i = 0; i < size(); i += 1) set_digit(i, 9 - get_digit(i)); } void trim_leading_zeros() { cout << "trim_leading_zeros still needs to be written\n"; } void add_ignoring_signs(bigint & A, bigint & B) // assumes A and B are positive, even if they aren't { // reference just to avoid wasting time coping, // we will not change A or B clear(); int pos = 0, carry = 0; while (pos < A.size() || pos < B.size() || carry > 0) { int sum = A.get_digit(pos) + B.get_digit(pos) + carry; carry = sum / 10; push_back(sum % 10); pos += 1; } } void subtract_ignoring_signs(bigint & A, bigint & B) { // reference just to avoid wasting time coping, // we will not change A or B clear(); int pos = 0, borrow = 0; while (pos < A.size() || pos < B.size()) { int diff = A.get_digit(pos) - B.get_digit(pos) - borrow; if (diff < 0) { diff += 10; borrow = 1; } else borrow = 0; push_back(diff); pos += 1; } if (borrow != 0) { cout << "Borrow still set\n"; nines_comp(); increment(); negative = true; } trim_leading_zeros(); } void add(bigint & A, bigint & B) { if (A.negative && B.negative) { add_ignoring_signs(A, B); negative = true; } else if (A.negative) subtract_ignoring_signs(B, A); else if (B.negative) subtract_ignoring_signs(A, B); else add_ignoring_signs(A, B); } }; int main() { bigint A, B, C; while (true) { string s; cin >> s; A.become(s); cin >> s; B.become(s); C.add(A, B); C.print(); } }