BigInt.cpp and BigInt.h I have already finished the first 2 parts, which are the
ID: 3761204 • Letter: B
Question
BigInt.cpp and BigInt.h
I have already finished the first 2 parts, which are the subtraction and the < operator. All i need now is the bonus part, here are my files so far.
BigInt.cpp
---------------
#include
#include
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. 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 < 0) return;
if (block > MAX_BLOCK)
cerr << "Illegal block -- " << block << " -- ignoring ";
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 << 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::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;
result = temp % 1000;
carry = temp / 1000;
sum.myList.push_front(result);
}
if (carry > 0)
sum.myList.push_front(carry);
return sum;
}
BigInt BigInt::operator-(BigInt addend2) //subtraction function
{
BigInt total;
short int first, second, result;
list::reverse_iterator
it1 = myList.rbegin(),
it2 = addend2.myList.rbegin();
while (it1 != myList.rend() || it2 != addend2.myList.rend())
{
if (it1 != myList.rend())
{
first = *it1;
}
else
first = 0;
if (it2 != addend2.myList.rend())
{
second = *it2;
}
else
second = 0;
if (first < second) //if the first set of numbers is less than the second it enters this loop
{
first = first + 1000;
short int temp = first - second; //subtracts the two numbers
result = temp % 1000;
first = first - 1000;
}
else
{
short int temp = first - second;
result = temp % 1000;
}
if (it1 != myList.rend())
{
it1++;
if (first < second)
{
first = *it1;
first = first - 1; //subtracts the 1
*it1 = first;
}
}
if (it2 != addend2.myList.rend())
{
it2++;
}
total.myList.push_front(result);
}
return total;
}
bool BigInt ::operator<(BigInt rum)
{
bool variable;
short int first, second;
list::reverse_iterator // to iterate right to left
it1 = myList.rbegin(), // through 1st list
it2 = rum.myList.rbegin(); // through 2nd list
while (it1 != myList.rend() || it2 != rum.myList.rend())
{
if (it1 != myList.rend())
{
first = *it1;
it1++;
}
else
first = 0;
if (it2 != rum.myList.rend())
{
second = *it2;
it2++;
}
else
second = 0;
}
if (first < second)
return true;
else
return false;
}
BigInt.h
------------
#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 Constructor
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.
------------------------------------------------------------------------*/
bool BigInt ::operator<(BigInt num); //< operator function
BigInt operator-(BigInt addend2); // subtraction function
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 & out, const BigInt & number)
{
number.display(out);
return out;
}
#endif
proj11test.cpp
---------------------
//cmpsc122 Su
// 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
using namespace std;
#include "BigInt.h"
int main()
{
char response;
do
{
BigInt number1, number2;
cout <<"Enter a big integer: ";
cin >> number1;
cout <<"Enter another big integer: ";
cin >> number2;
// original one: test the operation +
cout << "The sum of "
<< number1 << " + " << number2
<< " is " << number1 + number2 << endl;
// 1. test the operation <
cout << " The bigger number of "
<< number1 << " and " << number2
<< " is " << ((number1 < number2) ? number2 : number1) << endl;
// 2. test the operation -
cout << " The subtraction of "
<< number1 << " - " << number2
<< " is " << number1 - number2 << endl;
// 3.(bonus) test the operation *
// comment the following out if you don't do 3.
cout << " BONUS part:" << endl;
cout << "The multiplication of "
<< number1 << " * " << number2
<< " is " << number1 * number2 << endl;
// end of comment out for bonus part 3.
cout << " Add more integers (Y or N)? ";
cin >> response;
}
while (response == 'y' || response == 'Y');
return 0;
}
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
#include "BigInt.h"
void BigInt::read(istream & in)
{
static bool instruct = true;
if (instruct)
{
cout << "Enter " << digts block << "-digit blocks, separated by "
"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 < 0) return;
if (block > MAX_BLOCK)
cerr << "Illegal block -- " << block << " -- ignoring ";
else
myList.push_back(block);
}
}
// Method for display()
void BigInt::display(ostream & out) const
{
int blockCount = 0;
const int BLOCKS_PER_LINE = 20;
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++ ;
if (it != myList.end())
{
out << ',';
if (blockCount > 0 && blockCount % BLOCKS_PER_LINE == 0)
out << endl;
}
}
}
// Method for operator+()
BigInt BigInt::operator+(BigInt addend2)
{
BigInt sum;
short int first,
second,
result,
carry = 0;
list<short int>::reverse_iterator
it1 = myList.rbegin(),
it2 = addend2.myList.rbegin();
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;
}
//Method for the adding//
bool BigInt::operator +<(BigInt number2)
{
BigInt add;
short int first,
second,
result,
carry = 0;
bool res = false;
list<short int>::const_iterator
it1 = myList.begin(),
it2 = number2.myList.begin();
while (it1 != myList.end() || it2 != number2.myList.end())
{
if (it1 != myList.end())
{
first = *it1;
it1++ ;
}
else
first = 0;
if (it2 != number2.myList.end())
{
second = *it2;
it2++ ;
}
else
second = 0;
short int temp = first + second;
result = temp % 1000;
carry = temp / 1000;
add.myList.push_front(result);
}
if (carry > 0)
add.myList.push_front(carry);
return add;
}
//Method for subtraction
BigInt BigInt::operator-(BigInt addend2)
{
BigInt sub ;
short int first,
second,
result,
carry = 0;
list<short int>::reverse_iterator
it1 = myList.rbegin(),
it2 = addend2.myList.rbegin();
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;
sub .myList.push_front(result);
}
if (carry > 0)
sub.myList.push_front(carry);
return sub ;
}
//method for multiplication
BigInt BigInt::operator*(BigInt addend2)
{
BigInt multiplication;
short int first,
second,
result,
carry = 0;
list<short int>::reverse_iterator
it1 = myList.rbegin(),
it2 = addend2.myList.rbegin();
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;
result = temp % 1000;
carry = temp / 1000;
multiplication.myList.push_front(result);
}
if (carry > 0)
multiply.myList.push_front(carry);
return multiplication;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.