Specifically: 1) Your class should have 3 data members: a) a variable to store t
ID: 3556584 • Letter: S
Question
Specifically:
1) Your class should have 3 data members:
a) a variable to store the maximum size of the array that stores the large
integer
b) a variable to store actual length of the large integer
c) an int array to hold the large integer (this is obviously a pointer, since the
array will be dynamic).
2) Your class should have the following functions:
a) An appropriate constructor
b) A function to overload the + operator to support addition of LargeInt values
c) A function to overload the assignment operator =
d) A function to overload the comparison operator = =
e) A function to overload the << operator
f) A function to overload the >> operator
For this project LargeInt values can only be positive.
Test your class with the program on the next page.
NOTE: Check your GRADING CRITERIA FOR PROJECTS posted on Blackboard before
turning in your programs.
This is the mian given:
#include "LargeInt.h"
using namespace std;
int main ()
{
LargeInt x, y, z;
cout << "Enter an integer of your choice : ";
cin >> x;
cout<< "Enter another integer of your choice: ";
cin >> y;
if ( x == y )
cout<<" The two numbers entered are equal";
else
cout<<" The two numbers entered are different";
z = x + y;
cout<<" The sum of "<
system("pause");
}
Explanation / Answer
// LargeInt.h
#include <iostream>
#include <string>
using namespace std;
class LargeInt
{
public:
LargeInt();
LargeInt(int maxsz);
LargeInt(const LargeInt& v);
~LargeInt();
LargeInt& operator=(const LargeInt& v);
bool operator==(const LargeInt& v);
LargeInt operator+(const LargeInt& v);
friend ostream& operator<<(ostream& os, const LargeInt& v);
friend istream& operator>>(istream& is, LargeInt& v);
private:
int maximumSize;
int length;
int* value;
};
LargeInt::LargeInt()
{
maximumSize = 100;
value = new int[100];
value[0] = 0;
length = 1;
}
LargeInt::LargeInt(int maxsz)
{
maximumSize = maxsz;
value = new int[maxsz];
value[0] = 0;
length = 1;
}
LargeInt::LargeInt(const LargeInt& v)
{
value = NULL;
(*this) = v;
}
LargeInt::~LargeInt()
{
delete[] value;
}
LargeInt& LargeInt::operator=(const LargeInt& v)
{
delete[] value;
maximumSize = v.maximumSize;
length = v.length;
value = new int[maximumSize];
for (int i = 0; i < length; i++)
value[i] = v.value[i];
return *this;
}
bool LargeInt::operator==(const LargeInt& v)
{
if (length != v.length)
return false;
for (int i = 0; i < length; i++)
{
if (value[i] != v.value[i])
return false;
}
return true;
}
LargeInt LargeInt::operator+(const LargeInt& v)
{
int maxsize = maximumSize;
if (maxsize < length)
maxsize = length + 10;
if (maxsize < v.length)
maxsize = v.length + 10;
LargeInt r(maxsize);
r.length = 0;
int i = 0, j = 0;
int carry = 0;
while (i < length || j < v.length || carry > 0)
{
int temp = carry;
if (i < length)
{
temp += value[i];
i++;
}
if (j < v.length)
{
temp += v.value[j];
j++;
}
r.value[r.length] = temp % 10;
r.length += 1;
carry = temp / 10;
}
return r;
}
ostream& operator<<(ostream& os, const LargeInt& v)
{
for (int i = v.length - 1; i >= 0; i--)
os << v.value[i];
return os;
}
istream& operator>>(istream& is, LargeInt& v)
{
string str;
is >> str;
delete[] v.value;
v.maximumSize = str.size() + 10;
v.value = new int[v.maximumSize];
v.length = str.size();
for (int i = 0; i < str.size(); i++)
v.value[v.length - i - 1] = str[i] - '0';
return is;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.