A machine with 32-bit integers can represent integers in the range of approximately 2 billion to +2 billion. This fixed-size restriction is rarely troublesome, but there are applications in which we would like to be able to use a much wider range of integers, such as factorial. This is what C++ was built to do, namely, create powerful new data types.
Create class HugeInt that is capable of representing positive (non-negative) 30-digit decimal values (this will be able to represent values from 0 to 1030). The 30-digit number can be represented internally as array of integer whereby each digit takes values from zero to nine. The class must have conversion constructor that convert int or char value to HugeInt. Then overload the following operators
a. Stream insertion (<<) and extraction (>>) operators
b. Arithmetic operators (+,-,/,*). Each operation must also support operation with mix operands (integer and HugeInt objects)
c. Relational operators (==,<=,>=,!=).Each operation must also support operation with mix operands (integer and HugeInt objects)
d. Define a function factorial(X) that calculate factorial of X, whereby X is a HugeInt object.*

The program must follow good programming practices such as information hiding, principle of least privilege, etc.
Your program must compile successfully for the following driver program.

``````int main()
{
HugeInt a; //HugeInt object initialized to zero
HugeInt b(12345);
HugeInt c(“100200101002005550”);
HugeInt result;
cin >> a;
result = a+b;
cout  <<  a << “+” << b << “ = “ << result << endl;
result = c – b;
cout << result << endl;
result = c / b;
cout << result << endl;
result = c * b;
cout << result << endl;
if (a == b)
cout << “Equal” << endl;
else
cout << “Not Equal” << endl;

if (a >= b)
cout << “Greater” << endl;
else
cout << “Less” << endl;

if (a <= b)
cout << “Less” << endl;
else
cout << “Greater” << endl;

if (a != b)
cout << “Not Equal” << endl;
else
cout << “Equal” << endl;

factorial(b); //will output the result of b factorial

return 0;
}``````

Does that mean you want us to write that class for you? If not then you need to post the class that you have written. If it does mean that then sorry but you're out of luck here because we don't do …

## All 3 Replies

Does that mean you want us to write that class for you? If not then you need to post the class that you have written. If it does mean that then sorry but you're out of luck here because we don't do your homework for you.

elp me to do information hiding and make this program more friendly user

``````#include<iostream>
#include<string>
#include<cmath>
using namespace std;

class HugeInt {

friend ostream& operator<<(ostream& os,const HugeInt& b);
friend istream& operator>>(istream& is,HugeInt& b);

private:
friend void repair(HugeInt&);
friend void factorial(HugeInt&);
public:
int data[30];
HugeInt();
HugeInt(int input);
HugeInt(char input[]);
HugeInt operator+(HugeInt& b);
HugeInt operator-(HugeInt& b);
HugeInt operator/(HugeInt& b);
HugeInt operator*(HugeInt& b);
HugeInt operator+(int);
HugeInt operator-(int);
HugeInt operator/(int);
HugeInt operator*(int);

bool operator==(HugeInt&);
bool operator<=(HugeInt&);
bool operator>=(HugeInt&);
bool operator!=(HugeInt&);
bool operator==(int&);
bool operator<=(int&);
bool operator>=(int&);
bool operator!=(int&);

};

HugeInt::HugeInt() {
for (int x=0; x<30; x++)
data[x]=0;
}

HugeInt::HugeInt(int input) {
for (int z=29; z>=0;z--) {
data[z] = input % 10;                   //modulus
input /= 10;
}
}

HugeInt::HugeInt(char input[]) {
int x = (30-strlen(input)),z=0;
for (int y=0; y<30; y++) {
if (y>=x) {
data[y] = (input[z]-48);             //48 is a value for '0'
z++;
}
else
data[y] = 0;
}
}

void repair(HugeInt& temp) {
int temporary=0;
for (int k=29;k>=0;k--) {
if (temp.data[k] > 9) {
temporary = temp.data[k]/10;
temp.data[k] %= 10;

}
while (temporary%10 != 0) {

temp.data[k-1] += temporary %10;
temporary/=10; //to make temporary = 0
}
}

}

HugeInt HugeInt::operator+ (HugeInt& b) {
HugeInt temp;
for (int x=0; x<30; x++)
temp.data[x] = data[x] + b.data[x];
repair(temp);
return temp;
}

HugeInt HugeInt::operator- (HugeInt& c) {
HugeInt temp;
int check=0;
for (int x=0; x<30; x++) {
temp.data[x] = data[x] - c.data[x];
}

for (int k=0;k<30;k++) {
if (temp.data[k] > 0 && check==0)
check=1;
else if (temp.data[k] < 0 && check>0) {
temp.data[k-1] -= 1;
temp.data[k] += 10;
}
else if (temp.data[k] < 0 && check==0)
check=-1;
else if (temp.data[k] < 0 && check<0)
temp.data[k] = -temp.data[k];
}

return temp;
}

HugeInt HugeInt::operator/ (HugeInt& c) {

HugeInt temp;
int temp_num=0;

for (int x=29,j=0; x>=0; x--,j++)
temp_num += c.data[x] * pow(10,j);

for (x=29,j=0; x>=0; x--,j++) {
for (int save=1; /*( (static_cast<int>(data[x] * pow(10,j) / temp_num)) % (int)pow(10,save) != 0 )*/ ; save++)
{

if ((data[x] * pow(10,j) / temp_num) > 2000000000)
{
if ( (data[x] * pow( 10,(j-(save-1)) ) / temp_num) <= 2000000000 )
temp.data[29-(save-1)] += static_cast<int>(data[x] * pow( 10,(j-(save-1)) ) / temp_num) % 10 ;

}
else {
temp.data[29-(save-1)] += static_cast<int>(data[x] * pow(10,j) / temp_num) / (int)pow(10,save-1) % 10;
}

if ( (int)((data[x] * pow(10,j) / temp_num) / pow(10,(save))) == 0 )
break;
}

}
return temp;

}

HugeInt HugeInt::operator* (HugeInt& c) {
HugeInt temp;
for (int x=29; x>=0; x--) {
for (int z=29; z>=0 ; z--) {
temp.data[z-(29-x)] += data[x] * c.data[z];
}
}
repair(temp);
return temp;
}

HugeInt HugeInt::operator+ (int b) {
HugeInt temp;
for (int x=29; x>=0; x--) {
temp.data[x] = data[x] + b%10;
b/=10;
}
repair(temp);
return temp;
}

HugeInt HugeInt::operator- (int c) {
HugeInt temp;
int check=0;
for (int x=29; x>=0; x--) {
temp.data[x] = data[x] - c%10;
c/=10;
}

for (int k=0;k<30;k++) {
if (temp.data[k] > 0 && check==0)
check=1;
else if (temp.data[k] < 0 && check>0) {
temp.data[k-1] -= 1;
temp.data[k] += 10;
}
else if (temp.data[k] < 0 && check==0)
check=-1;
else if (temp.data[k] < 0 && check<0)
temp.data[k] = -temp.data[k];
}

return temp;
}

HugeInt HugeInt::operator* (int c) {
HugeInt temp;
for (int x=29; x>=0; x--)
temp.data[x] = data[x] * c;
repair(temp);
return temp;
}

HugeInt HugeInt::operator/ (int c) {

HugeInt temp;
int temp_num=c;

for (int x=29,j=0; x>=0; x--,j++) {
for (int save=1; /*( (static_cast<int>(data[x] * pow(10,j) / temp_num)) % (int)pow(10,save) != 0 )*/ ; save++)
{

if ((data[x] * pow(10,j) / temp_num) > 2000000000)
{
if ( (data[x] * pow( 10,(j-(save-1)) ) / temp_num) <= 2000000000 )
temp.data[29-(save-1)] += static_cast<int>(data[x] * pow( 10,(j-(save-1)) ) / temp_num) % 10 ;

}
else {
temp.data[29-(save-1)] += static_cast<int>(data[x] * pow(10,j) / temp_num) / (int)pow(10,save-1) % 10;
}

if ( (int)((data[x] * pow(10,j) / temp_num) / pow(10,(save))) == 0 )
break;
}

}
return temp;

}

bool HugeInt::operator== (HugeInt& object)
{
for(int i=0;i<30;i++)
if(data!=object.data)
return false;

return true;

}

bool HugeInt::operator<= (HugeInt& object)
{
for(int i=0;i<30;i++)
{
if(data > object.data)
return false;
}

return true;

}

bool HugeInt::operator>= (HugeInt& object)
{
for(int i=0;i<30;i++)
{
if(data < object.data)
return false;
}

return true;

}

bool HugeInt::operator!= (HugeInt& object)
{
for(int i=0;i<30;i++)
{
if(data != object.data)
return false;
}

return true;
}

bool HugeInt::operator== (int& variable)
{
for(int i=29;i<=0;i--)
{
if(data!=variable%10)
{
return false;
}

variable /=10;
}
return true;
}

bool HugeInt::operator<= (int& variable)
{
for(int i=29;i<=0;i--)
{
if(data>variable%10)
return false;

variable/=10;
}

return true;
}

bool HugeInt::operator>= (int& variable)
{
for(int i=29;i<=0;i--)
{
if(data<variable%10)
return false;

variable/=10;
}
return true;

}

bool HugeInt::operator!= (int& variable)
{
for(int i=29;i<=0;i--)
{
if(data==variable%10)
return false;

variable/=10;
}

return true;
}

/*
void factorial(HugeInt& b) {

int factor[2][100000]={0},print=0,toolong=0;
HugeInt temp;
HugeInt check(1);

for (int x=0; x<30; x++) { //
temp.data[x] = b.data[x];
factor[1][99970+x] = b.data[x]; //99,970 is from 100,000 - 30
}
//  if (b <= check) {//if (b == 0)
temp.data[29] = 1;
factor[1][99999] = 1;
}

//  while (temp != check) {
temp.data[29] -= 1; //temp.data is reduced by 1
for (int k=29;k>=0;k--) { //repair temp.data
if (temp.data[k] < 0) {
temp.data[k-1] -= 1;
temp.data[k] += 10;
}
} //end repair temp.data

for (int x=99999; x>=0; x--) {

for (int z=29; z>=0 ; z--)
factor[0][(99970+z)-(99999-x)] += factor[1][x] * temp.data[z];
}

for (int a=0; a<100000; a++) {
factor[1][a] = factor[0][a];
factor[0][a] = 0;
}

int temporary=0,helper;
for (k=99999;k>=0;k--) {
if (factor[1][k] > 9) {
temporary = factor[1][k]/10;
factor[1][k] %= 10;
helper=0;
}
while (temporary%10 != 0) {
if (k-helper < 0) {
cout << "The result of the factorial is too long!";
toolong=1;
break;
}
helper++;
factor[1][k-helper] += temporary %10;
temporary/=10;
}
if (toolong == 1)
break;
}
}

cout << endl;
for (x=0;x<100000;x++) {
if (toolong == 1)
break;
if (print!=0)
cout << factor[1][x];
else
if (factor[1][x]>0) {
cout << factor[1][x];
print++;
}
}
cout << endl;
}
*/
int main()
{

HugeInt a; //HugeInt object initialized to zero
HugeInt b(12345);
HugeInt c("100200101002005550");
HugeInt result;

cin >> a;

result = a+b;
cout  <<  a << "+" << b << " = " << result << endl;

result = c - b;
cout << result << endl;

result = c / b;
cout << result << endl;

result = c * b;
cout << result << endl;

if (a == b)
cout << "Equal" << endl;
else
cout << "Not Equal" << endl;

if (a >= b)
cout << "Greater" << endl;
else
cout << "Less" << endl;

if (a <= b)
cout << "Less" << endl;
else
cout << "Greater" << endl;

if (a != b)
cout << "Not Equal" << endl;
else
cout << "Equal" << endl;

//  factorial(b); //will output the result of b factorial

return 0;
}

/*istream& operator>>(istream& input, HugeInt& object)
{

int x;
input>>x;
for (int y=0,z=29; y<30; y++,z--) {
object.data[z] = x % 10;
x /= 10;
}
return input;

}

ostream& operator<<(ostream& output, HugeInt& object)
{
for (int x=0;x<30;x++)
output<<object.data[x];

return output;
}

*/

ostream& operator<<(ostream& os,const HugeInt& b) { // operator << overload
int check=0;
for (int x=0;x<30;x++) {
if (check!=0 || x==29)
os << b.data[x];
else
if (b.data[x]!=0) {
os << b.data[x];
check++;
}
}
return os;
}

istream& operator>>(istream& is,HugeInt& b) { // operator >> overload
char uinput[35];
cout << "Please enter positive integer value(range from 0 to 10^30) :\n>> ";
is >> uinput;
int x = (30-strlen(uinput));
for (int y=0,z=0; y<30; y++) {
if (uinput[z] < 48 || uinput[z] > 57) {
cout << "\nInvalid data!\nPlease enter positive integer value only :\n>> ";
is >> uinput;
x = (30-strlen(uinput));
y=-1;
z=0;
}
else if (strlen(uinput)>30) {
cout << "\nInvalid data(exceed 30 digits)!\nPlease re-enter value :\n>> ";
is >> uinput;
x = (30-strlen(uinput));
y=-1;
z=0;
}
else if (y>=x) {
b.data[y] = (uinput[z]-48);
z++;
}
else
b.data[y] = 0;
}
return is;
}
``````

Hugeint can be better represented using a link list than an array of 30 integers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.