(Important* bigint is the name of the class in the header file. There is pseudoc
ID: 3792883 • Letter: #
Question
(Important* bigint is the name of the class in the header file. There is pseudocode for the overload operator*)
Implementation:
A method bigint times_digit(int) const; to multiply a bigint and a single digit integer between 0 and 9 Note that this is different than bigint * int because there the int will be converted into a bigint automatically by the constructor so would end up being bigint * bigint.
A method bigint times_10(int) const; to multiply a bigint by 10^n with n>0. (a.k.a. shift left base 10). For example, given x == 234, x.times_10(2) == 23400, that is 234 * 10^2. This can be implemented simply by shifting the bigint n digits to the left.
Overload operator* to multiply two bigints. Implement this multiply using the shift left and times digit methods above. The algorithm to multiply two bigints is as follows:
//To compute A * B
//B[0] is 1s place, B[1] is 10s place, B[2] is 100s place, etc.
product = 0;
for i = 0 to maxDigits-1 do
//product = product + ( (A * B[i]) * 10^i )
temp = A.times_digit(B[i]);
product = product + temp.times_10(i);
end for
Testing:
Build unit tests for the times 10 and times digit methods.
Test with different values and ranges.
The tests for multiply are provided and your method should pass this test completely.
Do you think these tests are complete? If not feel free to add additional ones.
You will need to update the Makefile - instructions are in the Makefile.
The command make tests will build and run the unit tests.
Create a main body (name the file multiply.cpp) that reads from the file data1-2.txt and does the following:
Test for success of opening the file in your program.
Read in two numbers into bigints and write both out on separate lines.
Multiply these together and write the result.
Then read in two more big numbers, multiplying them and writing the result until end of file.
All output must be labeled and neat.
The command make multiply will build and run this program.
Explanation / Answer
bigintDriver:
#include "bigint.h"
#include <iostream>
using namespace std;
int main(){
// Setup fixture
bigint left(12);
bigint right(4);
bigint result;
// Test
result = left * right;
cout<<left<<" * "<<right<<" = "<<result<<endl<<endl;
cout<<result<<".times_digit(2) = "<<result.times_digit(2)<<endl<<endl;
// Setup fixture
bigint left1(123);
bigint right1(56);
// Test
result = left1 + right1;
cout<<left1<<" + "<<right1<<" = "<<result<<endl<<endl;
cout<<result<<".times_10(3) = "<<result.times_10(3)<<endl<<endl;
return 0;
}
bigint.h:
#ifndef BIGINT_H_
#define BIGINT_H_
#include <iostream>
#include <cassert>
#include <cstdlib>
using namespace std;
const int maxSize=100;
class bigint{
public:
bigint ();
bigint (int);
bigint(const char[]);
bool operator==(const bigint&);
void output(ostream&);
void zero();
bigint operator+(bigint)const;
int operator[](int)const ;
bigint times_digit(int);
bigint times_10(int);
bigint operator*(bigint);
friend ostream& operator<<(ostream&, bigint);
friend istream& operator>>(istream&, bigint&);
private:
int bigInt[maxSize];
};
#endif
bigint.cpp:
#include "bigint.h"
void bigint::zero() { // Initializes a bigint to zero.
for (int i = 0; i < maxSize; ++i)
bigInt[i] = 0;
}
bigint::bigint(){ // Default constructor
zero();
}
bigint::bigint(int x){ // Initialize with an int value provided
zero();
for (int i = 0; i<maxSize; i++){
bigInt[i] = x % 10;
x /= 10;
}
}
bigint::bigint(const char c[]){ // Initialize a bigint to a char[] provided
zero();
int len = 0;
int i = 0;
do {
++len;
} while (c[len] != '');
for (int j = len - 1; j >= 0; j--, i++){
bigInt[i] = int(c[j]) - int('0');
}
}
bool bigint::operator==(const bigint& right){ // A method to compare if two bigints are equal
for (int i = 0; i<maxSize; i++){
if (bigInt[i] != right.bigInt[i])
{
return false;
}
}
return true;
}
void bigint::output(ostream& out){ // takes a stream as input and writes a bigint to that stream.
int size = maxSize, newOut = 0;
do{
--size;
} while (size>0 && bigInt[size] == 0);
do{
if (size % 80)
out << bigInt[size];
else
out << bigInt[size];
--size, ++newOut;
} while (size >= 0);
}
bigint bigint::operator+(bigint right)const { // Overload the addition operator
int r = 0, temp = 0;
for (int i = 0; i<maxSize; ++i){
r = bigInt[i] + right.bigInt[i] + r;
temp = r % 10;
r /= 10;
right.bigInt[i] = temp;
}
return right;
}
int bigint::operator[](int i)const{ // Overload the subscript operator[]
if ((i<0) || (i >= maxSize)) return 0;
return bigInt[i];
}
ostream& operator<<(ostream& out, bigint right){ // Overload the extraction operator
right.output(out);
return out;
}
istream& operator>>(istream& in, bigint& right){ // Overload the insertion operator
char ch, charArr[maxSize];
for (int j = 0; j<maxSize; j++){
charArr[j] = 0;
}
in >> ch;
for (int i = 0; i<maxSize && ch != ';'; ++i){
charArr[i] = ch;
in >> ch;
}
right = bigint(charArr);
return in;
}
bigint bigint::times_digit(int x){ // Multiply a bigint and a single digit integer between 0 and 9
bigint temp;
int r = 0, result = 0;
for (int i = 0; i<maxSize; i++){
r = bigInt[i] * x + r;
result = r % 10;
r /= 10;
temp.bigInt[i] = result;
}
return temp;
}
bigint bigint::times_10(int x){ // A method to multiply a bigint by 10^n with n>0
bigint temp;
for (int i = maxSize - 1 - x; i >= 0; i--){
temp.bigInt[i + x] = bigInt[i];
}
for (int j = 0; j<x; j++){
temp.bigInt[j] = 0;
}
return temp;
}
bigint bigint::operator*(bigint rhs){ // Overload * to multiply two bigints
bigint product = 0;
for (int i = 0; i<maxSize; i++){
product = product + (times_digit(rhs.bigInt[i])).times_10(i);
}
return product;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.