Write a C++ program that implements an ADT class named Int50 for manipulating in
ID: 3622972 • Letter: W
Question
Write a C++ program that implements an ADT class named Int50 for manipulating integer numbers with up to 50 digits. The operators provided on this type must include: sum (+), difference (-), product (*), division (/) {dropping the remainder}, output (<<), and input (>>) of values for Int50 objects. Each number will be represented by an array of short int elements, each representing one decimal digit of the whole number. Allow for positive and negative values. The input (>>) operation must allow values formatted like 0, 247, -86, and +244 to be entered but for any number of digits up to 50. On output, do not print leading zeroes. Do not prompt the user for input in the ADT, that should only be done in the application code that uses the ADT class. Use the class in a program that gives the user the ability to enter data and perform each of the calculations. The operator overloading you provide must support expressions like:N3 = N1+N2; //each variable is of type Int50
cout << "results: " << N4 << " & " << N5 << endl; // {might print -- results: -2738 & 74682354970035}
Bonus credit will be given for properly "throwing" an "exception" for division by zero and for overflow (number entered or produced exceeds 50 digits).
Explanation / Answer
please rate - thanks
to get you started
#include <iostream>
#include <cctype> // isdigit function prototype
#include <cstring> // strlen function prototype
using namespace std;
class Int50
{
friend ostream &operator<<( ostream &, const Int50 & );
friend istream& operator>>( istream &,Int50 & );
public:
Int50( long = 0 ); // conversion/default constructor
Int50( const char * ); // conversion constructor
// addition operator; Int50 + Int50
Int50 operator+( const Int50 & ) const;
// addition operator; Int50 + int
Int50 operator+( int ) const;
// addition operator;
// Int50 + string that represents large integer value
Int50 operator+( const char * ) const;
Int50 operator/( const Int50 &op2 ) const;
Int50 operator*( const Int50 &op2 ) const;
/* Write prototypes for the six relational and equality operators */
Int50 operator-( const Int50 & ) const;
bool operator==(const Int50&)const;
bool operator==(int)const;
bool operator==(long)const;
bool operator==(const char *)const;
bool operator!=(const Int50&)const;
bool operator!=(int)const;
bool operator!=(long)const;
bool operator!=(const char *)const;
bool operator<(const Int50&)const;
bool operator<(int)const;
bool operator<(long)const;
bool operator<(const char *)const;
bool operator<=(const Int50&)const;
bool operator<=(int)const;
bool operator<=(long)const;
bool operator<=(const char *)const;
bool operator>(const Int50&)const;
bool operator>(int)const;
bool operator>(long)const;
bool operator>(const char *)const;
bool operator>=(const Int50&)const;
bool operator>=(int)const;
bool operator>=(long)const;
bool operator>=(const char *)const;
int getLength() const;
private:
short integer[ 50 ];
}; // end class Int50
//#include "Int50.h" // Int50 class definition
// default constructor; conversion constructor that converts
// a long integer into a Int50 object
Int50::Int50( long value )
{
// initialize array to zero
for ( int i = 0; i <= 49; i++ )
integer[ i ] = 0;
// place digits of argument into array
for ( int j = 49; value != 0 && j >= 0; j-- )
{
integer[ j ] = value % 10;
value /= 10;
} // end for
} // end Int50 default/conversion constructor
// conversion constructor that converts a character string
// representing a large integer into a Int50 object
Int50::Int50( const char *string )
{
// initialize array to zero
for ( int i = 0; i <= 49; i++ )
integer[ i ] = 0;
// place digits of argument into array
int length = strlen( string );
for ( int j = 50 - length, k = 0; j <= 49; j++, k++ )
if ( isdigit( string[ k ] ) )
integer[ j ] = string[ k ] - '0';
} // end Int50 conversion constructor
// get function calculates length of integer
int Int50::getLength() const
{
int i;
for (i = 0; i <= 49; i++ )
if ( integer[ i ] != 0 )
break; // break when first digit is reached
return 50 - i; // length is from first digit (at i) to end of array
} // end function getLength
Int50 Int50::operator/( const Int50 &op2 ) const
{int i,j,digits;
Int50 tmp(*this),val("0"),ans,temp("0"),prod;
digits=(*this).getLength();
for(i=50-digits;i<=49;i++)
{val=val*10;
val.integer[49]=tmp.integer[i];
if(op2<=val)
{for(j=0;j<10;j++ )
{prod=op2*Int50(j+1);
if(prod>val)
break;
}
temp.integer[i]=j;
ans=op2*Int50(j);
}
val=val-ans;
}
return temp;
}
Int50 Int50::operator*( const Int50 &op2 ) const
{ Int50 temp;
int i,j,k,tmp;
for(i=0; i<50; i++)
temp.integer[ i ] =0;
for (int i=49; i>=1; i--)
{ k=i;
for (j=49; 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;
}
// addition operator; Int50 + Int50
Int50 Int50::operator+( const Int50 &op2 ) const
{
Int50 temp; // temporary result
int carry = 0;
for ( int i = 49; i >= 0; i-- )
{
temp.integer[ i ] =
integer[ i ] + op2.integer[ i ] + carry;
// determine whether to carry a 1
if ( temp.integer[ i ] > 9 )
{
temp.integer[ i ] %= 10; // reduce to 0-9
carry = 1;
} // end if
else // no carry
carry = 0;
} // end for
return temp; // return copy of temporary object
} // end function operator+
// addition operator; Int50 + int
Int50 Int50::operator+( int op2 ) const
{
// convert op2 to a Int50, then invoke
// operator+ for two Int50 objects
return *this + Int50( op2 );
} // end function operator+
// addition operator;
// Int50 + string that represents large integer value
Int50 Int50::operator+( const char *op2 ) const
{
// convert op2 to a Int50, then invoke
// operator+ for two Int50 objects
return *this + Int50( op2 );
} // end function operator+
Int50 Int50::operator-( const Int50 &op2 ) const
{
Int50 temp;
int borrow = 0;
for( int i = 49;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 Int50::operator==( const Int50 &op2 ) const
{
for(int i=0;i<=49;i++)
{
if(integer[i]!= op2.integer[i])
return false;
}
return true;
}
bool Int50::operator==( int op2 ) const
{
return *this == Int50(op2);
}
bool Int50::operator==( long op2 ) const
{
return *this == Int50(op2);
}
bool Int50::operator==( const char *op2 ) const
{
return *this == Int50(op2);
}
// inequality operator; Int50 != Int50
/* Write a definition for the != operator
by calling the == operator */
bool Int50::operator!=(const Int50 &op2) const
{
// only check in range maximum length from the last digit
int maxLen = (getLength() > op2.getLength()) ? getLength() : op2.getLength();
for(int i=49;i>=50-maxLen;i--)
{
if(integer[i] != op2.integer[i])
return true;
}
return false;
}
bool Int50::operator!=( int op2 ) const
{
return *this != Int50(op2);
}
bool Int50::operator!=( long op2 ) const
{
return *this != Int50(op2);
}
bool Int50::operator!=( const char *op2 ) const
{
return *this != Int50(op2);
}
// less than operator; Int50 < Int50
/* Write a definition for the < operator */
bool Int50::operator<(const Int50 &op2) const
{
for(int i=0;i<=49;i++)
{
if(integer[i] < op2.integer[i])
return true;
else if(integer[i] > op2.integer[i])
return false;
// ==, continue
}
return false;
}
bool Int50::operator<( int op2 ) const
{
return *this < Int50(op2);
}
bool Int50::operator<( long op2 ) const
{
return *this < Int50(op2);
}
bool Int50::operator<( const char *op2 ) const
{
return *this < Int50(op2);
}
// less than or equal operator; Int50 <= Int50
/* Write a definition for the <= operator
by calling the < and == operators */
bool Int50::operator<=(const Int50 &op2) const
{
if (*this == op2 || *this < op2)
return true;
return false;
}
bool Int50::operator<=( int op2 ) const
{
return *this <= Int50(op2);
}
bool Int50::operator<=( long op2 ) const
{
return *this <= Int50(op2);
}
bool Int50::operator<=( const char *op2 ) const
{
return *this <= Int50(op2);
}
// greater than operator; Int50 > Int50
/* Write a definition for the > operator
by calling the <= operator */
bool Int50::operator>(const Int50 &op2) const
{
for(int i=0;i<=49;i++)
{
if(integer[i] > op2.integer[i])
return true;
else if(integer[i] < op2.integer[i])
return false;
// ==, continue
}
return false;
}
bool Int50::operator>( int op2 ) const
{
return *this > Int50(op2);
}
bool Int50::operator>( long op2 ) const
{
return *this > Int50(op2);
}
bool Int50::operator>( const char *op2 ) const
{
return *this > Int50(op2);
}
// greater than or equal operator; Int50 >= Int50
/* Write a definition for the >= operator
by calling the > and == operators */
bool Int50::operator>=(const Int50 &op2) const
{
if (*this == op2 || *this > op2)
return true;
return false;
}
bool Int50::operator>=( int op2 ) const
{
return *this >= Int50(op2);
}
bool Int50::operator>=( long op2 ) const
{
return *this >= Int50(op2);
}
bool Int50::operator>=( const char *op2 ) const
{
return *this >= Int50(op2);
}
// overloaded output operator
ostream& operator<<( ostream &output, const Int50 &num )
{
int i;
for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <= 49 ); i++ )
; // skip leading zeros
if ( i == 50 )
output << 0;
else
for ( ; i <= 49; i++ )
output << num.integer[ i ];
return output;
} // end function operator<<
istream& operator>>( istream &input, Int50 &n )
{string in;
int i=0,digits=0,start,sign;
start=0;
sign=1;
for(i=0;i<50;i++)
n.integer[i]=0;
input>>in;
if (in[0]=='-')
{start=1;
sign=-1;
}
else if(in[0]=='+')
start=1;
for(digits=start;in[digits]!='';digits++);
for(i=start;i<digits;i++)
{ n.integer[50-digits+i]=in[i]-'0';
if(i==start)
n.integer[50-digits+i]*=sign;
}
return input;
} // end function operator>>
int main()
{
Int50 n1( 7654321 );
Int50 n2( 7891234 );
Int50 n3( "99999999999999999999999999999" );
Int50 n4( "1" );
Int50 n5( 7654321 );
Int50 n6( 991234 );
Int50 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;
Int50 n7( 7554321 );
Int50 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;
cout<<"enter a number: ";
cin>>n2;
cout<<"enter a number: ";
cin>>n3;
n4=n2+n3;
cout<<n4<<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.