Exemplo n.º 1
0
bigint::bigvalue_t do_bigadd
      (bigint::bigvalue_t v1, bigint::bigvalue_t v2){

   //do_mult may pass in empty vectors as args
   if(v1.size() == 0) v1.push_back(0);
   if(v2.size() == 0) v2.push_back(0);

   char carry = 0;
   bigint::bigvalue_t v3;
   bool done1 = false;
   bool done2 = false;
   for (size_t i = 0; ; ++i) {
      char digit1, digit2;
      if (i < v1.size()) { digit1 = v1[i]; }
                    else { digit1 = 0; done1 = true; }
      if (i < v2.size()) { digit2 = v2[i]; }
                    else { digit2 = 0; done2 = true; }
      if (done1 && done2) break;
      char sum = digit1 + digit2 + carry;
      v3.push_back (sum % 10);
      carry = sum / 10;
   }
   if (carry != 0) v3.push_back (carry);
   return v3;


}
bigint::bigvalue_t trim(bigint::bigvalue_t result) {
    for(auto it = result.rbegin(); it != result.rend()
            and *it == 0; ++it) {
        result.pop_back();
    }
    if(result.empty()) result.push_back(0);
    return result;
}