// false == left > right, true == left < right int bigint::do_bigless(const bigvalue_t& left, const bigvalue_t& right) { auto left_it = left.rbegin(); auto right_it = right.rbegin(); auto left_end = left.rend(); auto right_end = right.rend(); //cout << "left size" << left.size() << endl; //cout << "right size" << right.size() << endl; if(left.size() > right.size()) { return -1; } if(left.size() < right.size()) { return 1; } while(left_it != left_end and right_it != right_end) { if(*left_it > *right_it) { return -1; } else if (*left_it < *right_it) { return 1; } ++left_it; ++right_it; } return 0; }
bool bigint::do_bigabs_less (const bigvalue_t& left, const bigvalue_t& right) { if(right.size() > left.size()) { return true; } bigvalue_t::const_reverse_iterator left_it = left.rbegin(); bigvalue_t::const_reverse_iterator right_it = right.rbegin(); bigvalue_t::const_reverse_iterator left_end = left.rend(); bigvalue_t::const_reverse_iterator right_end = right.rend(); while(left_it != left_end and right_it != right_end) { if(*left_it > *right_it) { return false; } else if (*left_it < *right_it) { return true; } ++left_it; ++right_it; } return false; }
bigint::bigvalue_t bigint::do_bigadd(const bigvalue_t& left, const bigvalue_t& right) { bigvalue_t sum; int carry = 0; bool left_empty = false; bool right_empty = false; bigvalue_t::const_reverse_iterator left_it = left.rbegin(); bigvalue_t::const_reverse_iterator right_it = right.rbegin(); bigvalue_t::const_reverse_iterator left_end = left.rend(); bigvalue_t::const_reverse_iterator right_end = right.rend(); if(left.size() == 0) { left_empty = true; } if(right.size() == 0) { right_empty = true; } while(true) { int curr_value = (left_empty?0:*left_it) + (right_empty?0:*right_it) + carry; carry = 0; if(curr_value >= 10) { carry = 1; curr_value -= 10; } sum.push_back(curr_value); if(++left_it == left_end) { left_empty = true; } if(++right_it == right_end) { right_empty = true; } if(left_empty and right_empty) { if(carry == 1) { sum.push_back(carry); } break; } } return sum; }
// false == left > right true == left < right int bigint::do_bigless(const bigvalue_t& left, const bigvalue_t& right) { bigvalue_t::const_reverse_iterator left_it = left.rbegin(); bigvalue_t::const_reverse_iterator right_it = right.rbegin(); bigvalue_t::const_reverse_iterator left_end = left.rend(); bigvalue_t::const_reverse_iterator right_end = right.rend(); if(left.size() > right.size()) { return -1; } if(left.size() < right.size()) { return 1; } while(left_it != left_end and right_it != right_end) { if(*left_it > *right_it) { return -1; } else if (*left_it < *right_it) { return 1; } ++left_it; ++right_it; } return 0; }
bigint::bigvalue_t bigint::do_bigsub(const bigvalue_t& left, const bigvalue_t& right) { bigvalue_t sum; int carry = 0; bool left_empty = false; bool right_empty = false; bigvalue_t::const_reverse_iterator left_it = left.rbegin(); bigvalue_t::const_reverse_iterator right_it = right.rbegin(); bigvalue_t::const_reverse_iterator left_end = left.rend(); bigvalue_t::const_reverse_iterator right_end = right.rend(); if(left.size() == 0) { left_empty = true; } if(right.size() == 0) { right_empty = true; } while(true) { int curr_value = (left_empty?0:*left_it) - (right_empty?0:*right_it) + carry; carry = 0; if(curr_value < 0 and !left_empty) { carry = -1; curr_value += 10; } sum.push_back(curr_value); if(++left_it == left_end) { left_empty = true; } if(++right_it == right_end) { right_empty = true; } if(left_empty and right_empty) { break; } } //remove leading 0s //sum = trim(sum); return sum; }