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

Can anyone please fix the code according to the given instruction on assignment

ID: 3890680 • Letter: C

Question

Can anyone please fix the code according to the given instruction on assignment paper:

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

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

--------------biginteger.h-----------------

#ifndef BIGINTEGER_H
#define BIGINTEGER_H
#include <iostream>
using namespace std;

typedef unsigned int uint;
typedef unsigned long long ull;
class BigInteger
{
public:
BigInteger(const char* input);
BigInteger();
~BigInteger();
BigInteger(const BigInteger& other);
BigInteger(const int & other);
BigInteger(const uint & other);
BigInteger& operator=(const BigInteger& other);
bool operator==(const BigInteger &cmp)const;
bool operator<(const BigInteger &cmp)const;
bool operator>(const BigInteger &cmp)const;
const BigInteger BigIntegerAbs(const BigInteger& numb)const;
friend ostream& operator << (ostream& os, const BigInteger& out);
friend const BigInteger operator+(const BigInteger &lhs, const BigInteger &rhs);
friend const BigInteger operator-(const BigInteger &lhs, const BigInteger &rhs);
friend const BigInteger operator*(const BigInteger &lhs, const BigInteger &rhs);
friend const BigInteger operator/(const BigInteger &lhs, const BigInteger &rhs);
protected:

private:
uint uIntArray[(100000 / 9 + 1)];
int tail;
int sig;
void printUnit(ostream& os, const uint& out, bool isFirst) const;
int getDigitNumber(const uint& digit) const;
};

#endif // BIGINTEGER_H

--------------biginteger.cpp-----------------

#include "..includeBigInteger.h"
#include <ctype.h>
#include <iostream>
#include <cstring>
using namespace std;
BigInteger::BigInteger()
{
sig = 1;
memset(uIntArray, 0, sizeof(uint)*(100000 / 9 + 1));
tail = 0;
}

BigInteger::BigInteger(const int& v)
{
sig = v >= 0 ? 1 : -1;
memset(uIntArray, 0, sizeof(uint) * (100000 / 9 + 1));
tail = 0;
uIntArray[0] = v > 0 ? v : -v;
}

BigInteger::BigInteger(const uint& v)
{
sig = 1;
memset(uIntArray, 0, sizeof(uint) * (100000 / 9 + 1));
tail = 0;
uIntArray[0] = v;
}
BigInteger::BigInteger(const char* input)
{
//ctor
//initialize variables
const char *digit = input;
sig = 1;//suppose the signature is '+'
tail = -1;

//test input validation
while(!isdigit(*digit) && *digit != '+' && *digit != '-' && *digit) ++digit;

if(0 == *digit) //if scan ends because of ''
{
//throw(IncorrectNumberException);
}
else if('+' == *digit || '-' == *digit)
{
sig = ('+' == *digit)? 1 : -1;
++digit;
}
else if(isdigit(*digit))//if scan ends because of isdigit
{
//do nothing
}
else//scan end because of illegal char
{
//throw(IncorrectNumberException);
}

//deal with heading '0'
while('0' == *digit) ++digit;

//clear the memory to 0
memset(uIntArray, 0, (100000 / 9 + 1) * sizeof(uint));

//copy 9 digits to every unit of array
int i, j;
int tmp;
for(i = strlen(digit) - 1, j = 0 ; i >= 0 ; -- i, ++j)
{
if(!isdigit(*digit))
{
//throw(incorrect input);
}
if(0 == (j % 9))
{
++tail;
tmp = 1u;
}
uIntArray[tail] = uIntArray[tail] + (digit[i] - '0') * tmp;
tmp *= 10;
}
// in case of -0
if(this->tail <= 0 && uIntArray[0] == 0) this->sig = 1, this->tail = 0;

// end of constructure
}

BigInteger::~BigInteger()
{
//dtor
}

const BigInteger BigInteger::BigIntegerAbs(const BigInteger& numb)const
{
BigInteger result(numb);
result.sig = 1;
return result;
}

BigInteger::BigInteger(const BigInteger& other)
{
//copy ctor
this->sig = other.sig;
this->tail = other.tail;
//in case of -0
if(this->tail <= 0 && this->uIntArray[0] == 0u) this->sig = 1, this->tail = 0;
memcpy(this->uIntArray, other.uIntArray, sizeof(uint) * (100000 / 9 + 1));
}

BigInteger& BigInteger::operator=(const BigInteger& rhs)
{
if (this == &rhs) return *this; // handle self assignment

//assignment operator
this->sig = rhs.sig;
this->tail = rhs.tail;
memcpy(this->uIntArray, rhs.uIntArray, sizeof(uint) * (100000 / 9 + 1));

//return the value
return *this;
}

int BigInteger::getDigitNumber(const uint& digit)const
{
uint tmp = digit;
int digitNumber = 0;
do
{
++digitNumber;
tmp /= 10;
}
while(tmp);
return digitNumber;
}

void BigInteger::printUnit(ostream& os, const uint& out, bool isFirst = false) const
{
if(isFirst)
{
os << out;
}
else
{
int digitNumber = getDigitNumber(out);
for(int i = 0; i < 9 - digitNumber; ++i)
os << 0;
os << out;
}
}


bool BigInteger::operator==(const BigInteger& cmp)const
{
if(this->sig != cmp.sig) return false;
else if(this->tail != cmp.tail) return false;
else
{
bool isEqual = true;
for(int i = this->tail; isEqual && i >= 0; -- i)
isEqual = (this->uIntArray[i] == cmp.uIntArray[i]);
return isEqual;
}
}

bool BigInteger::operator<(const BigInteger& cmp)const
{
if(this->sig < cmp.sig) return true;
else if(this->sig > cmp.sig) return false;
else
{
if(this->tail < cmp.tail) return true;
else if(this->tail > cmp.tail) return false;
else
{
for(int i = this->tail; i >= 0; --i)
if(this->uIntArray[i] > cmp.uIntArray[i]) return this->sig > 0? false : true;
else if(this->uIntArray[i] < cmp.uIntArray[i]) return this->sig > 0? true : false;
if(this->sig == cmp.sig)
return false;//fabs(*this) == fabs(cmp)
else
return this->sig < 0;
}
}
}

bool BigInteger::operator>(const BigInteger& cmp)const
{
if(this->sig > cmp.sig) return true;
else if(this->sig < cmp.sig) return false;
else
{
if(this->tail > cmp.tail) return true;
else if(this->tail < cmp.tail) return false;
else
{
for(int i = this->tail;i >= 0; -- i)
if(this->uIntArray[i] > cmp.uIntArray[i]) return this->sig > 0 ? true : false;
else if(this->uIntArray[i] < cmp.uIntArray[i]) return this->sig > 0? false : true;
if(this->sig == cmp.sig)
{
return false;
}
else
{
return this->sig > 0;
}
}
}
}

ostream& operator << (ostream& os, const BigInteger& out)
{
if(out.sig == -1) os << '-';
for(int i = out.tail; i >= 0; -- i)
{
out.printUnit(os, out.uIntArray[i], i == out.tail);
}
return os;
}
const BigInteger operator+(const BigInteger &lhs, const BigInteger &rhs)
{
BigInteger sum;
if(lhs.sig == rhs.sig)
{
sum.sig = lhs.sig;
ull carry = 0;
sum.tail = 0;
int len = lhs.tail > rhs.tail? lhs.tail : rhs.tail;
for(int &i = sum.tail; i <= len; ++ i)
{
carry = lhs.uIntArray[i] + rhs.uIntArray[i] + carry;
sum.uIntArray[i] = carry % 1000000000;
carry /= 1000000000;
}
if(carry > 0)
{
sum.uIntArray[sum.tail] = carry;
}
else
--sum.tail;
}
else//sig is not the same 1+(-2) or -2 + 1
{
if(lhs.sig > 0)
{
BigInteger trhs(rhs);
trhs.sig = 1;
sum = lhs - trhs;
while(sum.tail > 0 && sum.uIntArray[sum.tail] == 0)
{
-- sum.tail;
}
}
else
{
BigInteger tlhs(lhs);
tlhs.sig = 1;
sum = rhs - tlhs;
while(sum.tail > 0 && sum.uIntArray[sum.tail] == 0)
{
-- sum.tail;
}
}
}
if(sum.tail < 0) sum.tail = 0;
return sum;
}

const BigInteger operator-(const BigInteger &lhs, const BigInteger &rhs)
{
BigInteger result;//initialize result = 0
if(lhs.sig == rhs.sig)// 1-2 or (-1) - (-2)
{
if(lhs.sig > 0) // 1-2 or 2-1
{
if(lhs > rhs) // 2 - 1
{
uint carry = 0u;
for(int &i = result.tail; i <= lhs.tail ; ++ i)
{
if(lhs.uIntArray[i] >= rhs.uIntArray[i] + carry)
{
result.uIntArray[i] = lhs.uIntArray[i] - rhs.uIntArray[i] - carry;
carry = 0u;
}
else
{
result.uIntArray[i] = lhs.uIntArray[i] + 1000000000 - rhs.uIntArray[i] - carry;
carry = 1u;
}
}
}
else if(lhs < rhs)// 1 - 2=>-(2-1)
{
result = rhs - lhs;
result.sig = -1;
}
while(result.tail > 0 && result.uIntArray[result.tail] == 0)
{
-- result.tail;
}
}
else// -1-(-2) or -2-(-1) => 2-1 or 1-2
{
BigInteger tlhs(lhs), trhs(rhs);
tlhs.sig = 1;
trhs.sig = 1;
result = trhs - tlhs;
while(result.tail > 0 && result.uIntArray[result.tail] == 0)
{
-- result.tail;
}
}
}
else
{
BigInteger tmp(rhs);
tmp.sig = lhs.sig;
result = lhs + tmp;
}
return result;
}

const BigInteger operator*(const BigInteger &lhs, const BigInteger &rhs)
{
BigInteger result(0);
BigInteger tmp;
ull carry = 0u;
for(int i = 0; i <= lhs.tail; ++ i)
{
memset(tmp.uIntArray, 0, sizeof(uint) * (100000 / 9 + 1));
tmp.tail = i - 1;
tmp.sig = 1;
carry = 0u;
for(int j = 0; j <= rhs.tail; ++ j)
{
carry = (ull)lhs.uIntArray[i] * (ull)rhs.uIntArray[j] + carry;
tmp.uIntArray[++tmp.tail] = carry % 1000000000;
carry /= 1000000000;
}
if(carry > 0)
{
tmp.uIntArray[++tmp.tail] = carry;
}
result = result + tmp;
}
result.sig = lhs.sig * rhs.sig;
// in case of -0 * 1
//if(result.tail <= 0 && 0 == result.uIntArray[0]) result.sig = 1;
return result;
}
const BigInteger operator/(const BigInteger &lhs, const BigInteger &rhs)
{
BigInteger result(0), abslhs = lhs.BigIntegerAbs(lhs), absrhs = rhs.BigIntegerAbs(rhs);
result.tail = -1;
BigInteger tmp(0);
uint up, low, mid,t;
if(absrhs == 0)
{
//throw(dividbyzeroException)
}
else if(abslhs < absrhs)
{
result.sig = 1;
result.tail = 0;
}
else
{
for(int i = abslhs.tail; i >= 0; )
{
while(tmp < absrhs)
{
tmp = tmp * 1000000000 + abslhs.uIntArray[i--];
}
up = 1000000000;
low = 0u;
while(up > low)
{
mid = (up + low)>> 1;
if(absrhs * mid == tmp)
{
up = low = mid;
}
else if(absrhs * mid > tmp)
{
up = mid - 1;
}
else
{
low = mid + 1;
}
}
if(up * absrhs > tmp)
result.uIntArray[++result.tail] = up - 1;
else
result.uIntArray[++result.tail] = up;
tmp = tmp - absrhs * result.uIntArray[result.tail];
}
}
result.sig = rhs.sig * lhs.sig;
for(int i = result.tail; i > (result.tail >> 1); -- i)
{
t = result.uIntArray[i];
result.uIntArray[i] = result.uIntArray[result.tail - i];
result.uIntArray[result.tail - i] = t;
}
return result;
}

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