مشاهدة النسخة كاملة : دعوة للتفكير C++


منطلق بطموحي
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




السؤال الثاني :

هل يمكنك تطوير البرنامج بحيث يصبح الة حاسبة متكاملة تقوم بعمليات الضرب و الطرح و القسمة و الجمع ( محلولة ) للارقام الكبيرة جدا هذا اضافة للمقارانة بينها ( ايها اكبر مثلا او اصغر ...الخ ؟ )

حاسوبيه
04-04-2006, 10:22 PM
السلام عليكم ورحمة الله وبركاته:
اتشرف بانضمامي لهذا الموقع الرائع جدا وعلى الزملاء الموجودين
اسأل الله العلي القدير ان يوفقنا ويكتب لنا الخير والصلاح انا شاء الله.....

اريد انا اشكر المهندس (منطلق بطموحي)على شرحه الوافي لبرمجة السي بلس بلس.....

انا اريد مساعده في operator overloading

اريد كيفيه عمل كل عمليه مع شرح ومع ذكر مثال اذا تفضلتوا
واشكر لكم حسن تعاونكم.........

منطلق بطموحي
05-04-2006, 12:16 AM
و عليكم السلام و رحمة الله و بركاته :

حياك الله اختي حاسوبية في المنتدى و ان شاء الله تقضي الاوقات الممتعة و المفيدة :)

بالنسبة ل overloading ففي السؤال الذي طرحته مثال اما بالنسبة للعمليات فهي كثيرة و متعددة

+
-
*
/
%
=
==
+=
-=
*=
/=
%=
()
[]
<<
>>

هذا ما يخطر في بالي حاليا و اذا اردتي المزيد من الامثلة و الشرح فساضع لك لاحقا ان شاء الله

حاسوبيه
07-04-2006, 02:06 PM
اشكرك....
لكن انا اريد التوضيح في عمليةsubscript[] لان فيها اكثر من حاله احيانا نستخدم ال const واحيانا لا...
اريد شرح وافي لهذه العمليه...
وشكرا لك مره اخرى

حاسوبيه
07-04-2006, 07:19 PM
Define a class Byte which includes 8 bits, each bit can be 0 or 1. The bits are stored as an array in class Byte. The type of the array can be bool or int or char based on your choice. For the class offer the following methods:
1. A constructor
a. That initializes the Byte by 00000000.
b. Or initializes the array with a given value (sent to the constructor).
2. A copy constructor – state why you need it as a comment in the code
3. An overloaded assignment operator – state why you need it as a comment in the code
4. + which ORs the bits of two Bytes
5. * which ANDs the bits of two Bytes
6. ! which complements the bits of the Byte and returns the result
7. << >> to read and write objects of class Byte
8. operator[](int) to access a specific bit in the Byte
9. operator()(int,int,int) to set or clear multiple bits in the Byte

For example

Byte b1(11001100), b2(11110000), b3, b4(10000000), b5(10001000);

b3 = b1 * b2; // b3 = (11000000)

b3 = b1 + b2; // b3 = (11111100)

!b1; //b1=(00110011)

b4(2,5,1) //Function call operator. Sets the bits from bit 2 to bit 5 to be 1
//it leaves the other bits unchanged b4=(10111100)

b5(0,2,0) //Function call operator. Sets the bits from bit 0 to bit 2 to be 0
//it leaves the other bits unchanged b5=(00001000)



A reminder:
x y x OR y
0 0 0
0 1 1
1 0 1
1 1 1
x y x AND y
0 0 0
0 1 0
1 0 0
1 1 1
x !x
0 1
1 0

واشكر لكم حسن تعاونكم