bigint::bigvalue_t do_bigadd(const bigint::bigvalue_t& left,
                             const bigint::bigvalue_t& right) {
    bigint::bigvalue_t sum;
    int carry = 0;
    bool left_empty = false;
    bool right_empty = false;
    auto left_it = left.begin();
    //bigint::bigvalue_t::const_reverse_iterator left_it = left.rbegin();
    auto right_it = right.begin();
    auto left_end = left.end();
    auto right_end = right.end();
    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 do_bigsub(const bigint::bigvalue_t& left,
                             const bigint::bigvalue_t& right) {
    bigint::bigvalue_t sum;
    int carry = 0;
    bool left_empty = false;
    bool right_empty = false;
    auto left_it = left.begin();
    //bigint::bigvalue_t::const_reverse_iterator left_it = left.rbegin();
    auto right_it = right.begin();
    auto left_end = left.end();
    auto right_end = right.end();
    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;
}
Пример #3
0
//
// do_bigless()
// returns true if left < right
// returns false if left > right or left == right
//
bool do_bigless (const bigint::bigvalue_t& left,
      const bigint::bigvalue_t& right) {
   if (left.size() < right.size()) {
      return true;
   } else if (left.size() > right.size()) {
      return false;
   }

   auto itor_left = left.begin();
   auto itor_right = right.begin();

   while(itor_left != left.end() && itor_right != right.end()) {
      if (itor_left > itor_left) {
         return false;
      } else if (itor_left < itor_right) {
         return true;
      } else {
         itor_left++;
         itor_right++;
      }
   }
   return true;
}