Question

In: Computer Science

We wish to implement a BigNumber class that represents nonnegative integers with up to 100 digits....

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;

}

Solutions

Expert 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


Related Solutions

Suppose we are given a set ? containing 2? integers, and we wish to partition it...
Suppose we are given a set ? containing 2? integers, and we wish to partition it into two sets ?1 and ?2 so that |?1 | = |?2 | = ? and so that the sum of the numbers in ?1 is as close as possible to the sum of those in ?2. Let the neighborhood ? be determined by all possible interchanges of two integers between ?1 and ?2. Is ? exact?
THIS IS IMPLEMENTED IN C++ The SetInt class (Annex B1) represents a set of integers. This...
THIS IS IMPLEMENTED IN C++ The SetInt class (Annex B1) represents a set of integers. This class contains a constructor without parameters which creates an empty set and a constructor which receives as parameters an array of integers as well as its size and which creates a set of integers containing the elements of this array. A test program is given in Annex B2. The class also contains a destructor and the copy constructor, as well as methods that have...
Based on a Node class; Use Java to implement the OrderedList class which represents an ordered...
Based on a Node class; Use Java to implement the OrderedList class which represents an ordered singly linked list that cannot contain any duplicates. Note that items in the OrderedList are always kept in descending order. Complete the class with the following methods. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the list at the proper location in the list. If the insertion is successful, the function returns true; otherwise,...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable array size. Your class should support storing an integer at a specific index value, retrieving the integer at a specific index value, and automatically increasing storage for the saved values.
you are asked to implement a C++ class to model a sorted array of unsigned integers....
you are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Class will provide (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be used...
On a circular array with n positions, we wish to place the integers 1, 2, ......
On a circular array with n positions, we wish to place the integers 1, 2, ... r in order, clockwise, such that consecutive integers, including the pair (r,1) are not in adjacent positions on the array. Arrangements obtained by rotation are considered the same. In how many ways can this be done? Give a combinatorial proof.
Implement Lexer(Scanner) that scans the whole source code entered by the user that includes Alphabets(characters), integers(digits),...
Implement Lexer(Scanner) that scans the whole source code entered by the user that includes Alphabets(characters), integers(digits), operators(arithmatic, logical and relational operators), and special symbols. output should be in the form of Symbol table that shows each token with its name, type and number of times entered(lexexmes). dictionary should be implemented in the code by writing RegX for each category. code entered from internet will be discarded. Implementation language will be of your own choice. I really need the answer of...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT