J_B
asked on
C++ HugeInt - trouble with size
I am having trouble obtaining the size of a HugeInt object.. (I want to eventually be able to compare to values)
I'm not sure if a convert (via another constructor) is needed or what. Would really appreciate help with this. Thanks
Please see below for for the two constructors..
I'm not sure if a convert (via another constructor) is needed or what. Would really appreciate help with this. Thanks
Please see below for for the two constructors..
HugeInt::HugeInt( long value )
:size(0)
{
// initialize array to zero
for ( int i = 0; i < digits; i++ )
integer[ i ] = 0;
// place digits of argument into array
for ( int j = digits - 1; value != 0 && j >= 0; j-- )
{
integer[ j ] = value % 10;
value /= 10;
} // end for
//???size = sizeof(value);
} // end HugeInt default/conversion constructor
HugeInt::HugeInt( const string &number )
:size(0)
{
size = sizeof(number);
// initialize array to zero
for ( int i = 0; i < digits; i++ )
integer[ i ] = 0;
// place digits of argument into array
int length = number.size();
// int size = strlen( &number );
for ( int j = digits - length, k = 0; j < digits; j++, k++ )
if ( isdigit( number[ k ] ) ) //ensure that char is digit
integer[ j ] = number[ k ] - '0';
} // end HugeInt conversion constructor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm not getting any values for size still here: (It is always 0)
// place digits of argument into array
for ( int j = digits - 1; value != 0 && j >= 0; j-- )
{
integer[ j ] = value % 10;
value /= 10;
size++;
} // end for
cout << "************ debug size = " << size << '\n';
// place digits of argument into array
for ( int j = digits - 1; value != 0 && j >= 0; j-- )
{
integer[ j ] = value % 10;
value /= 10;
size++;
} // end for
cout << "************ debug size = " << size << '\n';
What are you passing in as 'value'?
Oh, and what is 'digits'? It does not seem to be initialized in your originaly code.
ASKER
digits is in my header file as follows:
public:
static const int digits = 30; // maximu
public:
static const int digits = 30; // maximu
ASKER
Here is my code for my divide. I am trying to divide by subtracting.
(very messy code right now)
(very messy code right now)
HugeInt HugeInt::operator/( const HugeInt &op2 ) const
{
cout << "op2 = " << op2 << '\n' ;
HugeInt temp; // temporary result
HugeInt temp2 = 0; // temporary result
int carry = 0;
for ( int i = digits - 1; i >= 0; i-- )
{
if (integer[i] < op2.integer[i])
{
temp.integer[i] = 10 + integer[i] - op2.integer[i];
//Borrow from previous position
temp.integer[i+1]--;
}
else
{
temp.integer[ i ] = integer[ i ] - op2.integer[ i ];
}
}
int index2 = 1;
cout << "debug temp.size = " << temp.size << '\n';
do
{
temp = temp - op2;
if (temp.size == 0)
{
break;
}
index2 = index2 + 1;
}while ((temp.isGreaterThan(op2))& (temp.isGreaterThan(op2)) & (index2 < 940));
if (temp.isGreaterThan(0))
{
}
return temp; // return copy of temporary object
} // end function operator/
ASKER
While debugging I found out that this loop is never executing:
for ( int j = digits - 1; value != 0 && j >= 0; j-- )
{
integer[ j ] = value % 10;
value /= 10;
size++;
} // end for
I'm not sure how my subtraction is working without this executing?
for ( int j = digits - 1; value != 0 && j >= 0; j-- )
{
integer[ j ] = value % 10;
value /= 10;
size++;
} // end for
I'm not sure how my subtraction is working without this executing?
Thus my earlier question: What are you passing in as 'value'?
ASKER
-------------------------- ---------- ---
HugeInt n3( "9999" );
HugeInt n4( "11" );
//Divide
cout << "Division **************** " << '\n' ;
cout << n3 << " / " << n4 << "\n= " << ( n3 / n4) << "\n\n";
-------------------------- ---------- ---------- ---------- ---------- --
My / function is subtracting ok but I need to get it to stop when it hits 0 or is below n4
HugeInt n3( "9999" );
HugeInt n4( "11" );
//Divide
cout << "Division **************** " << '\n' ;
cout << n3 << " / " << n4 << "\n= " << ( n3 / n4) << "\n\n";
--------------------------
My / function is subtracting ok but I need to get it to stop when it hits 0 or is below n4
ASKER
jkr: - Do you need more information?
Um, 'value' is used when you call the constructor that takes a 'long' - the ctor taking a string is the other one where you use 'length()'
ASKER
The code posted in - HugeInt HugeInt::operator/( const HugeInt &op2 ) const
is calling the HugeInt::HugeInt( long value ) constructor.
is calling the HugeInt::HugeInt( long value ) constructor.
Not really - two times the default constructor, and then an assignment in
HugeInt temp2 = 0; // temporary result
HugeInt temp2 = 0; // temporary result
ASKER
I commented out the
//HugeInt temp2 = 0; // temporary result
and ran it again with the same results. (still calls HugeInt (long) )
//HugeInt temp2 = 0; // temporary result
and ran it again with the same results. (still calls HugeInt (long) )
ASKER
Sorry it was the following subtraction function that was calling the HugeInt constructor.
This is called by the division function.
This is called by the division function.
HugeInt HugeInt::operator-( const HugeInt &op2 ) const
{
cout << "Debug HugeInt temp in - function" << '\n';
HugeInt temp; // temporary result
int carry = 0;
int borrow = 0;
for ( int i = digits - 1; i >= 0; i-- )
{
// temp.integer[ i ] = integer[ i ] + op2.integer[ i ] + carry;
//cout << "1 temp = " << temp << '\n' ;
if (borrow == 1)
{
if ((integer[i] - 1) < op2.integer[i])
{ //going to need to borrow again
temp.integer[i] = 9 + integer[i] - op2.integer[i];
}
else
{
borrow = 0;
temp.integer[i] = integer[i] - 1 - op2.integer[i];
}
}
else
{
if (integer[i] < op2.integer[i])
{
temp.integer[i] = 10 + integer[i] - op2.integer[i];
//Borrow from previous position
cout << "2 temp = " << temp << '\n' ;
borrow = 1;
if (temp.integer[i-1] = 0)
{
// temp.integer[i-2]--;
// temp.integer[i-1] = 9;
}
else
{
// temp.integer[i-1]--;
}
cout << "3 temp = " << temp << '\n' ;
}
else
{
temp.integer[ i ] = integer[ i ] - op2.integer[ i ];
}
}
} // end for
return temp; // return copy of temporary object
} // end function operator-
ASKER
Thank you very much for your help. I ended up using a different method for comparing HugeInt values.
ASKER