Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the BigInt class by * adding the > operation to determine whether one BigInt object is bigger than another. * adding the subtraction operation

Modify the BigInt class by

* adding the > operation to determine whether one BigInt object is bigger than another.

* adding the subtraction operation - to class BigInt: int1 - int2 which should return 0 if int1 is less than int2.

* adding the multiplication operation * to class BigInt.

*******************************************************************************************

--------THERE ARE TWO ITEMS BigInt.h and BigInt.cpp---------------------------

*****************************************************************************************

/*-- BigInt.h ------------------------------------------------------------

This header file defines the data type BigInt for processing

integers of any size.

Basic operations are:

Constructor

+: Addition operator

read(): Read a BigInt object

display(): Display a BigInt object

> : Input and output operators,>

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

*/

#include

#include // setfill(), setw()

#include

#include // pow

#ifndef BIGINT

#define BIGINT

const int DIGITS_PER_BLOCK = 3;

const int MODULUS = (short int)pow(10.0, DIGITS_PER_BLOCK);

class BigInt

{

public:

/***** Constructors *****/

BigInt()

{ }

/*------------------------------------------------------------------

-----

Default cConstructor

Precondition: None

Postcondition: list's constructor was used to build

this BigInt object.

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

---*/

BigInt(int n);

/*------------------------------------------------------------------

-----

Construct BigInt equivalent of n.

Precondition: n >= 0.

Postcondition: This BigInt is the equivalent of integer n.

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

---*/

/******** Function Members ********/

/***** Constructor *****/

// Let the list constructor take care of this.

/***** read *****/

void read(istream & in);

/*------------------------------------------------------------------

-----

Read a BigInt.

Precondition: istream in is open and contains blocks of

nonnegative

integers having at most DIGITS_PER_BLOCK

digits per block.

Postcondition: Blocks have been removed from in and added

to myList.

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

---*/

/***** display *****/

void display(ostream & out) const;

/*------------------------------------------------------------------

-----

Display a BigInt.

Precondition: ostream out is open.

Postcondition: The large integer represented by this

BigInt object

has been formatted with the usual comma

separators and inserted

into ostream out.

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

----*/

/***** addition operator *****/

BigInt operator+(BigInt addend2);

/*------------------------------------------------------------------

------

Add two BigInts.

Precondition: addend2 is the second addend.

Postcondition: The BigInt representing the sum of the

large integer

represented by this BigInt object and addend2 is

returned.

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

----*/

private:

/*** Data Members ***/

list myList;

}; // end of BigInt class declaration

//-- Definition of constructor

inline BigInt::BigInt(int n)

{

do

{

myList.push_front(n % MODULUS);

n /= MODULUS;

}

while (n > 0);

}

//------ Input and output operators

inline istream & operator>>(istream & in, BigInt & number)

{

number.read(in);

return in;

}

inline ostream & operator(ostream>

{

number.display(out);

return out;

}

#endif

*************************************************************************

************************************************************************

/*-- BigInt.cpp-----------------------------------------------------------

--

This file implements BigInt member functions.

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

*/

#include

#include

using namespace std;

#include \"BigInt.h\"

//--- Definition of read()

void BigInt::read(istream & in)

{

static bool instruct = true;

if (instruct)

{

cout

\"

\"spaces. Enter a negative integer in last block to signal \"

\"the end of input. \";

instruct = false;

}

short int block;

const short int MAX_BLOCK = (short) pow(10.0, DIGITS_PER_BLOCK) - 1;

for (;;)

{

in >> block;

if (block

if (block > MAX_BLOCK)

cerr

else

myList.push_back(block);

}

}

//--- Definition of display()

void BigInt::display(ostream & out) const

{

int blockCount = 0;

const int BLOCKS_PER_LINE = 20; // number of blocks to display per

line

for (list::const_iterator it = myList.begin(); ; )

{

out

if (blockCount == 0)

out

if (it == myList.end())

return;

out

blockCount++ ;

it++;

if (it != myList.end())

{

out

if (blockCount > 0 && blockCount % BLOCKS_PER_LINE == 0)

out

}

}

}

//--- Definition of operator+()

BigInt BigInt::operator+(BigInt addend2)

{

BigInt sum;

short int first, // a block of 1st addend (this

object)

second, // a block of 2nd addend (addend2)

result, // a block in their sum

carry = 0; // the carry in adding two blocks

list::reverse_iterator // to iterate right to left

it1 = myList.rbegin(), // through 1st list, and

it2 = addend2.myList.rbegin(); // through 2nd list

while (it1 != myList.rend() || it2 != addend2.myList.rend())

{

if (it1 != myList.rend())

{

first = *it1;

it1++ ;

}

else

first = 0;

if (it2 != addend2.myList.rend())

{

second = *it2;

it2++ ;

}

else

second = 0;

short int temp = first + second + carry;

result = temp % 1000;

carry = temp / 1000;

sum.myList.push_front(result);

}

if (carry > 0)

sum.myList.push_front(carry);

return sum;

}

****************************************************************************

*************************************************************************

U CAN USE THIS BELOW TO TEST YOUR MODIFIED BigInt Class

****************************************************************************

#include

using namespace std;

#include \"BigInt.h\"

int main()

{

// However, you are not allowed to modify the following codes.

char response;

do

{

BigInt number1, number2;

cout \"enter>

cin >> number1;

cout \"enter>

cin >> number2;

// original one: test the operation +

cout

// 1. test the operation >

cout

number2) ? number1 : number2)

endl;

// 2. test the operation -

cout

// 3.(bonus) test the operation *

// comment the following out if you don't do 3.

cout

cout

cout

cin >> response;

}

while (response == 'y' || response == 'Y');

return 0;

}

*************************************************

************************************************

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions

Question

Are Gantt charts still viable project management tools? Explain.

Answered: 1 week ago

Question

Define procrastination and explain its causes.

Answered: 1 week ago