Skip to content

Instantly share code, notes, and snippets.

@CauanCabral
Created April 16, 2011 03:41
Show Gist options
  • Save CauanCabral/922844 to your computer and use it in GitHub Desktop.
Save CauanCabral/922844 to your computer and use it in GitHub Desktop.
BigInt para operção com inteiros gigantes
class BigInt
{
protected:
int length; // tamanho do vetor alocado
public:
int* digitos;
int ultimoDigito; // tamanho do número definido
BigInt()
{
this->length = 10;
this->digitos = new int[10];
this->reset();
}
BigInt(int l)
{
this->length = l;
this->digitos = new int[l];
this->reset();
}
void reset()
{
for(int i = 0; i < 10; ++i)
{
this->digitos[i] = 0;
}
this->ultimoDigito = -1;
}
void setDigito(string* c)
{
string::reverse_iterator rit;
for ( rit = c->rbegin() ; rit < c->rend(); ++rit )
{
this->digitos[++this->ultimoDigito] = *rit - '0';
}
}
BigInt operator+(const BigInt& bi) const
{
BigInt result;
int vaiUm = 0;
for(int i = 0; i < 10; i++)
{
result.digitos[i] = (vaiUm + bi.digitos[i] + this->digitos[i]) % 10;
vaiUm = (vaiUm + bi.digitos[i] + this->digitos[i]) / 10;
}
return result;
}
~BigInt()
{
delete[] digitos;//erro de execução, invalid pointer
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment