#ifndef BIGINTEGER_H #define BIGINTEGER_H #include using namespace::std; const i
ID: 642079 • Letter: #
Question
#ifndef BIGINTEGER_H
#define BIGINTEGER_H
#include
using namespace::std;
const int NUMDIGITS = 50;
// A non-negative integer with NUMDIGITS
// Leading zeores are stored in the number
// if + or * result in overflow, i.e. and answer with more that NUMDIGIST, the overflow flag is set
class BigInteger
{
friend ostream & operator <<( ostream &, const BigInteger &);
public:
BigInteger(); // return NULL
BigInteger(const BigInteger &); // copy constructor
BigInteger( int );
BigInteger( const char *);
~BigInteger();
void setToZero();
bool operator==(const BigInteger &) const;
bool operator<(const BigInteger &) const;
bool operator<=(const BigInteger &) const;
bool operator>(const BigInteger &) const;
bool operator>=(const BigInteger &) const;
bool operator!=(const BigInteger &) const;
const BigInteger & operator=(const BigInteger &) ;
BigInteger operator+(const BigInteger &) const;
BigInteger operator*(const BigInteger &) const;
bool hasOverFlowed(void) const;
static int getNumberOfDigits(void)
{
return NUMDIGITS;
}
private:
void convertIntToBigInteger(int); // place the int parameter into the BigInteger format for the invoking instance
int digits[NUMDIGITS]; // high order digit stored in cell 0, low order digit stored in cell NUMDIGITS-1
bool overflow;
};
#endif
bool BigInteger::operator<(const BigInteger & r) const
{
// STUDENTS
// Write a for or a while loop to compare digits left to right
// if a smaller digit is found in the invoking instance return true
// if a larger digit is found in the invoking instance return false
//for(int i=0; i for(int i=0; i if(digits[i] < r.digits[i])
{return true;
}
else{
// for loop has been completed here ==> all digits match, so return false
return false; // all digits match, both BigIntegers are equal
}
}
bool BigInteger::operator<=(const BigInteger & r) const
{
// STUDENTS
// code needed
for(int i = 0; i < NUMDIGITS; i++)
if(digits[i] >= NUMDIGITS) return false;
return true;
}
bool BigInteger::operator>(const BigInteger & r) const
{
// STUDENTS
// code needed
return true;
}
bool BigInteger::operator>=(const BigInteger & r) const
{
// STUDENTS
// code needed
return !(*this < r);
// return true;
}
bool BigInteger::operator!=(const BigInteger & r) const
{
return !( *this == r );
}
const BigInteger & BigInteger::operator=(const BigInteger & r)
{
if ( this == & r ) return *this;
overflow = r.overflow;
for ( int i = 0; i < NUMDIGITS; i++)
digits[i] = r.digits[i];
return *this;
}
BigInteger BigInteger::operator+(const BigInteger & r) const
{
BigInteger answer;
int carry = 0;
// STUDENTS
// using a for or a while loop add the digits right to left using the carry
// review your notes from class
// loop exit, set the overflow flag
answer.overflow = ( carry > 0 );
return answer;
}
#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 HERE
Explanation / Answer
BigInteger.h
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.