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

Write Code In C++ Motivation: The C++ long int can only take up to 64 bits of in

ID: 3806976 • Letter: W

Question

Write Code In C++

Motivation:

The C++ long int can only take up to 64 bits of information, which may not   be enough for some scientific computation. In this project, we will design    and implement a class named UnsignedLargeInt which will overcome these problems.

The requirement for this project is to use UnsignedLargeInt at almost all situations when int is used.

2           Requirements:

As int, UnsignedLargeInt can be zero or positive. Unlike int, Unsigned- LargeInt has ”infinite” precision and can hold as many digits (char) as pos- sible. The digits are represented by char, and are stored in a dynamically allocated array.

The size of the array can be larger than the actual number of digits in the number. For example, to add two UnsignedLargeInt a1 and a2, the resulted UnsignedLargeInt can have max(a1.len,a2.len) + 1 bytes allocated as the digit array, which is long enough to handle any overflow.

UnsignedLargeInt should have the following operators:

2.1         Constructors and destructor

There are 3 constructors:

default constructor will set the UnsignedLargeInt to zero

copy constructor to take another UnsignedLargeInt as the argument, because we are working with a dynamically allocated array, you must perform deep copy.

constructor to take a unsigned long long as the argument, and transfer this unsigned long long as digits (char) and store the digits in the array. The argument can be positive or zero, so your UnsignedLargeInt must be able to reflect the sign of the argument.

Destructor: C++ allows only one version of destructor, and it should delete the char array.

2.2    +, * operators (bonus for *)

These operators should perform exactly as their counter parts in int, i.e.

unsigned long long a = 1000000; UnsignedLargeInt i1(a); UnsignedLargeInt i2(100); UnsignedLargeInt i3;

i3 = i1 + i2;

cout << i3; // should output 1,000,100;

Each of these operators should take two ”const UnsignedLargeInt&” as arguments and return a UnsignedLargeInt. These operators should be designed and implemented as friend operators. It has no requirement to overload these operators. For example:

unsigned long long a = 1000000; UnsignedLargeInt i1(a); UnsignedLargeInt i2;

i2 = i1 + 100; //compiler will transfer 100 to a UnsignedLargeInt,

//so we do not have a + operator to take int as argument. cout << i2; // should output 1,000,100;

2.3    +=, *= operators (bonus for *=)

These operators should perform exactly as their counter parts in int, i.e.

unsigned long long a = 1000000; UnsignedLargeInt i1(a); UnsignedLargeInt i2(100);

i1 += i2;

cout << i1; // should output 1,000,100;

Each of these operators should take a ”const UnsignedLargeInt&” as ar- gument and return ”UnsignedLargeInt&”. It has no requirement to overload these operators.

2.4      boolean operators !, !=, ==, <, >, <=, >=

These operators should perform exactly as their counter parts in int, i.e.

unsigned long long a = 1000000; UnsignedLargeInt i1(a); UnsignedLargeInt i2(100);

if (i2 < i1) cout << "i2 is smaller" << endl; else cout << "i1 is smaller" << endl;

Each of these operator should take two ”const UnsignedLargeInt&” as arguments (except the unary operator ! which will take no argument) and return ”bool”. These operators (except !) should be designed and imple- mented as friend operators. It has no requirement to overload these opera- tors.

2.5            incremental operators ++

These operators should perform exactly as their counter parts in int, and should have two versions (pre-increment and post-increment), i.e.

unsigned long long a = 1000000; UnsignedLargeInt i1(a);

i1++;

cout << i1; //should output 1,000,001 cout << ++i1; //should output 1,000,002

2.6            << operator

Output the UnsignedLargeInt in a nice format, i.e.

unsigned long long a = 10000000000000000; UnsignedLargeInt i1(a);

cout << i1;

//The output should look like: 10,000,000,000,000,000

This operator should be friend (just like what we did in the previous project).

2.7            Other requirements

No need to overload the ”>>” operator. Comment you code! Indent your code!

3               Files to turn in:

You need to turn in four files: makefile, main.C, UnsignedLargeInt.C, Un- signedLargeInt.h. This time, I will not provide a UnsignedLargeInt.h, you need to design your own.

You should have a main.C that can test all your operators.

4            Grading

The total points is 14, you will get full credit if all constructors, destructor     and operators work correctly and there is no major bug in your code. You     will get 6 bonus points for * and *= operators.

The grade will be based on the implementation of individual operators. However, 2 points are reserved for the implementation of the main function.

No late turn in is acceptable, any late turn in will be given 0 point.

Your code must be compilable on Linux/Unix, any code that cannot be compiled by g++ will automatically get ZERO point.   This suggests that you should implement and test the operators one by one, and do not turn in the operators that can not be compiled.

Explanation / Answer

Given below are the needed files for the question. Please do rate the answer if it helped. Thank you very much.

UnsignedLargeInt.h

#ifndef UnsignedLargeInt_h
#define UnsignedLargeInt_h
#include <iostream>
using namespace std;
class UnsignedLargeInt
{
private:
char* digits;
void add(char *n1, char *n2 ,char* &answer);
public:
UnsignedLargeInt();
UnsignedLargeInt(const UnsignedLargeInt &other);
UnsignedLargeInt(const unsigned long long num);
~UnsignedLargeInt();
UnsignedLargeInt& operator = (const UnsignedLargeInt &other);
UnsignedLargeInt operator +(const UnsignedLargeInt &other);
UnsignedLargeInt operator *(const UnsignedLargeInt &other);
UnsignedLargeInt operator +=(const UnsignedLargeInt &other);
UnsignedLargeInt operator *=(const UnsignedLargeInt &other);
bool operator !();
bool operator !=(const UnsignedLargeInt &other);
bool operator ==(const UnsignedLargeInt &other);
bool operator <(const UnsignedLargeInt &other);
bool operator >(const UnsignedLargeInt &other);
bool operator <=(const UnsignedLargeInt &other);
bool operator >=(const UnsignedLargeInt &other);
UnsignedLargeInt operator ++(int);
UnsignedLargeInt operator ++();
  
friend ostream& operator <<(ostream &out, const UnsignedLargeInt &num);
  
};

#endif /* UnsignedLargeInt_h */

UnsignedLargeInt.cpp


#include "UnsignedLargeInt.h"
#include <cstring>
#include <iostream>
using namespace std;
UnsignedLargeInt::UnsignedLargeInt()
{
digits = new char[2];
strcpy(digits,"0");
}
UnsignedLargeInt::UnsignedLargeInt(const UnsignedLargeInt &other)
{
digits = new char[strlen(other.digits) + 1];
strcpy(digits, other.digits);

}
UnsignedLargeInt& UnsignedLargeInt::operator = (const UnsignedLargeInt &other)
{
delete []digits;
digits = new char[strlen(other.digits) + 1];
strcpy(digits, other.digits);
return *this;
}
UnsignedLargeInt::UnsignedLargeInt(const unsigned long long num)
{
const char *str = to_string(num).c_str();
digits = new char[strlen(str) + 1];
strcpy(digits, str);

}
UnsignedLargeInt::~UnsignedLargeInt()
{
delete []digits;
}
UnsignedLargeInt UnsignedLargeInt::operator +(const UnsignedLargeInt &other)
{
UnsignedLargeInt result;
add(digits, other.digits, result.digits);
return result;
}
void UnsignedLargeInt::add(char *n1, char *n2, char* &answer)
{
int len1 = strlen(n1);
int len2 = strlen(n2);
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 = n1[idx1--] - '0';
else
d1 = 0;

if(idx2 >= 0)
d2 = n2[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';

answer = new char[strlen(res + idx3 + 1) + 1];
strcpy(answer, res + idx3 + 1);
delete []res;

}
UnsignedLargeInt UnsignedLargeInt::operator *(const UnsignedLargeInt &other)
{
UnsignedLargeInt result;
int reqLen = strlen(digits) + 2;
char* prod;
int len1 = strlen(digits);
int len2 = strlen(other.digits);
int d1 , d2;
int p, carry = 0, m;
//perform high school multiplication method wehre multiple rows are added up
for(int i = len2-1, row = 0; i >= 0; i--, row++)
{
carry = 0;
d2 = other.digits[i] - '0';
reqLen = len1 + 1 + row + 1; // 1 for overflow and 1 for null terminator
prod = new char[reqLen];
prod[reqLen] = '';
for(int k = row; k > 0; k--)
prod[reqLen - k] = '0';
m = reqLen - 1 - row ;
for(int j = len1-1 ; j >= 0; j--, m --)
{
d1 = digits[j] - '0';
p = d1 * d2 + carry;

carry = p / 10;
p %= 10;
prod[m] = p + '0';
}
if(carry != 0)
prod[m--] = '0' + carry;
char *temp;
//cout << "d2 = " << d2 <<", result = "<<result.digits << endl;
//cout << "prod = " << (prod+m+1) << endl;
add(result.digits, prod + m + 1, temp);
//cout << "temp = " << temp << endl;
delete []result.digits;
delete []prod;
result.digits = temp;
}
return result;
}
UnsignedLargeInt UnsignedLargeInt::operator +=(const UnsignedLargeInt &other)
{
*this = (*this) + other;
return *this;
}
UnsignedLargeInt UnsignedLargeInt::operator *=(const UnsignedLargeInt &other)
{
*this = (*this) * other;
return *this;
}
bool UnsignedLargeInt::operator !()
{
if(strcmp(digits, "0") == 0)
return true;
else
return false;
}
bool UnsignedLargeInt::operator !=(const UnsignedLargeInt &other)
{
return strcmp(digits, other.digits) != 0;
}
bool UnsignedLargeInt::operator ==(const UnsignedLargeInt &other)
{
return strcmp(digits, other.digits) == 0;
}
bool UnsignedLargeInt::operator <(const UnsignedLargeInt &other)
{
return strcmp(digits, other.digits) < 0;

}
bool UnsignedLargeInt::operator >(const UnsignedLargeInt &other)
{
return strcmp(digits, other.digits) > 0;

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

}
bool UnsignedLargeInt::operator >=(const UnsignedLargeInt &other)
{
return strcmp(digits, other.digits) >= 0;

}
UnsignedLargeInt UnsignedLargeInt::operator ++(int)
{
UnsignedLargeInt oldValue = *this;
*this = *this + 1;
return oldValue;
}
UnsignedLargeInt UnsignedLargeInt::operator ++()
{
*this = *this + 1;
return *this;
}
ostream& operator << (ostream &out, const UnsignedLargeInt &num)
{
int len = strlen(num.digits);
int rem = len % 3;
char buf[4];
if(len <= 3)
out <<num.digits;
else
{
if(rem == 0)
rem = 3;
strncpy(buf,num.digits,rem);
cout << buf;
for(int i = rem; i < len; i+=3)
{
strncpy(buf,num.digits + i, 3);
cout << "," << buf;
}
}
return out;
}

main.cpp


#include "UnsignedLargeInt.h"
#include <iostream>
using namespace std;
int main()
{
UnsignedLargeInt num1;
UnsignedLargeInt num2(100);
UnsignedLargeInt num3(123456789);
  
cout << "num1 = " << num1 << "; num2 = " << num2 << "; num3 = " << num3 << endl;
  
UnsignedLargeInt n4 = num2++;
UnsignedLargeInt n5 = ++num2;
cout << "num2 = " << num2 << "; n4 = " << n4 << "; n5 = " << n5 << endl;
  
cout << "n4 + n5 = " << n4 + n5 << endl;
cout << "num2 + 999 = " << num2 + 999 << endl;
  
UnsignedLargeInt n6(12), n7(15);
cout << "n6 = " << n6 << "; n7 = " << n7 << "; n6*n7 = " << n6*n7 << endl;
cout << "num3 * num3 = " << num3 * num3 << endl;
cout << "n6 == n7 " << (n6 == n7) << endl;
cout << "n6 != n7 " << (n6 != n7) << endl;
cout << "n6 < n7 " << (n6 < n7) << endl;
cout << "n6 > n7 " << (n6 > n7) << endl;
cout << "n6 <= n7 " << (n6 <= n7) << endl;
cout << "n6 >= n7 " << (n6 >= n7) << endl;
  
  
  
  
}

output

num1 = 0; num2 = 100; num3 = 123,456,789
num2 = 102; n4 = 100; n5 = 102
n4 + n5 = 202
num2 + 999 = 1,101
n6 = 12; n7 = 15; n6*n7 = 180
num3 * num3 = 15,241,578,750,190,521
n6 == n7 0
n6 != n7 1
n6 < n7 1
n6 > n7 0
n6 <= n7 1
n6 >= n7 0

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