An array can be used to store large integers one digit at a time. For example, t
ID: 3650106 • Letter: A
Question
An array can be used to store large integers one digit at a time. For example, the integer 1234 could be stored in the array a by setting a[0] to 11, a[1] to 2, a[2] to 3, and a[3] to 4. However, for this project, you might find it easier to store the digits backward, that is, place 4 in [0], place 3 in a[1], place 2 in a[2], and 1 in a[3];Design, implement and test a class in which each object is a large integer with each digit stored in a
separate element of a character array. You'll also need a private member variable to keep track of
the sign of the integer (perhaps a Boolean variable), and another private member variable to keep
the Size (or length) of the integer. The number of digits may grow as the program runs, so the class
must use a character pointer (dynamic character array) as a member variable, which stores its value
on the dynamic memory freestore (heap). You should also implement other appropriate operators
for this class. More specifically. your class should provide default constructor, deep-copy copy
constructor, overloaded constructor(s), destructor, deep-copy assignment operator.
overloaded arithmetic operators, comparison operators, input and output operators. You
should also have a nice driver code to test all implemented member functions and overloaded
operators.
Explanation / Answer
please rate - thanks
should have everything but the dynamic allocation, but you haven't gotten an answer, so it should at least should get you started
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
class LargeInt
{
friend ostream &operator<<( ostream &, const LargeInt & );
public:
LargeInt( long = 0 );
LargeInt( const char * );
LargeInt operator+( const LargeInt & ) const;
LargeInt operator+( int ) const;
LargeInt operator+( const char * ) const;
LargeInt operator/( const LargeInt &op2 ) const;
LargeInt operator*( const LargeInt &op2 ) const;
LargeInt operator-( const LargeInt & ) const;
bool operator==(const LargeInt&)const;
bool operator==(int)const;
bool operator==(long)const;
bool operator==(const char *)const;
bool operator!=(const LargeInt&)const;
bool operator!=(int)const;
bool operator!=(long)const;
bool operator!=(const char *)const;
bool operator<(const LargeInt&)const;
bool operator<(int)const;
bool operator<(long)const;
bool operator<(const char *)const;
bool operator<=(const LargeInt&)const;
bool operator<=(int)const;
bool operator<=(long)const;
bool operator<=(const char *)const;
bool operator>(const LargeInt&)const;
bool operator>(int)const;
bool operator>(long)const;
bool operator>(const char *)const;
bool operator>=(const LargeInt&)const;
bool operator>=(int)const;
bool operator>=(long)const;
bool operator>=(const char *)const;
int getLength() const;
private:
short integer[ 30 ];
};
LargeInt::LargeInt( long value )
{
for ( int i = 0; i <= 29; i++ )
integer[ i ] = 0;
for ( int j = 29; value != 0 && j >= 0; j-- )
{integer[ j ] = value % 10;
value /= 10;
}
}
LargeInt::LargeInt( const char *string )
{
for ( int i = 0; i <= 29; i++ )
integer[ i ] = 0;
int length = strlen( string );
for ( int j = 30 - length, k = 0; j <= 29; j++, k++ )
if ( isdigit( string[ k ] ) )
integer[ j ] = string[ k ] - '0';
}
int LargeInt::getLength() const
{
int i;
for (i = 0; i <= 29; i++ )
if ( integer[ i ] != 0 )
break;
return 30 - i;
}
LargeInt LargeInt::operator/( const LargeInt &op2 ) const
{int i,j,digits;
LargeInt tmp(*this),val("0"),ans,temp("0"),prod;
digits=(*this).getLength();
for(i=30-digits;i<=29;i++)
{val=val*10;
val.integer[29]=tmp.integer[i];
if(op2<=val)
{for(j=0;j<10;j++ )
{prod=op2*LargeInt(j+1);
if(prod>val)
break;
}
temp.integer[i]=j;
ans=op2*LargeInt(j);
}
val=val-ans;
}
return temp;
}
LargeInt LargeInt::operator*( const LargeInt &op2 ) const
{ LargeInt temp;
int i,j,k,tmp;
for(i=0; i<30; i++)
temp.integer[ i ] =0;
for (int i=29; i>=1; i--)
{ k=i;
for (j=29; j>=1; j--)
{tmp = integer[ j ] * op2.integer[i];
temp.integer[k] =temp.integer[k]+tmp;
temp.integer[k-1] =temp.integer[k-1]+ temp.integer[k]/10;
temp.integer[k] %=10;
k--;
if(k==0)
j=0;
}
}
return temp;
}
LargeInt LargeInt::operator+( const LargeInt &op2 ) const
{
LargeInt temp;
int carry = 0;
for ( int i = 29; i >= 0; i-- )
{
temp.integer[ i ] =
integer[ i ] + op2.integer[ i ] + carry;
if ( temp.integer[ i ] > 9 )
{
temp.integer[ i ] %= 10;
carry = 1;
}
else
carry = 0;
}
return temp;
}
LargeInt LargeInt::operator+( int op2 ) const
{
return *this + LargeInt( op2 );
}
LargeInt LargeInt::operator+( const char *op2 ) const
{
return *this + LargeInt( op2 );
}
LargeInt LargeInt::operator-( const LargeInt &op2 ) const
{
LargeInt temp;
int borrow = 0;
for( int i = 29;i >= 0;i--)
if(borrow==0)
{if(integer[i]>=op2.integer[i])
temp.integer[i]=integer[i]-op2.integer[i];
else
{borrow=1;
temp.integer[i]=integer[i]+10-op2.integer[i];
}
}
else
{borrow=0;
if(integer[i]-1>=op2.integer[i])
temp.integer[i]=integer[i]-1-op2.integer[i];
else
{borrow=1;
temp.integer[i]=integer[i]-1+10-op2.integer[i];
}
}
return temp;
}
bool LargeInt::operator==( const LargeInt &op2 ) const
{
for(int i=0;i<=29;i++)
{
if(integer[i]!= op2.integer[i])
return false;
}
return true;
}
bool LargeInt::operator==( int op2 ) const
{
return *this == LargeInt(op2);
}
bool LargeInt::operator==( long op2 ) const
{
return *this == LargeInt(op2);
}
bool LargeInt::operator==( const char *op2 ) const
{
return *this == LargeInt(op2);
}
bool LargeInt::operator!=(const LargeInt &op2) const
{
int maxLen = (getLength() > op2.getLength()) ? getLength() : op2.getLength();
for(int i=29;i>=30-maxLen;i--)
{
if(integer[i] != op2.integer[i])
return true;
}
return false;
}
bool LargeInt::operator!=( int op2 ) const
{
return *this != LargeInt(op2);
}
bool LargeInt::operator!=( long op2 ) const
{
return *this != LargeInt(op2);
}
bool LargeInt::operator!=( const char *op2 ) const
{
return *this != LargeInt(op2);
}
bool LargeInt::operator<(const LargeInt &op2) const
{
for(int i=0;i<=29;i++)
{
if(integer[i] < op2.integer[i])
return true;
else if(integer[i] > op2.integer[i])
return false;
}
return false;
}
bool LargeInt::operator<( int op2 ) const
{
return *this < LargeInt(op2);
}
bool LargeInt::operator<( long op2 ) const
{
return *this < LargeInt(op2);
}
bool LargeInt::operator<( const char *op2 ) const
{
return *this < LargeInt(op2);
}
bool LargeInt::operator<=(const LargeInt &op2) const
{
if (*this == op2 || *this < op2)
return true;
return false;
}
bool LargeInt::operator<=( int op2 ) const
{
return *this <= LargeInt(op2);
}
bool LargeInt::operator<=( long op2 ) const
{
return *this <= LargeInt(op2);
}
bool LargeInt::operator<=( const char *op2 ) const
{
return *this <= LargeInt(op2);
}
bool LargeInt::operator>(const LargeInt &op2) const
{
for(int i=0;i<=29;i++)
{
if(integer[i] > op2.integer[i])
return true;
else if(integer[i] < op2.integer[i])
return false;
}
return false;
}
bool LargeInt::operator>( int op2 ) const
{
return *this > LargeInt(op2);
}
bool LargeInt::operator>( long op2 ) const
{
return *this > LargeInt(op2);
}
bool LargeInt::operator>( const char *op2 ) const
{
return *this > LargeInt(op2);
}
bool LargeInt::operator>=(const LargeInt &op2) const
{
if (*this == op2 || *this > op2)
return true;
return false;
}
bool LargeInt::operator>=( int op2 ) const
{
return *this >= LargeInt(op2);
}
bool LargeInt::operator>=( long op2 ) const
{
return *this >= LargeInt(op2);
}
bool LargeInt::operator>=( const char *op2 ) const
{
return *this >= LargeInt(op2);
}
ostream& operator<<( ostream &output, const LargeInt &num )
{
int i;
for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <= 29 ); i++ );
if ( i == 30 )
output << 0;
else
for ( ; i <= 29; i++ )
output << num.integer[ i ];
return output;
}
#include <iostream>
using namespace std;
int main()
{
LargeInt n1( 7654321 );
LargeInt n2( 7891234 );
LargeInt n3( "99999999999999999999999999999" );
LargeInt n4( "1" );
LargeInt n5( 7654321 );
LargeInt n6( 991234 );
LargeInt result;
cout << "n1 is " << n1 << " n2 is " << n2
<< " n3 is " << n3 << " n4 is " << n4
<< " n5 is " << n5 << " n6 is " << n6
<< " result is " << result << " ";
// test relational and equality operators
if ( n1 == n5 )
cout << "n1 equals n5" << endl;
if ( n1 == n2 )
cout << "n1 equals n2" << endl;
if ( n1 != n5 )
cout << "n1 is not equal to n5" << endl;
if ( n1 != n2 )
cout << "n1 is not equal to n2" << endl;
if ( n1 < n2 )
cout << "n1 is less than n2" << endl;
if ( n1 < n5 )
cout << "n1 is less than n5" << endl;
if ( n2 < n6 )
cout << "n2 is less than n6" << endl;
if ( n1 <= n2 )
cout << "n1 is less than or equal to n2" << endl;
if ( n1 <= n5 )
cout << "n1 is less than or equal to n5" << endl;
if ( n1 > n2 )
cout << "n1 is greater than n2" << endl;
if ( n1 > n5 )
cout << "n1 is greater than n5" << endl;
if ( n2 > n1 )
cout << "n2 is greater than n1" << endl;
if ( n1 >= n2 )
cout << "n1 is greater than or equal to n2" << endl;
if ( n2 >= n1 )
cout << "n2 is greater than or equal to n1" << endl;
result = n1 + n2;
cout << n1 << " + " << n2 << " = " << result << " ";
cout << n3 << " + " << n4 << " = " << ( n3 + n4 ) << " ";
result = n1 + 9;
cout << n1 << " + " << 9 << " = " << result << endl;
result = n2 + "10000";
cout << n2 << " + " << "10000" << " = " << result << endl;
LargeInt n7( 7554321 );
LargeInt n8(200);
result = n2 - n7;
cout << n2 << " - " << n7 << " = " << result << endl;
result = n2 - "7554321";
cout << n2 << " - " << "7554321" << " = " << result << endl;
result=n7*n8;
cout << n7 << " * " << n8 << " = " << result << endl;
result=n7/n8;
cout << n7 << " / " << n8 << " = " << result << endl;
system("pause");
} // end main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.