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

Create a class HugeInteger that uses a 40-element array of digits to store integ

ID: 3620852 • Letter: C

Question

Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and subtract. For comparing HugeInteger objects, provide functions isEqualTo and isNotEqualTo, isGreaterThan, isLessThan, IsGreaterThanOrEqualTo and isLessthanOrEqualTo - each of these is a "predicate" function that simply returns true or false if the relationship does not hold. Also, provide a predicate function isZero.

I'm don't even know where to begin and my teacher is not much help. Please help me!

Explanation / Answer

please rate - thanks should get you started #include <iostream>

#include <cctype> // isdigit function prototype

#include <cstring> // strlen function prototype

using namespace std;

class HugeInt

{

friend ostream &operator<<( ostream &, const HugeInt & );

public:

HugeInt( long = 0 ); // conversion/default constructor

HugeInt( const char * ); // conversion constructor

// addition operator; HugeInt + HugeInt

HugeInt operator+( const HugeInt & ) const;

// addition operator; HugeInt + int

HugeInt operator+( int ) const;

// addition operator;

// HugeInt + string that represents large integer value

HugeInt operator+( const char * ) const;
HugeInt operator/( const HugeInt &op2 ) const;
HugeInt operator*( const HugeInt &op2 ) const;
/* Write prototypes for the six relational and equality operators */


HugeInt operator-( const HugeInt & ) const;

bool operator==(const HugeInt&)const;

bool operator==(int)const;

bool operator==(long)const;

bool operator==(const char *)const;

bool operator!=(const HugeInt&)const;

bool operator!=(int)const;

bool operator!=(long)const;

bool operator!=(const char *)const;

bool operator<(const HugeInt&)const;

bool operator<(int)const;

bool operator<(long)const;

bool operator<(const char *)const;

bool operator<=(const HugeInt&)const;

bool operator<=(int)const;

bool operator<=(long)const;

bool operator<=(const char *)const;

bool operator>(const HugeInt&)const;

bool operator>(int)const;

bool operator>(long)const;

bool operator>(const char *)const;

bool operator>=(const HugeInt&)const;

bool operator>=(int)const;

bool operator>=(long)const;

bool operator>=(const char *)const;



int getLength() const;

private:

short integer[ 40 ];

}; // end class HugeInt
//#include "Hugeint.h" // HugeInt class definition

// default constructor; conversion constructor that converts

// a long integer into a HugeInt object

HugeInt::HugeInt( long value )

{

// initialize array to zero

for ( int i = 0; i <= 39; i++ )

integer[ i ] = 0;

// place digits of argument into array

for ( int j = 39; value != 0 && j >= 0; j-- )

{

integer[ j ] = value % 10;

value /= 10;

} // end for

} // end HugeInt default/conversion constructor

// conversion constructor that converts a character string

// representing a large integer into a HugeInt object

HugeInt::HugeInt( const char *string )

{

// initialize array to zero

for ( int i = 0; i <= 39; i++ )

integer[ i ] = 0;

// place digits of argument into array

int length = strlen( string );

for ( int j = 40 - length, k = 0; j <= 39; j++, k++ )

if ( isdigit( string[ k ] ) )

integer[ j ] = string[ k ] - '0';

} // end HugeInt conversion constructor

// get function calculates length of integer

int HugeInt::getLength() const

{

int i;

for (i = 0; i <= 39; i++ )

      if ( integer[ i ] != 0 )

break; // break when first digit is reached

return 40 - i; // length is from first digit (at i) to end of array

} // end function getLength


HugeInt HugeInt::operator/( const HugeInt &op2 ) const
{int i,j,digits;
HugeInt tmp(*this),val("0"),ans,temp("0"),prod;
digits=(*this).getLength();
for(i=40-digits;i<=39;i++)
     {val=val*10;
      val.integer[39]=tmp.integer[i];
      if(op2<=val)
            {for(j=0;j<10;j++ )
                 {prod=op2*HugeInt(j+1);
                 if(prod>val)
                     break;
                  }
            temp.integer[i]=j;
            ans=op2*HugeInt(j);
            }
       val=val-ans;
       }
return temp;
}
HugeInt HugeInt::operator*( const HugeInt &op2 ) const
    { HugeInt temp;
    int i,j,k,tmp;
          for(i=0; i<40; i++)
               temp.integer[ i ] =0;
          
       for (int i=39; i>=1; i--)
        { k=i;
          for (j=39; 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; HugeInt + HugeInt
HugeInt HugeInt::operator+( const HugeInt &op2 ) const

{

HugeInt temp; // temporary result

int carry = 0;

for ( int i = 39; 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; HugeInt + int

HugeInt HugeInt::operator+( int op2 ) const

{

// convert op2 to a HugeInt, then invoke

// operator+ for two HugeInt objects

return *this + HugeInt( op2 );

} // end function operator+

// addition operator;

// HugeInt + string that represents large integer value

HugeInt HugeInt::operator+( const char *op2 ) const

{

// convert op2 to a HugeInt, then invoke

// operator+ for two HugeInt objects

return *this + HugeInt( op2 );

} // end function operator+

HugeInt HugeInt::operator-( const HugeInt &op2 ) const

{

HugeInt temp;

int borrow = 0;

for( int i = 39;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 HugeInt::operator==( const HugeInt &op2 ) const

{

for(int i=0;i<=39;i++)

{

if(integer[i]!= op2.integer[i])

return false;

}

return true;

}

bool HugeInt::operator==( int op2 ) const

{

return *this == HugeInt(op2);

}

bool HugeInt::operator==( long op2 ) const

{

return *this == HugeInt(op2);

}

bool HugeInt::operator==( const char *op2 ) const

{

return *this == HugeInt(op2);

}

// inequality operator; HugeInt != HugeInt

/* Write a definition for the != operator

by calling the == operator */

bool HugeInt::operator!=(const HugeInt &op2) const

{

// only check in range maximum length from the last digit

int maxLen = (getLength() > op2.getLength()) ? getLength() : op2.getLength();



for(int i=39;i>=40-maxLen;i--)

{

if(integer[i] != op2.integer[i])

return true;

}

return false;

}

bool HugeInt::operator!=( int op2 ) const

{

return *this != HugeInt(op2);

}

bool HugeInt::operator!=( long op2 ) const

{

return *this != HugeInt(op2);

}

bool HugeInt::operator!=( const char *op2 ) const

{

return *this != HugeInt(op2);

}

// less than operator; HugeInt < HugeInt

/* Write a definition for the < operator */

bool HugeInt::operator<(const HugeInt &op2) const

{

for(int i=0;i<=39;i++)

{

if(integer[i] < op2.integer[i])

return true;

else if(integer[i] > op2.integer[i])

return false;

// ==, continue

}



return false;

}

bool HugeInt::operator<( int op2 ) const

{

return *this < HugeInt(op2);

}

bool HugeInt::operator<( long op2 ) const

{

return *this < HugeInt(op2);

}

bool HugeInt::operator<( const char *op2 ) const

{

return *this < HugeInt(op2);

}

// less than or equal operator; HugeInt <= HugeInt

/* Write a definition for the <= operator

by calling the < and == operators */

bool HugeInt::operator<=(const HugeInt &op2) const

{

if (*this == op2 || *this < op2)

return true;

return false;

}

bool HugeInt::operator<=( int op2 ) const

{

return *this <= HugeInt(op2);

}

bool HugeInt::operator<=( long op2 ) const

{

return *this <= HugeInt(op2);

}

bool HugeInt::operator<=( const char *op2 ) const

{

return *this <= HugeInt(op2);

}

// greater than operator; HugeInt > HugeInt

/* Write a definition for the > operator

by calling the <= operator */

bool HugeInt::operator>(const HugeInt &op2) const

{

for(int i=0;i<=39;i++)

{

if(integer[i] > op2.integer[i])

return true;

else if(integer[i] < op2.integer[i])

return false;

// ==, continue

}



return false;

}

bool HugeInt::operator>( int op2 ) const

{

return *this > HugeInt(op2);

}

bool HugeInt::operator>( long op2 ) const

{

return *this > HugeInt(op2);

}

bool HugeInt::operator>( const char *op2 ) const

{

return *this > HugeInt(op2);

}

// greater than or equal operator; HugeInt >= HugeInt

/* Write a definition for the >= operator

by calling the > and == operators */

bool HugeInt::operator>=(const HugeInt &op2) const

{

if (*this == op2 || *this > op2)

return true;

return false;

}

bool HugeInt::operator>=( int op2 ) const

{

return *this >= HugeInt(op2);

}

bool HugeInt::operator>=( long op2 ) const

{

return *this >= HugeInt(op2);

}

bool HugeInt::operator>=( const char *op2 ) const

{

return *this >= HugeInt(op2);

}



// overloaded output operator

ostream& operator<<( ostream &output, const HugeInt &num )

{

int i;

for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <= 39 ); i++ )

; // skip leading zeros

if ( i == 40 )

output << 0;

else

for ( ; i <= 39; i++ )

output << num.integer[ i ];

return output;

} // end function operator<<

//HugeIntTest.cpp

// HugeInt test program.

#include <iostream>

using namespace std;

//#include "Hugeint.h"

int main()

{

HugeInt n1( 7654321 );

HugeInt n2( 7891234 );

HugeInt n3( "99999999999999999999999999999" );

HugeInt n4( "1" );

HugeInt n5( 7654321 );

HugeInt n6( 991234 );

HugeInt 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;


HugeInt n7( 7554321 );
HugeInt 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


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