BigInteger BigInteger::operator*(const BigInteger & r) const { BigInteger i(0),
ID: 642078 • Letter: B
Question
BigInteger BigInteger::operator*(const BigInteger & r) const
{
BigInteger i(0), total(0), one(1);
// STUDENTS
// modify the code so that the small BigInteger is always used to control the for loop
for(int i ; i < *this; i = i + one ) // runs faster if *this < r
{
total = total + r;
if ( total.hasOverFlowed() ) return total; // stop when overflow
}
return total;
}
bool BigInteger::hasOverFlowed(void) const
{
return this->overflow;
}
void BigInteger::convertIntToBigInteger(int r) // place the int parameter into the BigInteger format for the invoking instance
{
this->setToZero();
if ( r <= 0 ) return;
int placer = NUMDIGITS - 1; // rightmost digit, work right to left
int value = r;
while ( value > 0 && placer >= 0 )
{
// Students
// use % operator to select the right most digit and place in the array
// change the variable value by removing the right digit using / operator
// move to the left for placing digits in the array
//
//
digits[placer] = value % 10;
value = value/10;
placer --;
//
//
//
//
//
//
}
return;
}
#include <iostream>
using namespace::std;
#include "BigInteger.h"
int main()
{
{
BigInteger a(123456);
cout<<" a is " << a << endl;
return 0;
}
// STUDENTS
// test the code piece by piece by adding and removing comments
// REMEMBER that you can use break points and the watch window to help debugging
// Your final output should match the output at the end of the program
cout << "Number of digits in our numbers is " << BigInteger::getNumberOfDigits() << endl;
// test default constructor and << operator
BigInteger a;
cout << a << endl;
// test int argument constructor and << operator
BigInteger b(123456789), c(999);
// test <<
cout << b << ' ' << c << endl;
// test copy constructor
BigInteger d(b);
cout << "d is " << d << endl;
// test = operator
d = c;
cout << "d is now " << d << endl;
// test == and + operators
BigInteger e(123456788), f(667788), g(667788);
cout << b << ' ' << e << ' ' << " b == e is " << ( b == e ) << endl;
cout << f << ' ' << g << ' ' << " f == g is " << ( f == g ) << endl;
cout << e << " + " << f << " is " << ( e + f ) << endl;
// test *
BigInteger h(20), k(60);
cout << h << " * " << k << " is " << ( h * k ) << endl;
// Calculate powers of 2 until overflow
BigInteger s(1), value(0), stop(20), one(1), two(2),powerOf2(1), save(5);
while ( !powerOf2.hasOverFlowed())
{
powerOf2 = two * powerOf2; // multiple by 2 each time
cout << " 2 to the power of " << s << " is " << powerOf2 << endl;
s = s + one;
}
// test C++ string argument constructor
BigInteger w("12345678901234567890");
cout << "w is " << w << endl;
BigInteger w1,w2,oneMillion("1000000");
w1 = w + w;
w2 = oneMillion * w;
cout << w1 << " " << w2 << endl;
return;
}
// need help with written these codes
Explanation / Answer
biginteger.h
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.