hugeint.c

 
// Lab 2: Hugeint.h // HugeInt class definition. #ifndef HUGEINT_H #define HUGEINT_H #include <iostream> 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; /* Write prototypes for the six relational and equality operators */ int getLength() const; private: short integer[ 30 ]; }; // end class HugeInt #endif /************************************************************************** * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * **************************************************************************/
11-13
// Lab 2: Hugeint.cpp // HugeInt member-function and friend-function definitions. #include <iostream> #include <cctype> // isdigit function prototype #include <cstring> // strlen function prototype using namespace std; #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 <= 29; i++ ) integer[ i ] = 0; // place digits of argument into array for ( int j = 29; 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 <= 29; i++ ) integer[ i ] = 0; // place digits of argument into array int length = strlen( string ); for ( int j = 30 - length, k = 0; j <= 29; 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 { for ( int i = 0; i <= 29; i++ ) if ( integer[ i ] != 0 ) break; // break when first digit is reached return 30 - i; // length is from first digit (at i) to end of array } // end function getLength // addition operator; HugeInt + HugeInt HugeInt HugeInt::operator+( const HugeInt &op2 ) const { HugeInt temp; // temporary result int carry = 0; for ( int i = 29; 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+ // equality operator; HugeInt == HugeInt /* Write a definition for the == operator */ // inequality operator; HugeInt != HugeInt /* Write a definition for the != operator by calling the == operator */ // less than operator; HugeInt < HugeInt /* Write a definition for the < operator */ // less than or equal operator; HugeInt <= HugeInt /* Write a definition for the <= operator by calling the < and == operators */ // greater than operator; HugeInt > HugeInt /* Write a definition for the > operator by calling the <= operator */ // greater than or equal operator; HugeInt >= HugeInt /* Write a definition for the >= operator by calling the > and == operators */ // overloaded output operator ostream& operator<<( ostream &output, const HugeInt &num ) { int i; for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <= 29 ); i++ ) ; // skip leading zeros if ( i == 30 ) output << 0; else for ( ; i <= 29; i++ ) output << num.integer[ i ]; return output; } // end function operator<< /************************************************************************** * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * **************************************************************************/
最新发布
11-13
// Lab 2: 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 result; cout << "n1 is " << n1 << "\nn2 is " << n2 << "\nn3 is " << n3 << "\nn4 is " << n4 << "\nresult is " << result << "\n\n"; // test relational and equality operators if ( n1 == n2 ) cout << "n1 equals n2" << endl; if ( n1 != n2 ) cout << "n1 is not equal to n2" << endl; if ( n1 < n2 ) cout << "n1 is less than n2" << endl; if ( n1 <= n2 ) cout << "n1 is less than or equal to n2" << endl; if ( n1 > n2 ) cout << "n1 is greater than n2" << endl; if ( n1 >= n2 ) cout << "n1 is greater than or equal to n2" << endl; result = n1 + n2; cout << n1 << " + " << n2 << " = " << result << "\n\n"; cout << n3 << " + " << n4 << "\n= " << ( n3 + n4 ) << "\n\n"; result = n1 + 9; cout << n1 << " + " << 9 << " = " << result << endl; result = n2 + "10000"; cout << n2 << " + " << "10000" << " = " << result << endl; } // end main /************************************************************************** * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * **************************************************************************/
11-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值