منطلق بطموحي
26-03-2006, 11:16 PM
tk1 السلام عليكم :
كما هو معروف و ما اعتدنا عليه عند اجراء عمليات ال overloading باستعمال لغة C++ و اقصد بالذات عمليات I/O
فناخذ العملية << و هي عملية الاخراج ...فاننا عادة ما نجعلها friend ل class الذي نتعامل معه بحيث يمكن رؤية بياناتها
من منكم يخبرني كيف يمكن اجراء عملية overloading لها دون استعمال friend اي اريد اخذ المعلومات منها عن طريق دالة عضو member function ؟
ان شاء الله ساضع الاجابة و لكن بعد رؤية المحاولات !
جرب المطلوب على هذا المثال:
#include<iostream>
#include<cctype>
#include<cstring>
#include<cstdlib>
using namespace std;
class HugeInt **
friend 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<<
public:
HugeInt( long = 0 ); // conversion/default constructor
HugeInt( const char * ); // conversion constructor
// addition operator; HugeInt + HugeInt
HugeInt operator+( const HugeInt & );
// addition operator; HugeInt + int
HugeInt operator+( int );
// addition operator;
// HugeInt + string that represents large integer value
HugeInt operator+( const char * );
// multiply hugeinteger*hugeinteger
HugeInt operator*(const HugeInt &);
HugeInt operator/(const HugeInt &);
bool operator<(const HugeInt &op2) **
for(int i=0;i<=29;i++ ) ;
if(integer[i]<op2.integer[i]) return true;
}
bool operator >(const HugeInt &op2) **
for(int i=0;i<=29;i++);
if(integer[i]>op2.integer[i]) return true;
}
bool operator==(const HugeInt &op2) **
for(int i=0;i<=0;i++) ;
if(integer[i]==op2.integer[i]) return true;
}
private:
short integer[ 30 ];
}; // end class HugeInt
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
// addition operator; HugeInt + HugeInt
HugeInt HugeInt::operator+( const HugeInt &op2 )
**
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
// no carry
else
carry = 0;
}
return temp; // return copy of temporary object
} // end function operator+
// addition operator; HugeInt + int
HugeInt HugeInt::operator+( int op2 )
**
// 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 )
**
// convert op2 to a HugeInt, then invoke
// operator+ for two HugeInt objects
return *this + HugeInt( op2 );
} // end operator+
int main()
**
HugeInt n1( 7654321 );
HugeInt n2( 7891234 );
HugeInt n3( "99999999999999999999999999999" );
HugeInt n4( "1" );
HugeInt n5;
cout << "n1 is " << n1 << "\nn2 is " << n2
<< "\nn3 is " << n3 << "\nn4 is " << n4
<< "\nn5 is " << n5 << "\n\n";
n5 = n1 + n2;
cout << n1 << " + " << n2 << " = " << n5 << "\n\n";
cout << n3 << " + " << n4 << "\n= " << ( n3 + n4 )
<< "\n\n";
n5 = n1 + 9;
cout << n1 << " + " << 9 << " = " << n5 << "\n\n";
n5 = n2 + "10000";
cout << n2 << " + " << "10000" << " = " << n5 << endl;
return 0;
} // end main
السؤال الثاني :
هل يمكنك تطوير البرنامج بحيث يصبح الة حاسبة متكاملة تقوم بعمليات الضرب و الطرح و القسمة و الجمع ( محلولة ) للارقام الكبيرة جدا هذا اضافة للمقارانة بينها ( ايها اكبر مثلا او اصغر ...الخ ؟ )
كما هو معروف و ما اعتدنا عليه عند اجراء عمليات ال overloading باستعمال لغة C++ و اقصد بالذات عمليات I/O
فناخذ العملية << و هي عملية الاخراج ...فاننا عادة ما نجعلها friend ل class الذي نتعامل معه بحيث يمكن رؤية بياناتها
من منكم يخبرني كيف يمكن اجراء عملية overloading لها دون استعمال friend اي اريد اخذ المعلومات منها عن طريق دالة عضو member function ؟
ان شاء الله ساضع الاجابة و لكن بعد رؤية المحاولات !
جرب المطلوب على هذا المثال:
#include<iostream>
#include<cctype>
#include<cstring>
#include<cstdlib>
using namespace std;
class HugeInt **
friend 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<<
public:
HugeInt( long = 0 ); // conversion/default constructor
HugeInt( const char * ); // conversion constructor
// addition operator; HugeInt + HugeInt
HugeInt operator+( const HugeInt & );
// addition operator; HugeInt + int
HugeInt operator+( int );
// addition operator;
// HugeInt + string that represents large integer value
HugeInt operator+( const char * );
// multiply hugeinteger*hugeinteger
HugeInt operator*(const HugeInt &);
HugeInt operator/(const HugeInt &);
bool operator<(const HugeInt &op2) **
for(int i=0;i<=29;i++ ) ;
if(integer[i]<op2.integer[i]) return true;
}
bool operator >(const HugeInt &op2) **
for(int i=0;i<=29;i++);
if(integer[i]>op2.integer[i]) return true;
}
bool operator==(const HugeInt &op2) **
for(int i=0;i<=0;i++) ;
if(integer[i]==op2.integer[i]) return true;
}
private:
short integer[ 30 ];
}; // end class HugeInt
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
// addition operator; HugeInt + HugeInt
HugeInt HugeInt::operator+( const HugeInt &op2 )
**
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
// no carry
else
carry = 0;
}
return temp; // return copy of temporary object
} // end function operator+
// addition operator; HugeInt + int
HugeInt HugeInt::operator+( int op2 )
**
// 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 )
**
// convert op2 to a HugeInt, then invoke
// operator+ for two HugeInt objects
return *this + HugeInt( op2 );
} // end operator+
int main()
**
HugeInt n1( 7654321 );
HugeInt n2( 7891234 );
HugeInt n3( "99999999999999999999999999999" );
HugeInt n4( "1" );
HugeInt n5;
cout << "n1 is " << n1 << "\nn2 is " << n2
<< "\nn3 is " << n3 << "\nn4 is " << n4
<< "\nn5 is " << n5 << "\n\n";
n5 = n1 + n2;
cout << n1 << " + " << n2 << " = " << n5 << "\n\n";
cout << n3 << " + " << n4 << "\n= " << ( n3 + n4 )
<< "\n\n";
n5 = n1 + 9;
cout << n1 << " + " << 9 << " = " << n5 << "\n\n";
n5 = n2 + "10000";
cout << n2 << " + " << "10000" << " = " << n5 << endl;
return 0;
} // end main
السؤال الثاني :
هل يمكنك تطوير البرنامج بحيث يصبح الة حاسبة متكاملة تقوم بعمليات الضرب و الطرح و القسمة و الجمع ( محلولة ) للارقام الكبيرة جدا هذا اضافة للمقارانة بينها ( ايها اكبر مثلا او اصغر ...الخ ؟ )