Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write program using C++. This is the complete instruction. Wirit a pmegnam to pr

ID: 3888396 • Letter: W

Question

Write program using C++. This is the complete instruction.

Wirit a pmegnam to proces numbers of any mgnn a BigInteger with constructors and overload operators indicated below class BigInteger f public: BigInteger (const char*) BigInteger (int); BigInteger (BigInteger&); BigInteger() BigInteger operatort (const BigInteger&) const; BigInteger operator+- (const BigInteger&); BigInteger operator= (const BigInteger&); BigInteger operator++(); eme Biginteger operator++(int) bool operator (const BigInteger&) const; bool operator

Explanation / Answer

Given below is an implementation for BigInteger in .h and .cpp files along with a sample program to use it. Hope it helps. If it helped, please do rate the answer. Thank you.

BigInteger.h

#ifndef BigInteger_h
#define BigInteger_h
#include <iostream>
using namespace std;
class BigInteger
{

public:
  
BigInteger(const char* val);
BigInteger(int );
BigInteger(const BigInteger& num);
~BigInteger();
  
BigInteger operator +(const BigInteger &other);
BigInteger operator +=(const BigInteger &other);
  
BigInteger operator = (const BigInteger &other);
BigInteger operator ++(int);
BigInteger operator ++();
  
bool operator ==(const BigInteger &other);
bool operator <(const BigInteger &other);

friend ostream& operator <<(ostream &out, const BigInteger &num);
private:
char* value;
void add(char *n1, char *n2 ,char* &answer);

};

#endif /* BigInteger_h */

BigInteger.cpp


#include "BigInteger.h"
#include <cstring>
#include <iostream>
using namespace std;

BigInteger::BigInteger(const char* val)
{
value = new char[strlen(val) + 1];
strcpy(value, val);
}
BigInteger::BigInteger(int num)
{
string str = to_string(num);
value = new char[str.length() + 1];
strcpy(value, str.c_str());
}
BigInteger::BigInteger(const BigInteger &other)
{
value = new char[strlen(other.value) + 1];
strcpy(value, other.value);
  
}
BigInteger BigInteger::operator = (const BigInteger &other)
{
delete []value;
value = new char[strlen(other.value) + 1];
strcpy(value, other.value);
return *this;
}

BigInteger::~BigInteger()
{
delete []value;
}
BigInteger BigInteger::operator +(const BigInteger &other)
{
BigInteger result(0);
int len1 = strlen(value);
int len2 = strlen(other.value);
int len3 = 1 + (len1 > len2 ? len1 : len2);
char *res = new char[len3];
res[len3] = '';
int idx1 = len1 -1;
int idx2 = len2 - 1;
int idx3 = len3 - 1;
int d1, d2, sum, carry = 0;
while(idx1 >= 0 || idx2 >= 0)
{
if(idx1 >= 0)
d1 = value[idx1--] - '0';
else
d1 = 0;
  
if(idx2 >= 0)
d2 = other.value[idx2--] - '0';
else
d2 = 0;
  
sum = d1 + d2 + carry;
if(sum >= 10)
{
carry = 1;
sum -= 10;
}
else
carry = 0;
  
res[idx3--] = sum + '0';
  
}
if(carry != 0)
res[idx3--] = carry + '0';
  
delete []result.value;
result.value = new char[strlen(res + idx3 + 1) + 1];
strcpy(result.value, res + idx3 + 1);
delete []res;
return result;
  
}

BigInteger BigInteger::operator +=(const BigInteger &other)
{
*this = (*this) + other;
return *this;
}

bool BigInteger::operator ==(const BigInteger &other)
{
return strcmp(value, other.value) == 0;
}
bool BigInteger::operator <(const BigInteger &other)
{
return strcmp(value, other.value) < 0;
  
}

BigInteger BigInteger::operator ++(int)
{
BigInteger oldValue = *this;
*this = *this + 1;
return oldValue;
}
BigInteger BigInteger::operator ++()
{
*this = *this + 1;
return *this;
}
ostream& operator << (ostream &out, const BigInteger &num)
{
out << num.value;
return out;
}

Test.cpp

#include "BigInteger.h"
#include <iostream>
using namespace std;
int main()
{
BigInteger num1(5);
BigInteger num2("500");
BigInteger num3(1122334455);
  
cout << "num1 = " << num1 << "; num2 = " << num2 << "; num3 = " << num3 << endl;
  
BigInteger n4 = num2++;
BigInteger n5 = ++num2;
cout << "num2 = " << num2 << "; n4 = " << n4 << "; n5 = " << n5 << endl;
  
cout << "n4 + n5 = " << n4 + n5 << endl;
cout << "num2 + 999 = " << num2 + 999 << endl;
  
BigInteger n6(12), n7(15);
  
cout << "n6 == n7 " << (n6 == n7) << endl;
cout << "n6 < n7 " << (n6 < n7) << endl;
  
  
}

output

num1 = 5; num2 = 500; num3 = 1122334455
num2 = 502; n4 = 500; n5 = 502
n4 + n5 = 1002
num2 + 999 = 1501
n6 == n7 0
n6 < n7 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote