// LabContest #1 // Complete insert, show_perm, intersect of the Set class. // - Set: a collection of distinct elements // - Permutation: ordered arrangement of elts in which each elt appears // exactly once /* A sample output C:\>settst Enter numbers for a set, a negative number when you are done: 3 5 7 -1 S1 = ( 3 5 7 ) Permutations: - 357 - 375 - 537 - 573 - 735 - 753 Enter numbers for a set, a negative number when you are done: 2 5 7 -1 S2 = ( 2 5 7 ) Permutations: - 257 - 275 - 527 - 572 - 725 - 752 S1 ^ S2 = ( 5 7 ) */ #include #include // Provides size_t #include // Provides assert class Set { public: enum { CAPACITY = 3 }; // Or static const size_t CAPACITY = 3; typedef int Item; Set( ) { used = 0; } void insert(const Item& entry); size_t size( ) const { return used; } size_t occurrences(const Item& target) const; void show_perm() const; friend void operator >> (istream& ins, Set& b); friend void operator << (ostream& outs, Set& b); friend Set intersect(const Set& s1, Set& s2); private: Item data[CAPACITY]; // The array to store items size_t used; // How much of array is used }; size_t Set::occurrences(const Item& target) const { size_t answer = 0; for (size_t i = 0; i < used; i++) if (target == data[i]) answer++; return answer; } void Set::insert(const Item& entry) { } void Set::show_perm() const // binding time problem; should be recursive sol. { } Set intersect(const Set& s1, Set& s2) { } void operator >> (istream& ins, Set& b) { Set::Item number; cout << "Enter numbers for a set, a negative number when you are done: "; ins >> number; while (number >= 0) { if (b.size( ) < b.CAPACITY) b.insert(number); else cout << "I have run out of room and can't add that number." << endl; cin >> number; } } void operator << (ostream& outs, Set& b) { outs << "( "; for(int i = 0; i < b.used; i++) outs << b.data[i] << " "; outs << ")" << endl; } void main( ) { Set s1, s2; cin >> s1; cout << "S1 = " << s1; s1.show_perm(); cin >> s2; cout << "S2 = " << s2; s2.show_perm(); cout << "S1 ^ S2 = " << intersect(s1, s2); }