Question

Big Numbers Library The creators of C++ have created primitive data types to allow developers to store numbers of various size they expect most users will need. Such as short, int, double. In our c...

  1. Big Numbers Library

The creators of C++ have created primitive data types to allow developers to store numbers of various size they expect most users will need. Such as short, int, double.

In our case we want to expand on these primitive data types and allow users to store big numbers that can store up to 1,000 digits long. In order to do this, you will create a BigNumbers class, using TDD, Test Driven Development, that will use vectors from STL to store the digits. BigNumbers should have the same functionality as other numerical data types. Hence, they should be able to:

Add

Subtract

Multiply

Divide

Mod

All of the operations listed above should be completed using operator overloading, ADD “+” operator, SUBTRACT “-”…etc.

Finally, this should be created as a static library for users to download off your github account.

I need a cpp file and .h

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// BigNumbers.h

#include <iostream>
#include <vector>
#include <string>
using namespace std;


class BigNumbers{
private:
vector <int> digits; // vector to store digits of big integer
bool ispositive; // boolean to check number is positive or negative
void addition(const vector <int> &p,const vector <int> &q, vector <int> &r); // helper function for addition
void subtraction(const vector <int> &p ,const vector <int> &q, vector <int> &r); // helper function for subtraction
void reversenumber( vector <int> & r); // helper function to reverse a number
bool findmaximum(const vector <int> & p,const vector <int> & q); // helper function to find maximum between two numbers

public:
BigNumbers(); // empty constructot
BigNumbers(string str); // paramterized constructor
BigNumbers operator -(const BigNumbers & bignum); // overload - operator
BigNumbers operator *(const BigNumbers & bignum); // overload * operator
BigNumbers operator +(const BigNumbers & bignum); // overload + operator
BigNumbers & operator =(const BigNumbers & bignum); // overload = operator
void printbiginteger(); // method to print big integer
};


//BigNumbers.cpp
#include "BigNumbers.h"
#include "math.h"

using namespace std;

class BigNumbers{

// empty constructor
BigNumbers::BigNumbers() {
digits.push_back(0);
ispositive=false;
}
  
// paramterized constructor
BigNumbers::BigNumbers(string str){
for(int k=0;k<str.size();k++) {
if(str[k]!='-') {
digits.push_back(str[k]-'0');
}
}
if(str[0]=='-') ispositive=true;
else ispositive=false;
}
  
// helper method for addition
void BigNumbers::addition(const vector <int> & p,const vector <int> & q, vector <int> & r) {
long long int num1=0,num2=0,answer=0;
BigNumbers biganswer; // variable to store answer
int temp=1;
biganswer.digits.clear(); // clear digits of biganswer
for(int i=p.size()-1;i>=0;i--)
{
num1+=p[i]*temp;
temp*=10;
}
temp=1;
for(int i=q.size()-1;i>=0;i--)
{
num2+=q[i]*temp;
temp*=10;
}
answer=num1+num2;
while(answer>0)
{
r.push_back(answer%10); // push to r
answer=answer/10; // divide answer by 10 for next field
}
reversenumber(r); // reverse the number as push_back adds the number to the back of vector r

}
  
// helper method for subtraction
void BigNumbers::subtraction(const vector <int> & p,const vector <int> & q, vector <int> & r){
long long int num1=0,num2=0,answer=0;
BigNumbers biganswer; // variable to store answer
int temp=1;
biganswer.digits.clear(); // clear digits of biganswer
for(int i=p.size()-1;i>=0;i--)
{
num1+=p[i]*temp;
temp*=10;
}
temp=1;
for(int i=q.size()-1;i>=0;i--)
{
num2+=q[i]*temp;
temp*=10;
}
answer=num1-num2;
while(answer>0){
r.push_back(answer%10); // push to r
answer/=10; // divide answer by 10 for next field
}
reversenumber(r); // reverse the number as push_back adds the number to the back of vector r

}

// helper method for finding maximum
bool BigNumbers::findmaximum(const vector <int> & num1,const vector <int> & num2){
if(num1.size()>num2.size())
return true;
else
return false;
}
  
// helper method to find reverse of number
void BigNumbers::reversenumber(vector <int> & r) {
int tempnum,j=r.size()-1;
for(int i=0;i<r.size()/2;i++)
{
tempnum=r[i];
r[i]=r[j];
r[j]=tempnum;
j-=1;
}
}

  
// oveload * operator
BigNumbers BigNumbers::operator *(const BigNumbers & bignum){
BigNumbers biganswer; // variable to store answer
long long int num1=0,num2=0,answer=0;
int temp=1;
biganswer.digits.clear();
for(int i=digits.size()-1;i>=0;i--)
{
num1+=digits[i]*temp;
temp*=10;
}
temp=1;
for(int i=bignum.digits.size()-1;i>=0;i--)
{
num2+=bignum.digits[i]*temp;
temp*=10;
}
answer=num1*num2;
if(ispositive==bignum.ispositive)
biganswer.ispositive=false;
else
biganswer.ispositive=true;
while(answer>0)
{
biganswer.digits.push_back(answer%10); // push to biganswer
answer=answer/10; // divide by 10 for next field
}
reversenumber(biganswer.digits); // reverse the number as push_back adds the number to the back of vector r
return biganswer;
}
  
//overload + operator
BigNumbers BigNumbers::operator +(const BigNumbers & bignum){
BigNumbers biganswer; // variable to store answer
biganswer.digits.clear(); // clear all digits of biganswer
bool first;
if(ispositive==bignum.ispositive){ // if both num are positive add them
biganswer.ispositive=ispositive;
addition(digits,bignum.digits,biganswer.digits);
}
else{
first= findmaximum(digits,bignum.digits); // find max of two if one is negative
if(first) { // if first is greater than subtract second from first else vice-versa
subtraction(digits,bignum.digits,biganswer.digits);
}
else {
subtraction(bignum.digits,digits,biganswer.digits);
}
biganswer.ispositive=false;
if(ispositive&&first) {
biganswer.ispositive=true;
}
if((!ispositive)&&(!first)) biganswer.ispositive=true;
}
return biganswer;
}
  
  
// Overload = operator
BigNumbers & BigNumbers::operator =(const BigNumbers & bignum){
for(int i=0;i<bignum.digits.size();i++)
digits.push_back(bignum.digits[i]);
ispositive=bignum.ispositive;
return (*this);
}
  
// overload - operator
BigNumbers BigNumbers::operator -(const BigNumbers & bignum){
BigNumbers biganswer; // variable to store answer
biganswer.digits.clear(); // clear all digits of biganswer
bool first=findmaximum(digits,bignum.digits);
if(ispositive!=bignum.ispositive){
biganswer.ispositive=true;
if(first) {
addition(digits,bignum.digits,biganswer.digits);
}
else {
addition(bignum.digits,digits,biganswer.digits);
}
} else if(ispositive&&bignum.ispositive){
if(first) {
subtraction(digits,bignum.digits,biganswer.digits);
biganswer.ispositive=true;
  
} else {
subtraction(bignum.digits,digits,biganswer.digits);
biganswer.ispositive=false;
  
}
} else {
if(first){
biganswer.ispositive=false;
subtraction(digits,bignum.digits,biganswer.digits);
  
} else {
biganswer.ispositive=true;
subtraction(bignum.digits,digits,biganswer.digits);
}
}
return biganswer;

}


  
  
// Helper function to print big numbers
void BigNumbers::printbiginteger(){
if(ispositive == true)
cout<<"-";
else
cout<<"";

bool notzeropresent=false;
  
for(int k=0;k<digits.size();k++){
if(digits.size()!=1&& digits[k]==0 && notzeropresent==false)
{
digits[k]=digits[k]; }
else
{
notzeropresent=true;
cout<<digits[k];
}
}
cout<<endl;
}


};

Add a comment
Know the answer?
Add Answer to:
Big Numbers Library The creators of C++ have created primitive data types to allow developers to store numbers of various size they expect most users will need. Such as short, int, double. In our c...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT