We wish to implement a BigNumber class that represents nonne

We wish to implement a BigNumber class that represents nonnegative integers with up to 100 digits. To make this work, we will use an array of digits as the underlying representation. In particular, we will use an array of char, where each char represents an actual number from 0 to 9, not the ASCII characters \'0\' to \'9\'. That means when you implement the stream insertion operator you\'ll need to cast each array element to an int. So out << int(digits[i]) rather than out << digits[i].

Below it the code that will go in your BigNumber.h. You\'ll want to add the implementation file BigNumber.cpp where you implement all the prototypes in BigNumber.h. You\'ll also need to implement a Driver.cpp to with a main to test your functions.

class BigNumber

{

       friend ostream &operator<<(ostream &, const BigNumber &);     // stream insertion operator

// output the number as we would normally read it e.g. output 17 means the number seventeen.

private:

       char digits[100];  // to hold the digits with digits[i] representing the 10^i\'s place.

       int numDigits;  // number of digits in the number, e.g 5 has one digit but 45 has two. ZERO should have zero digits

public:

       BigNumber(int value = 0);  // default constructor.

  

int getNumDigits() const;         // return numDigits

       bool isZero() const;      // return true iff *this is equal to ZERO

       bool operator< (const BigNumber & c) const;     // less than

       bool operator>= (const BigNumber & c) const;    // greather than or equal

       bool operator<= (const BigNumber & c) const;    // less than or equal

       bool operator== (const BigNumber & c) const;    // equal

       bool operator!= (const BigNumber & c) const;    // not equal

};

//OUTPUT SHOULD BE AN INTEGER

UPLOAD THREE FILES: BigNumber.h and BigNumber.cpp and Driver.cpp. I should be able to load the three files in Visual Studio and everything will compile fine. I will also run your code with my driver so be sure your class will work correctly when tested extensively.

Your Driver.cpp will look something like this:

#include \"BigNumber.h\"

#include

using namespace std;

int main()

{

  BigNumber n(123);

  cout << n << endl;

// (etc.)

return 0;

}

Solution

//BigNumber.h

#include<iostream>
#include<fstream>
using namespace std;

class BigNumber
{
   friend ostream &operator<<(ostream &, const BigNumber &); // stream insertion operator
   // output the number as we would normally read it e.g. output 17 means the number seventeen.
private:
   char digits[100]; // to hold the digits with digits[i] representing the 10^i\'s place.
   int numDigits; // number of digits in the number, e.g 5 has one digit but 45 has two. ZERO should have zero digits
public:
   BigNumber(int value = 0); // default constructor.

   int getNumDigits() const; // return numDigits
   bool isZero() const; // return true iff *this is equal to ZERO
   bool operator< (const BigNumber & c) const; // less than
   bool operator>= (const BigNumber & c) const; // greather than or equal
   bool operator<= (const BigNumber & c) const; // less than or equal
   bool operator== (const BigNumber & c) const; // equal
   bool operator!= (const BigNumber & c) const; // not equal
};

//BigNumber.cpp

#include \"BigNumber.h\"

BigNumber::BigNumber(int val)
{
   numDigits = 0;
   int i = 0,rem;
   char str[100];

   do
   {
       rem = val % 10;
       str[i++] = rem;

   } while (val = val / 10);
   //reverse the string str ans assign to digits array
   for (int j = 0,k = i-1; j < i; j++)
   {
       digits[j] = str[k--];
   }
   i = 0;
   while ((int)digits[i] >= 0)
   {
       ++i;
   }
   numDigits = i;
}

int BigNumber::getNumDigits() const
{
  
   return numDigits;
}

bool BigNumber::isZero() const
{
   if (numDigits == 0)
       return true;
   else
       return false;
}

bool BigNumber::operator< (const BigNumber & c) const
{
   if (numDigits < c.numDigits)
       return true;
   if ((int)digits[0] >= (int)c.digits[0])
       return false;
   for (int i = 1; i < numDigits; i++)
   {
      
       if ((int)digits[i] >=(int)c.digits[i])
           return false;
       continue;
   }
   return true;
}

bool BigNumber:: operator>= (const BigNumber & c) const
{
   if (numDigits < c.numDigits)
       return false;
   if ((int)digits[0] < (int)c.digits[0])
       return false;
   for (int i = 0; i < numDigits; i++)
   {
       if (digits[i] >= c.digits[i])
           continue;
       else
           return false;
   }
   return true;
}

bool BigNumber:: operator<= (const BigNumber & c) const
{
   if (numDigits > c.numDigits)
       return false;
   if ((int)digits[0] > (int)c.digits[0])
       return true;
   for (int i = 0; i < numDigits; i++)
   {
       if (digits[i] <= c.digits[i])
           continue;
       else
           return false;
   }
   return true;
}

bool BigNumber:: operator== (const BigNumber & c) const
{
   if (numDigits != c.numDigits )
       return false;
   for (int i = 0; i < numDigits; i++)
   {
       if (digits[i] != c.digits[i])
           return false;
   }
   return true;
}

bool BigNumber:: operator!= (const BigNumber & c) const
{
   if (numDigits != c.numDigits)
       return true;
   for (int i = 0; i < numDigits; i++)
   {
       if ( digits[i] !=c.digits[i] )
           return true;
   }
   return false;
}

ostream &operator<<(ostream &out, const BigNumber &Int)
{
   for (int i = 0; i < Int.numDigits; i++)
       out << (int)(Int.digits[i]);
   cout << endl;
   return out;
}

//main.cpp

#include \"BigNumber.h\"
#include<iostream>
using namespace std;

int main()
{
   BigNumber n(1237);
   cout << n << endl;
   // (etc.)test operator overloading
   BigNumber m(1237);
   cout << m << endl;
  
   if (n.isZero())
   {
       cout << \"number is zero\" << endl;
   }
   else
   {
       cout << \"number is not zero\" << endl;
   }
   //test < operator
   if ( n < m )
   {
       cout << \"n is less than m\" << endl;
   }
   else
   {
       cout << \"n is not less than m\" << endl;
   }
   //test >= operator
   if (n >= m)
   {
       cout << \"n is greater than equal to m\" << endl;
   }
   else
   {
       cout << \"n is not greater than equal to m\" << endl;
   }
   //test <= operator
   if (n <= m)
   {
       cout << \"n is less than equal to m\" << endl;
   }
   else
   {
       cout << \"n is not less than equal to m\" << endl;
   }
   //test == operator
   if (n == m)
   {
       cout << \"n is equal to m\" << endl;
   }
   else
   {
       cout << \"n is not equal to m\" << endl;
   }

   //test != operator
   if (n != m)
   {
       cout << \"n is not equal to m\" << endl;
   }
   else
   {
       cout << \"n is equal to m\" << endl;
   }
   return 0;
}

----------------------------------------------------------------

output:

1237

1237

number is not zero
n is not less than m
n is greater than equal to m
n is less than equal to m
n is equal to m
n is equal to m

We wish to implement a BigNumber class that represents nonnegative integers with up to 100 digits. To make this work, we will use an array of digits as the unde
We wish to implement a BigNumber class that represents nonnegative integers with up to 100 digits. To make this work, we will use an array of digits as the unde
We wish to implement a BigNumber class that represents nonnegative integers with up to 100 digits. To make this work, we will use an array of digits as the unde
We wish to implement a BigNumber class that represents nonnegative integers with up to 100 digits. To make this work, we will use an array of digits as the unde
We wish to implement a BigNumber class that represents nonnegative integers with up to 100 digits. To make this work, we will use an array of digits as the unde

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site