Adding BigInt's
C / C++ / MFC
2
Posts
2
Posters
0
Views
1
Watching
-
//WTF?
BigInt BigInt::add_bigint(BigInt add)
{
int carry = 0;
int temp;
bool carry2;
stack<int> answer;
vector<int> tempvector;
vector<int> add_int = add.get_bigint();while(bigint\_int.size() > add\_int.size()) add\_int.insert(add\_int.begin(), 1, 0); while(bigint\_int.size() < add\_int.size()) bigint\_int.insert(bigint\_int.begin(), 1, 0); cout<<add\_int.size()<<" "<<bigint\_int.size()<<"\\n"; for(int i = size(); i > 0 ;i--) //<------if I do size - 1 here { //the first addition works but not the rest temp = add\_int\[i\] + bigint\_int\[i\]; if(temp >= 10) { temp -= 10; carry2 = true; } else carry2 = false; answer.push(temp + carry); if(carry2) carry = 1; else carry = 0; } if(carry) answer.push(carry); while(!answer.empty()) { tempvector.push\_back(answer.top()); answer.pop(); } BigInt ret(tempvector); return ret;
}
-
//WTF?
BigInt BigInt::add_bigint(BigInt add)
{
int carry = 0;
int temp;
bool carry2;
stack<int> answer;
vector<int> tempvector;
vector<int> add_int = add.get_bigint();while(bigint\_int.size() > add\_int.size()) add\_int.insert(add\_int.begin(), 1, 0); while(bigint\_int.size() < add\_int.size()) bigint\_int.insert(bigint\_int.begin(), 1, 0); cout<<add\_int.size()<<" "<<bigint\_int.size()<<"\\n"; for(int i = size(); i > 0 ;i--) //<------if I do size - 1 here { //the first addition works but not the rest temp = add\_int\[i\] + bigint\_int\[i\]; if(temp >= 10) { temp -= 10; carry2 = true; } else carry2 = false; answer.push(temp + carry); if(carry2) carry = 1; else carry = 0; } if(carry) answer.push(carry); while(!answer.empty()) { tempvector.push\_back(answer.top()); answer.pop(); } BigInt ret(tempvector); return ret;
}