Each BigInt object will need: Data members : The digits of the number must be st
ID: 3538819 • Letter: E
Question
Each BigInt object will need:
Data members:
The digits of the number must be stored in a vector.
Functions:
All should be member functions except those specified non-member.
BigInt class Implement the BigInt class described here. Each BigInt object will need:
Data members:
The digits of the number must be stored in a vector.
- Use a vector of
ints. Each element holds a digit of the number. - Put the least significant digit at subscript 0.
- The size of the vector should be the number of digits in the number.
- Do not have any leading zeros in the number.
Functions:
All should be member functions except those specified non-member.
BigInt(...) Constructors Allow for the following possibilities, create a BigInt object: - With a default value of 0
- From a non-negative
int - From a string
BigInt& operator+=(const BigInt&) The BigInt object's value is changed to its old value plus the value of the parameter BigInt object. BigInt& operator++() The usual behavior for pre-increment. BigInt operator++(int) The usual behavior for post-increment. int& operator[](int index) Returns the digit whose positional value is the same as the parameter. See the notes, there needs to be two versions of this function. BigInt& operator*=(const BigInt&) The BigInt object's value is changed to its old value times the value of the parameter BigInt object. Use times_10 and times_single_digit. ostream& operator<<(ostream&, const BigInt&) The value of the BigInt object is output.Non-member.
istream& operator>>(istream&, BigInt&) Input, should behave as usual.Non-member.
BigInt operator+(const BigInt&, const BigInt&) Usual addition, result, a BigInt is returned. No side effect.Non-member.
BigInt operator*(const BigInt&, const BigInt&) Usual multiplication, result, a BigInt is returned. No side effect.Non-member.
void times_10(int n) Multiply the object by a power of 10, the power of 10 is given by the parameter. 100 is 1 so if the parameter is 0 the object is unchanged.
void times_single_digit(int n) Multiply a
BigInt times a single digit n where 0 <= n <= 9. int size() Return the number of digits the
BigInt has. string to_string() Return a string that is the digits of the number.
A complete
BigInt type is not being created, only implement what is above. Negative numbers need not be handled.
Explanation / Answer
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
class BigInt
{
public:
BigInt();
BigInt(int);
BigInt(string &);
BigInt& operator+=(const BigInt&);
BigInt& operator++();
BigInt operator++(int);
int& operator[](int index) ;
BigInt& operator*=(const BigInt&);
friend ostream& operator<<(ostream&, const BigInt&);
friend istream& operator>>(istream&, BigInt&);
BigInt operator+(const BigInt&) ;
BigInt operator*(const BigInt&);
void times_10(int n);
void times_single_digit(int n);
int size() const;
string to_string();
~BigInt();
private:
/* data */
vector<int> num;
};
BigInt :: BigInt()
{
num.push_back(0);
}
BigInt :: BigInt(int n)
{
while(n!=0)
{
num.push_back(n%10);
n/=10;
}
}
BigInt :: BigInt(string &str)
{
for(int i=str.size()-1 ; i>=0 ; i--)
{
num.push_back(str[i] - '0');
}
}
BigInt& BigInt :: operator+=(const BigInt& num2)
{
// this->num = num + n;
// return *this;
vector<int> sum;
vector<int> num1 = num;
int size1 = num1.size();
int size2 = num2.size();
int size = (size1>size2)?size1:size2;
for(int i=0;i<size;i++)
{
if(i>=size1)
{
sum.push_back(num2[i]);
}
else if(i>=size2)
{
sum.push_back(num1[i]);
}
else
{
sum.push_back(num1[i]+num2[i]);
}
}
for(int i=1;i<sum.size();i++)
{
sum[i] = sum[i]+sum[i-1]/10;
sum[i-1]%=10;
}
if(sum[sum.size()-1]>9)
{
sum.push_back(sum[sum.size()-1]/10);
sum[sum.size()-2]%=10;
}
num = sum;
return *this;
}
BigInt& BigInt :: operator++()
{
vector<int> unit(1,1);
num += unit;
return *this;
}
BigInt BigInt :: operator++(int)
{
BigInt old = *this;
++(*this);
return old;
}
int& BigInt :: operator[](int index)
{
return *(num.begin()+index);
}
BigInt& BigInt :: operator*=(const BigInt& num2)
{
BigInt prod;
for(int i=0;i<num2.size();i++)
{
BigInt temp = *this;
temp.times_single_digit(num2[i]);
temp.times_10(i);
prod+=temp;
}
*this = prod;
return *this;
}
ostream& operator<<(ostream& out, const BigInt& n)
{
for(int i=n.size()-1 ; i>=0 ; i--)
{
out << n[i];
}
return out;
}
istream& operator>>(istream& in, BigInt& n)
{
for(int i=n.size()-1 ; i>=0 ; i--)
{
in >> n[i];
}
return in;
}
BigInt BigInt :: operator+(const BigInt& num2)
{
vector<int> sum;
vector<int> num1 = num;
int size1 = num1.size();
int size2 = num2.size();
int size = (size1>size2)?size1:size2;
for(int i=0;i<size;i++)
{
if(i>=size1)
{
sum.push_back(num2[i]);
}
else if(i>=size2)
{
sum.push_back(num1[i]);
}
else
{
sum.push_back(num1[i]+num2[i]);
}
}
for(int i=1;i<sum.size();i++)
{
sum[i] = sum[i]+sum[i-1]/10;
sum[i-1]%=10;
}
if(sum[sum.size()-1]>9)
{
sum.push_back(sum[sum.size()-1]/10);
sum[sum.size()-2]%=10;
}
string str;
for(int i=sum.size()-1;i>=0;i--)
str.push_back(sum[i]+'0');
return BigInt(sum);
}
BigInt BigInt :: operator*( const BigInt& num2)
{
BigInt temp = *this;
temp*= num2;
return temp;
}
void BigInt :: times_10(int n)
{
vector<int> p_10(n,0);
for(int i=0; i<num.size() ; i++)
{
p_10.push_back(num[i]);
}
this->num = p_10;
}
void BigInt :: times_single_digit(int n)
{
num[0]*=n;
for(int i=1;i<num.size();i++)
{
num[i] = num[i]*n + num[i-1]/10;
num[i-1]%=10;
}
if(num[num.size()-1]/10)
{
num.push_back(num[num.size()-1]/10);
num[num.size()-2]%=10;
}
}
int BigInt :: size() const
{
return num.size();
}
string BigInt :: to_string()
{
string str;
for(int i=num.size()-1 ; i>=0 ; i--)
{
str.push_back(num[i]+'0');
}
return str;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.