bigint::bigvalue_t trim_zeros(bigint::bigvalue_t that){
   bigint::bigvalue_t result;
   
   bigint::bigvalue_t::reverse_iterator rit2 = that.rbegin();
   bigint::bigvalue_t::iterator it = result.begin();

   bool high_order = true;
   //int i = 0;

   while(rit2 != that.rend()){

      while(high_order){
         if(*rit2 != 0){
            high_order = false;
         } else{
            ++rit2;    
         }

         if (rit2 == that.rend()) break;
      }
      
      if (rit2 != that.rend()){
         it = result.insert(it, *rit2);
         it = result.begin();
         ++rit2;
      }
  
   }
   if (result.size() == 0){ 
      result.push_back(0);
      //result.negative = false;
   }
   return result;
}
//returns true if left is > right, false if 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;

   bigint::bigvalue_t::const_reverse_iterator
                       left_digit = left.rbegin();
   bigint::bigvalue_t::const_reverse_iterator
                       right_digit = right.rbegin();
 
   bool abs_val_greatest = false;
   while (left_digit != left.rend()){

      if (*left_digit > *right_digit){
         return true;
      } else if (*left_digit < *right_digit){
         break;  
      }        
      ++left_digit;
      ++right_digit;
   }
   return abs_val_greatest;
}
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;
}
Beispiel #4
0
bigint::bigvalue_t do_bigsub(const bigint::bigvalue_t& top,
      const bigint::bigvalue_t& bottom) {
   bigint::bigvalue_t diff{};
   auto itor_top = top.rbegin();
   auto itor_bottom = bottom.rbegin();

// use signed integers so we don't end up with -1 == 255
   int digit_top{0};
   int digit_bottom{0};
   int digit_diff{0};
   int borrow{0};

   while (itor_top != top.rend()) {
      digit_top = static_cast<int>(*itor_top++);
      if (itor_bottom  == bottom.rend()) {
         digit_bottom = 0;
      } else {
         digit_bottom = static_cast<int>(*itor_bottom++);
      }
      digit_top -= borrow;
      if (digit_top < digit_bottom) {
         borrow = 1;
         digit_top += 10;
      } else {
         borrow = 0;
      }
      digit_diff = digit_top - digit_bottom;
/*      cout << "do_bigsub():digit:"<< static_cast<unsigned>(digit_top);
      cout << " - " << static_cast<unsigned>(digit_bottom);
      cout << " = " << static_cast<unsigned>(digit_diff) << endl;*/
      diff.insert(diff.begin(), digit_diff);
   }
/*   cout << "do_bigsub(): ";
   for (auto i : top) cout << static_cast<unsigned>(i);
   cout << " - ";
   for (auto i : bottom) cout << static_cast<unsigned>(i);
   cout << " = ";
   for (auto i : diff) cout << static_cast<unsigned>(i);
   cout << endl;*/
   // Trim trailing zeros
   for (auto i : diff) {
      if (i == 0) diff.pop_back();
      else break;
   }
   return diff;
}
Beispiel #5
0
bigint::bigvalue_t do_bigmult(const bigint::bigvalue_t& top,
      const bigint::bigvalue_t& bottom) {

   bigint::bigvalue_t product{};

   auto itor_bottom = bottom.rbegin();

   while (itor_bottom != bottom.rend()) {
      unsigned char carry {0};
      auto itor_top = top.rbegin();
      while (itor_top != top.rend()) {
         unsigned char digit = *itor_top++ * *itor_bottom++;
         digit += carry;
         product.insert(product.begin(), digit % 10);
         carry = digit / 10;
      }
   }
   return product;
}
Beispiel #6
0
bigint::bigvalue_t do_bigadd(const bigint::bigvalue_t& top,
      const bigint::bigvalue_t& bottom) {
   bigint::bigvalue_t sum{};        // return value
   bigint::digit_t digit_sum{0};    // digit marker
   bigint::digit_t digit_top{0};
   bigint::digit_t digit_bottom{0};
   bigint::digit_t carry{0};        // carryover marker

   // iterators
   auto itor_top = top.rbegin();
   auto itor_bottom = bottom.rbegin();

   while(itor_top != top.rend()) {
      digit_top = static_cast<unsigned>(*itor_top++);
      if (itor_bottom  == bottom.rend()) {
         digit_bottom = 0;
      } else {
         digit_bottom = static_cast<unsigned>(*itor_bottom++);
      }
      digit_sum = digit_bottom + digit_top + carry;
      if (digit_sum > 9) {
         carry = 1;
         digit_sum -= 10;
      } else {
         carry = 0;
      }
      sum.insert(sum.begin(), digit_sum);
   }
   cout << "do_bigadd(): ";
   for (auto i : top) cout << static_cast<unsigned>(i);
   cout << " + ";
   for (auto i : bottom) cout << static_cast<unsigned>(i);
   cout << " = ";
   for (auto i : sum) cout << static_cast<unsigned>(i);
   cout << endl;

   for (auto i : sum) {
      if (i == 0) sum.pop_back();
      else break;
   }
   return sum;
}