In: Computer Science
Language: C++
I am starting to make a Bigint ADT and i have the hpp file finished now i need to make the methods for the functions.
Problem:
The data type int in C++ is limited to the word size of the CPU
architecture (e.g., 32 or 64 bit). Therefore you can only work with
signed integers up to 2,147,483,647 (in the case of signed 32 bit).
Unsigned 32 bit is still only 10 digits. Maxint for 64 is somewhat
larger at 9,223,372,036,854,775,807 but still only 19 digits.
Clearly, this causes difficulties for working with very large
integer values (say 100 digits). Your job is to develop an ADT
(called bigint) that can take any size postive integer. It
will work for 100, 200, 500, etc. digit integers.
Representation is a key issue for this assignment. We recommend an
array of integers, with each element representing one single digit
(0 to 9) of the big number. One could use an array of char, but the
memory savings is pretty minimal. Placing the values in the array
is the interesting part. The naïve representation makes storing the
bigint easy but makes the operations (add and multiply) very
difficult to implement. A slightly more clever representation makes
storing the big number a little bit harder but makes implementing
the operations way easier.
Arrays are typically drawn to be read left to right with the 0
element on the left and the largest on the right. However, arrays
are a completely made up concept and are not physical in nature. So
you can draw them and think about them anyway you want. For this
problem having the right side as the 0 element and the left side as
the largest makes much more sense.
Take the example of the number 299,793. We show how it is stored in
the array below. The 3 is in the one's position, the 9 in the 10's
position and so on. This neatly corresponds to the index of the
array. The addition and multiple algorithms given below use this
representation.
bigint
Index: | n | ... | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Place: | 10^n's | ... | 10^7's | 10^6's | 10^5's | 10000's | 1000's | 100's | 10's | 1's |
Value: | 0 | ... | 0 | 0 | 2 | 9 | 9 | 7 | 9 | 3 |
Directions:
bigint.hpp:
#ifndef BIGINT_HPP
#define BIGINT_HPP
const int CAPACITY = 400;
class bigint {
public:
bigint(); //default constructor
bigint(int);
bigint(const char[]);
void debugPrint(std::ostream&) const;
bool operator<< (const bigint&) const;
bool operator== (const bigint&) const;
private:
int j_[CAPACITY];
int num;
};
bigint.cpp: // start of method file
#include
#include "bigint.hpp"
bigint::bigint(){
for(int i=0;i j_[i]=0;
}
}
bigint::bigint(int){
}
bigint::bigint(const char[]){
}
Please find the explanation int the comments in the functions.
The code is tested;
#include "Header.h"
//since size of zero int is not given just set the num to
zero;
bigint::bigint()
{
num = 0;
}
//given a number in
bigint::bigint(int in)
{
//declare an array of capacity
int arr[CAPACITY];
num = 0;
while (in)
{
//extract the last digit of the
in
arr[num] = in % 10;
//remove the last digit from
in
in = in / 10;
//count the number of digits
remaining
num++;
}
//the number collected in arr is reverse of the
original number so reverse
// and store in j_
for (int i = 0; i < num; i++)
{
j_[i] = arr[num - i - 1];
}
}
bigint::bigint(const char n[])
{
num = 0;
int i = 0;
//nzFound stores if we are iterating over leading
zeros.
bool nzFound = false;
while (n[i] != '\0')
{
//as long as iterating over leading
zeros, dont store that digit
if (!nzFound && n[i] ==
'0')
{
i++;
continue;
}
//since finding first non zero
digit set the flag
//so that all upcoming zeros should
be stored in j_
nzFound = true;
//converting ascii value to
integer
j_[num] = n[i]-'0';
num++;
i++;
}
}
void bigint::debugPrint(std::ostream& o) const
{
int i;
for (i = 0; i < num-1; i++)
{
o << j_[i] <<
'|';
}
o << j_[i];
}
bool bigint::operator== (const bigint &b) const
{
if (num != b.num)
return false;
for (int i = 0; i < num; i++)
{
if (j_[i] != b.j_[i])
return
false;
}
return true;
}
bool operator<<(std::ostream o, const bigint &b)
{
int i;
for (i = 0; i < b.num; i++)
{
o << b.j_[i] <<
'|';
//print every 80th char to be \n
thus not printing more than 80 digits per line.
if ( (i + 1) % 80 == 0)
o <<
'\n';
}
return true;
}