Menu

(Solved) : Gain Experience Using Stl Containers Iterators Project Description Modify Bigint Class Add Q29974684 . . .

To gain experience of using STL containers and iterators.

Project Description

Modify the BigInt class by adding the > operation todetermine whether one BigInt object is bigger than another. addingthe subtraction operation – to class BigInt: int1 – int2 whichshould return 0 if int1 is less than int2. (Bonus) adding themultiplication operation * to class BigInt.

Files are given for modification

BigInt.h

/*– 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 <iostream>

#include <iomanip> // setfill(), setw()

#include <list>

#include <cmath> // 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<short int>’s constructor was used tobuild

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<short int> constructor take care ofthis.

/***** 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<short int> 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 & out, constBigInt & number)

{

number.display(out);

return out;

}

#endif

BigInt.cpp

/*–BigInt.cpp———————————————————–

This file implements BigInt member functions.

————————————————————————–

*/

#include <iostream>

#include <cmath>

using namespace std;

#include “BigInt.h”

//— Definition of read()

void BigInt::read(istream & in)

{

static bool instruct = true;

if (instruct)

{

cout << “Enter ” << DIGITS_PER_BLOCK <<“-digit blocks, separated by

“spaces.nEnter a negative integer in last block to signal “

“the end of input.nn”;

instruct = false;

}

short int block;

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

for (;;)

{

in >> block;

if (block < 0) return;

if (block > MAX_BLOCK)

cerr << “Illegal block — ” << block << ” –ignoringn”;

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 displayper

line

for (list<short int>::const_iterator it = myList.begin();; )

{

out << setfill(‘0’);

if (blockCount == 0)

out << setfill(‘ ‘);

if (it == myList.end())

return;

out << setw(3) << *it;

blockCount++ ;

it++;

if (it != myList.end())

{

out << ‘,’;

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

out << endl;

}

}

}

//— 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<short int>::reverse_iterator // to iterate right toleft

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;

}

ProjectTest.cpp to test the code

//cmpsc122 Assignment 6

// Please do not modify this file!

// — Program to test BigInt class.

// Modified from textbook Larry Nyhoff, ADTs, Data Structures,and

Problem Solving

// with C++, 2nd ed., Prentice-Hall, 2005.

#include <iostream>

using namespace std;

#include “BigInt.h”

int main()

{

// However, you are not allowed to modify the followingcodes.

char response;

do

{

BigInt number1, number2;

cout <<“Enter a big integer:n”;

cin >> number1;

cout <<“Enter another big integer:n”;

cin >> number2;

// original one: test the operation +

cout << “The sum ofnt”

<< number1 << ” + ” << number2

<< “nisnt” << number1 + number2 <<endl;

// 1. test the operation >

cout << “nThe bigger number ofnt”

<< number1 << “nandnt” << number2

<< “nisnt” << ((number1 > number2) ? number1 :number2) <<

endl;

// 2. test the operation –

cout << “nThe subtraction ofnt”

<< number1 << ” – ” << number2

<< “nisnt” << number1 – number2 <<endl;

// 3.(bonus) test the operation *

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

cout << “nBONUS part:” << endl;

cout << “The multiplication ofnt”

<< number1 << ” * ” << number2

<< “nisnt” << number1 * number2 <<endl;

cout << “nAdd more integers (Y or N)? “;

cin >> response;

}

while (response == ‘y’ || response == ‘Y’);

return 0;

}

Sample Run

Enter a big integer:
Enter 3-digit blocks, separated by spaces.
Enter a negative integer in last block to signal the end ofinput.

347 965 434 213 -1
Enter another big integer:
298 432 678 984 -1
The sum of
347,965,434,213 + 298,432,678,984
is
646,398,113,197

The bigger number of
347,965,434,213
and
298,432,678,984
is
347,965,434,213

The subtraction of
347,965,434,213 – 298,432,678,984
is
49,532,755,229

BONUS part:
The multiplication of
347,965,434,213 * 298,432,678,984
is
103,844,256,726,016,399,679,592

Add more integers (Y or N)? Y
Enter a big integer:
453 213 345 -1
Enter another big integer:
892 -1
The sum of
453,213,345 + 892
is
453,214,237

The bigger number of
453,213,345
and
892
is
453,213,345

The subtraction of
453,213,345 – 892
is
453,212,453

BONUS part:
The multiplication of
453,213,345 * 892
is
404,266,303,740

Add more integers (Y or N)? N
Press any key to continue

Expert Answer


Answer to Gain Experience Using Stl Containers Iterators Project Description Modify Bigint Class Add Q29974684 . . .

OR