Skip to content

Instantly share code, notes, and snippets.

@wuerges
Last active October 30, 2016 22:02
Show Gist options
  • Save wuerges/4fee388eab7b5d3377d42a2cfa3bab95 to your computer and use it in GitHub Desktop.
Save wuerges/4fee388eab7b5d3377d42a2cfa3bab95 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <vector>
#include <deque>
using namespace std;
bool is_digit(const char c) {
return (c >= '0' && c <='9');
}
struct BI {
vector<int> number;
void load_vi(deque<char> & vi) {
stringstream ss;
for (char c : vi) {
ss << c;
}
int n;
ss >> n;
number.push_back(n);
vi.clear();
}
friend istream & operator>>(istream& in, BI & bi) {
deque<char> n;
char c;
while (is_digit(in.peek()) && in.get(c)) {
n.push_front(c);
}
deque<char> vi;
for(char ci : n) {
vi.push_front(ci);
if(vi.size() > 7) {
bi.load_vi(vi);
}
}
if(vi.size() > 0) {
bi.load_vi(vi);
}
return in;
}
friend ostream& operator<<(ostream & o, const BI & bi) {
for (auto it = bi.number.rbegin();
it != bi.number.rend();
++it) {
o << '\'';
o << *it;
}
return o;
}
void add(const BI & o) {
if(number.size() < o.number.size())
number.resize(o.number.size());
for(int i = 0; i < o.number.size(); ++i)
number[i] += o.number[i];
number.resize(number.size() + 1);
int d = 0;
for(int i = 0; i < o.number.size(); ++i) {
int nd = (number[i] + d) / 100000000;
int nr = (number[i] + d) % 100000000;
number[i] = nr;
d = nd;
}
}
friend BI operator+(const BI & a, const BI & b) {
BI r;
r.add(a);
r.add(b);
return r;
}
};
int main() {
BI a;
BI b;
cin >> a;
cin >> std::ws;
cout << "read a\n";
cin >> b;
cout << "read b\n";
cout << (a + b) << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment