1. My bigInt multiplication does not work correctly it gives me the wrong output.
2. Also how do i modify the class BigInt to process both positive and negative large integers?
Merci beaucoup to anyone that helps!
Here is my code
BigInt.h
/*-- BigInt.h -------------------------------------------------------------
This header file defines the data type BigInt for processing
integers of any size.
Basic operations are:
Constructor
+: Addition operator
-: Subtraction operator
*: Multiplication operator
<: Lessthan operator
read(): Read a BigInt object
display(): Display a BigInt object
<<, >> : Input and output operators
Lab 8 CS 2413 modify the BigInt class in various ways
-------------------------------------------------------------------------*/
#include <iostream>
#include <iomanip> // setfill(), setw()
#include <list>
#ifndef BIGINT
#define BIGINT
const int DIGITS_PER_BLOCK = 3;
class BigInt
{
public:
/******** Function Members ********/
/***** Constructor *****/
// Let the list<short int> 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.
------------------------------------------------------------------------*/
/*** Less than operator ***/
bool operator<(BigInt);
/***** addition operator *****/
BigInt operator+(BigInt addend2);
//Compare operator
bool operator==(BigInt addend2);
//Subtraction operator
BigInt operator-(BigInt addend2);
//Multiplication 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
//------ 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
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.\n\n";
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\n";
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<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 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;
}
//Definition of less than operator
bool BigInt::operator <(BigInt number2)
{
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
bool res = false;
list<short int>::const_iterator // to iterate right to left
it1 = myList.begin(), // through 1st list, and
it2 = number2.myList.begin(); // through 2nd list
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;
if(first < second)
return true;
else if (first > second)
return false;
else
res = false;
}
}
//Definition for compare operator
bool BigInt::operator ==(BigInt number2)
{
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
bool res = false;
list<short int>::const_iterator // to iterate right to left
it1 = myList.begin(), // through 1st list, and
it2 = number2.myList.begin(); // through 2nd list
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;
if(first == second)
return true;
}
}
//Definition of subtraction
BigInt BigInt::operator-(BigInt addend2)
{
BigInt subtraction;
short int first, // a block of 1st addend (this object)
second, // a block of 2nd addend (addend2)
result, // a block in their result
carry = 0; // the carry in adding two blocks
list<short int>::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;
subtraction.myList.push_front(result);
}
if (carry > 0)
subtraction.myList.push_front(carry);
return subtraction;
}
//Definition of multiplication
BigInt BigInt::operator*(BigInt addend2)
{
BigInt multiply;
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 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;
multiply.myList.push_front(result);
}
if (carry > 0)
multiply.myList.push_front(carry);
return multiply;
}
driver.cpp
#include <iostream>
using namespace std;
#include "BigInt.h"
int main()
{
char response;
do
{
BigInt number1, number2;
cout <<"Enter a big integer:\n";
cin >> number1;
cout <<"Enter another big integer:\n";
cin >> number2;
cout << " " << endl;
cout << "The Sum of\n\t"
<< number1 << "\nand\n\t" << number2
<< "\nis\n\t" << number1 + number2 << endl;
cout << " " << endl;
//Get boolean result for lessthan operator
bool lessthan = number1 < number2;
//If the first number is less than the second number output 0 as the result
if (lessthan == 1) {
cout << "The Subtraction of\n\t"
<< number1 << "\nand\n\t" << number2
<< "\nis\n\t" << 0 << " because the first input is less than the second " << endl;
}
//Otherwise do the regular subtraction
else {
cout << "The Subtraction of\n\t"
<< number1 << "\nand\n\t" << number2
<< "\nis\n\t" << number1 - number2 << endl;
}
//show the multiplication of both
cout << " " << endl;
cout << "The Multiplication of\n\t"
<< number1 << "\nand\n\t" << number2
<< "\nis\n\t" << number1 * number2 << endl;
cout << " " << endl;
cout << " " << endl;
//Output if its lessthan and equal
cout << "Is the first less than the second? " << lessthan << endl;
cout << "Are the two equal? " << (number1 == number2) << endl;
//Prompt user for more integers
cout << "\nAdd more integers (Y or N)? ";
cin >> response;
}
while (response == 'y' || response == 'Y');
}