// ALMOST IDENTICAL TO D0_bIgAdD bigvalue_t do_bigsub(const bigvalue_t& left, const bigvalue_t& right){ bigvalue_t result = left; //.... might work // initialize variables digit_t num, left_num, right_num, carry; signed char temp; size_t i = 0; auto itor = result.cbegin(); auto left_itor = left.cbegin(); auto right_itor = right.cbegin(); // loop through each digit and put their sum into the result while(itor != result.cend()){ // in case left number has less digits if(left.size() < i) left_num = '0' - '0'; else left_num = *left_itor - '0'; // in case right number has less digits if(right.size() < i) right_num = '0' - '0'; else right_num = *right_itor - '0'; // deal with individual digit ///////////////////////////////////////////////////////////////// if(carry != '0') left_num = left_num - ('1' - '0');//// can you //substrationate chars temp = (left_num - right_num);// - '0'; carry = '0' - '0'; if(temp < '0' - '0'){ carry = '1' - '0'; //main differences are here... num = temp + 10; } num = temp; result.push_back(num); ///////////////////////////////////////////////////////////////// } return result; }
bigvalue_t do_bigadd(const bigvalue_t& left, const bigvalue_t& right){ // supposed tp be private //needs to return bigvalue_t... not too sure this will work bigvalue_t result{};// = left; int maxsize; int maxl = left.size(); int maxr = right.size(); if(maxl > maxr) maxsize = maxl; else maxsize = maxr; //initialize variables digit_t num; int left_num, right_num, carry, sum; auto left_itor = left.cbegin(); auto right_itor = right.cbegin(); // loop through each digit and put their sum into the result for(int j = 0; j < maxsize; ++j){ // in case left number has less digits //if(j < maxl and j < maxr) break; if(j < maxl){ num = *left_itor; left_num = num - '0'; ++left_itor; }else{ num = '0'; left_num = num - '0'; } // in case right number has less digits if(j < maxr){ num = *right_itor; right_num = num - '0'; ++right_itor; }else{ num = '0'; right_num = num - '0'; } // deal with individual digit sum = (left_num + right_num + carry);// - '0'; if(sum > 9){ carry = 1; sum -= 10; num = sum + '0'; }else{ carry = 0; num = sum + '0'; } result.push_back(num);// mod 10 on char? sum = 0; } if(carry == 1){ num = carry + '0'; result.push_back(num); } // not yet made.. // trim_zeros(result); return result; //return 2; }
bigint::bigvalue_t bigint::trim(bigvalue_t result) { bigvalue_t::const_iterator result_it = result.begin(); bigvalue_t::const_iterator result_end = result.end(); while(result_it != result_end and *result_it == 0) { ++result_it; result.pop_back(); } return result; }
// 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 do_bigless(const bigvalue_t& left, const bigvalue_t& right){ auto l_itor = left.crbegin(); auto r_itor = right.crbegin(); if (left.size()<right.size()){return true;} else if (left.size()>right.size()){return false;} while (l_itor != left.crend()) { if (*l_itor-'0' < *r_itor-'0'){return true;} else if (*l_itor-'0' > *r_itor-'0'){return false;} ++l_itor; ++r_itor; } return false; }
bigvalue_t do_bigmul(const bigvalue_t& left, const bigvalue_t& right){ int carry = 0; digit_t digit = 0; bigvalue_t prod; prod.reserve(left.size() + right.size()); for (size_t i=0; i<left.size(); ++i) { carry = 0; for (size_t j=0; j<right.size(); ++j) { digit = prod.at(i+j) + left.at(i)*right.at(j) + carry; prod.at(i+j) = digit % 10; carry = digit / 10; } prod.at(i+right.size()) = carry; } return prod; }
// 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; }
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; }
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; }
bigint::bigvalue_t bigint::do_bigmult(const bigvalue_t& left, const bigvalue_t& right) { bigvalue_t prod; prod.resize(left.size() + right.size()); int carry = 0; int d = 0; for(int i = left.size()-1; i != -1; --i) { carry = 0; for(int j = right.size()-1; j != -1; --j) { d = (prod.at((left.size()-1 -i) + (right.size()-1 - j))) + (left.at(i) * right.at(j)) + carry; //cout << (prod.at((left.size()-1 - i) + (right.size()-1 - j))) << " new "; //cout << (left.at(i) * right.at(j)) << " old "; //cout << carry << "carry" << endl; //cout << d << endl; //cout << d%10 << "<--insert"<<endl; //cout << d/10 << " carry" << endl; prod.at((left.size()-1 - i) + (right.size()-1 - j)) = d % 10; carry = d/10; } if(carry != 0) { //cout << carry << "<- insert c"<<endl; prod.at((right.size()-1) - -1) = carry; } } //trim return prod; }