精華區beta NTUEE107HW 關於我們 聯絡資訊
※ 引述《blackmonster (不夠堅持)》之銘言: : 建立IntegerSet的類別。類別IntegerSet的每物件都可以存入從0到100之間的整數。 : 集合內部都可以0和1的陣列表示。如果整數i是在此集合內的話,則陣列元素a[i]就為1, : 不在就為0。預設的建構子會將集合初始化成所謂的"空集合"(即代表此集合的陣列 : 其元素皆為0) : 提供成員函式能執行通常的集合運算。unionOfIntegerSets可建立現有二個集合的 : 聯集,intersectionOfIntegerSets建立交集,insertElement將一個新的整數k加入集合 : 中,deleteElement將一個新的整數m從集合中刪除。setPrint將集合中的數字以空白 : 隔開後列印出來。只列印出那些出現在集合中的元素(也就是他們在陣列位置的值是1) : 空集合的話就印出---。提供一個isEqualTo成員函式,判定是否二個集合相等。 : 提供接受五個整數引數的的額外建構子,用來設定集合物件的初值。 : 如果你想要提供少於五個元素在集合,可在多餘的位置使用預定引數值-1。 : 現在撰寫一個主程式來測試所寫的IntegerSet。產生幾個IntegerSet物件。測試你所有 : 的成員函式是否可以正確地執行。 : 我只把中文版簡略的打上去…希望有幫助 #include <iostream> using std::cout; using std::cin; using std::endl; using std::boolalpha; class IntegerSet { //各函數功能詳見課本 friend IntegerSet unionOfIntegerSets( const IntegerSet, const IntegerSet ); friend IntegerSet intersectionOfIntegerSets( const IntegerSet, const IntegerSet ); friend bool isEqualTo( const IntegerSet, const IntegerSet ); public: IntegerSet( int = -1, int = -1, int = -1, int = -1, int = -1 ); void insertElement( int ); void deleteElement( int ); void setPrint() const; private: int a[ 101 ]; }; IntegerSet::IntegerSet( int e1, int e2, int e3, int e4, int e5 ) { for ( int i = 0; i < 101; i++ ) a[ i ] = 0; insertElement( e1 ); insertElement( e2 ); insertElement( e3 ); insertElement( e4 ); insertElement( e5 ); } void IntegerSet::insertElement( int k ) { if ( k >= 0 && k <= 100 ) a[ k ] = 1; } void IntegerSet::deleteElement( int m ) { if ( m >= 0 && m <= 100 ) a[ m ] = 0; } void IntegerSet::setPrint() const { int count = 0; for ( int j = 0; j < 101; j++ ) { if ( a[ j ] == 1 ) { cout << j << ' '; count++; } if ( count % 20 == 0 && count != 0 ) cout << endl; } if ( count == 0 ) cout << "---"; cout << endl; } int main() { cout << "driver program test:\n\n"; IntegerSet A; cout << "Following are the elements in A:\n"; A.setPrint(); IntegerSet B( 68, 0, 32, 103, 100 ); cout << "Following are the elements in B:\n"; B.setPrint(); A.insertElement( 68 ); A.insertElement( 43 ); A.insertElement( 43 ); A.insertElement( -8 ); A.insertElement( 0 ); cout << "Following are the elements in A:\n"; A.setPrint(); B.deleteElement( 32 ); B.deleteElement( -9 ); B.insertElement( 23 ); cout << "Following are the elements in B:\n"; B.setPrint(); cout << "Set A is equal to set B?\n"; cout << boolalpha << isEqualTo( A, B ) << endl; IntegerSet C = unionOfIntegerSets( A, B ); cout << "Following are the elements in the union of A and B:\n"; C.setPrint(); IntegerSet D = intersectionOfIntegerSets( A, B ); cout << "Following are the elements in the intersection of A and B:\n"; D.setPrint(); A.insertElement( 23 ); A.insertElement( 100 ); A.deleteElement( 43 ); cout << "Following are the elements in A:\n"; A.setPrint(); cout << "Following are the elements in B:\n"; B.setPrint(); cout << "Set A is equal to set B?\n"; cout << boolalpha << isEqualTo( A, B ) << endl; return 0; } IntegerSet unionOfIntegerSets( const IntegerSet X, const IntegerSet Y ) { //聯集 IntegerSet Z; for ( int i = 0; i < 101; i++ ) { if ( X.a[ i ] == 1 || Y.a[ i ] == 1 ) Z.a[ i ] = 1; } return Z; } IntegerSet intersectionOfIntegerSets( const IntegerSet X, const IntegerSet Y ) { //交集 IntegerSet Z; for ( int i = 0; i < 101; i++ ) { if ( X.a[ i ] == 1 && Y.a[ i ] == 1 ) Z.a[ i ] = 1; } return Z; } bool isEqualTo( const IntegerSet X, const IntegerSet Y ) { //判斷集合是否相等 bool equal = true; for ( int i = 0; i < 101; i++ ) { if ( X.a[ i ] != Y.a[ i ] ) equal = false; } return equal; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.230.47.208
kafai:未到期限po程式碼是「天理不容」的XD 推140.112.239.182 12/10
timrau:這樣做對嗎?拿出良心和義氣來啊 推 210.85.10.126 12/10