Ejemplo n.º 1
0
Archivo: bigint.cpp Proyecto: foo/ii
// Full multiplication.
// For every digit d in 'a' b is multiplied by d.
// Final result is obtained by addition of all results of multiplications with proper offset.
digit* multiply(const digit* a, const digit* b)
{
  digit* result = 0;
  int offset = 0;
  while(a)
  {
    if(a->n != i2d(0))
    {
      digit* row = copy(b);
      multiply_by_digit(row, d2i(a->n));
      for(int i = 0; i < offset; ++i)
      {
	digit* new_row = new digit;
	new_row->next = row;
	new_row->n = i2d(0);
	row = new_row;
      }
      addto(row, result);
      deletebigint(row);
    }
    ++offset;
    a = a->next;
  }
  // needed when a or b is 0
  rm_leading_0s(result);
  return result;
}
Ejemplo n.º 2
0
BigInt::reference BigInt::operator*= ( const_reference o )
{
    if ( o.buffer.size() == 1 )
        return multiply_by_digit ( o.buffer[0] );

    sign_ = sign_ != o.sign_;

    std::queue<BigInt> partial;

    for ( unsigned i = 0; i < o.buffer.size(); i++ )
    {
        BigInt copy = *this;
        copy.multiply_by_digit ( o.buffer[i] );
        partial.push ( copy );
    }

    BigInt temp;
    *this = zero;
    for( unsigned i = 0; !partial.empty(); i++ )
    {
        temp = partial.front();
        partial.pop();
        temp.buffer = buffer_type(i,0) + temp.buffer;
        *this += temp;
    }
    return normalize();
}