[C++]. I need to implement the set operations intersection and relative complement. I’ve implemented union and subtraction. PLEASE DO NOT USE STL.
int main() { set set1; set set2; set set3; set1.insert(90); set1.insert(89); set1.insert(49); set1.insert(24); set1.insert(70); set1.insert(99); set1.insert(63); set1.insert(55); set1.insert(75); set1.insert(51); set2.insert(37); set2.insert(59); set2.insert(89); set2.insert(94); set2.insert(71); set2.insert(69); set2.insert(28); set2.insert(95); set2.insert(75); set1.erase(89); set2.erase_one(28); cout << "set1 = " << set1 << endl; cout << "set2 = " << set2 << endl; cout << endl; // UNION cout << "set1 + set2 ==> " << endl << set1 + set2 << endl << endl; cout << endl; //SUBTRACTION cout << "set1 - set2 ==> " << endl << set1 - set2 << endl << endl; cout << endl; }
set.cpp
#include <algorithm> // Provides copy function #include <cassert> #include "set.h" #include <iostream> #include <unordered_set> using namespace std; bool set::contains(const value_type & target)const { for(int k = 0;k < 1 ;k++ ) { if(data[k]==target) return true; } return false; } const set::size_type set::CAPACITY; set::size_type set::erase(const value_type& target) { size_type index = 0; size_type many_removed = 0; while (index < used) { if (data[index] == target) { --used; data[index] = data[used]; ++many_removed; } else ++index; } return many_removed; } bool set::erase_one(const value_type& target) { size_type index; // The location of target in the data array // First, set index to the location of target in the data array, // which could be as small as 0 or as large as used-1. If target is not // in the array, then index will be set equal to used. index = 0; while ((index < used) && (data[index] != target)) ++index; if (index == used) return false; // target isnít in the set, so no work to do. // When execution reaches here, target is in the set at data[index]. // So, reduce used by 1 and copy the last item onto data[index]. --used; data[index] = data[used]; return true; } void set::insert(const value_type& entry) // Library facilities used: cassert { assert(size() < CAPACITY); if (!this->contains(entry)) { data[used] = entry; ++used; } } std::ostream &operator << (std::ostream&os, const set& arg_set) { os << '{'; set::size_type i; for (i = 0; i < arg_set.used; ++i) { os << arg_set.data[i]; if (i != arg_set.used - 1) os << ", "; } os << '}'; return os; } void set::operator +=(const set& arg_set) // Library facilities used: algorithm, cassert { // OLD ASSERT NO LONGER VALID // assert(size( ) + arg_set.size( ) <= CAPACITY); // OLD METHOD NO LONGER VALID // copy(arg_set.data, arg_set.data + arg_set.used, data + used); // used += arg_set.used; size_type i; for (i = 0; i < arg_set.size(); ++i) this->insert(arg_set.data[i]); } void set::operator -=(const set& arg_set) { set *bufferSet; size_type i, loopVal; bufferSet = this; loopVal = bufferSet->used; ///*DEBUG*/ cout << "USED = " << bufferSet->used << endl; for (i = 0; i < loopVal; ++i) { ///*DEBUG*/ cout << "I = " << i << " DATA = " << bufferSet->data[i] << endl; if (arg_set.contains(bufferSet->data[i])) { ///*DEBUG*/ cout << "ERASING DATA[I] = " << bufferSet->data[i] << endl; this->erase(bufferSet->data[i]); } } } set operator +(const set& s1, const set& s2) // Library facilities used: cassert { set answer; // OLD ASSERT NO LONGER VALID // assert(b1.size( ) + b2.size( ) <= set::CAPACITY); answer += s1; answer += s2; return answer; } set operator -(const set& s1, const set& s2) // Library facilities used: cassert { set answer; answer = s1; answer -= s2; return answer; }
set.h
// FILE: set.h // CLASS PROVIDED: set(part of the namespace main_savitch_3) // // TYPEDEF and MEMBER CONSTANTS for the set class: // typedef ____ value_type // set::value_type is the data type of the items in the set. It may be any of // the C++ built-in types (int, char, etc.), or a class with a default // constructor, an assignment operator, and operators to // test for equality (x == y) and non-equality (x != y). // // typedef ____ size_type // set::size_type is the data type of any variable that keeps track of how many items // are in a set. // // static const size_type CAPACITY = _____ // set::CAPACITY is the maximum number of items that a set can hold. // // CONSTRUCTOR for the set class: // set( ) // Postcondition: The set has been initialized as an empty set. // // MODIFICATION MEMBER FUNCTIONS for the set class: // size_type erase(const value_type& target); // Postcondition: All copies of target have been removed from the set. // The return value is the number of copies removed (which could be zero). // // void erase_one(const value_type& target) // Postcondition: If target was in the set, then one copy has been removed; // otherwise the set is unchanged. A true return value indicates that one // copy was removed; false indicates that nothing was removed. // // void insert(const value_type& entry) // Precondition: size( ) < CAPACITY. // Postcondition: A new copy of entry has been added to the set. // // void operator +=(const set& addend) // Precondition: size( ) + addend.size( ) <= CAPACITY. // Postcondition: Each item in addend has been added to this set. // // CONSTANT MEMBER FUNCTIONS for the set class: // size_type size( ) const // Postcondition: The return value is the total number of items in the set. // // size_type count(const value_type& target) const // Postcondition: The return value is number of times target is in the set. // // bool set::contains (const value_type& target) const; // Postcondition: The return value is true if // target is in the set; otherwise the return // value is false. // // NONMEMBER FUNCTIONS for the set class: // set operator +(const set& b1, const set& b2) // Precondition: b1.size( ) + b2.size( ) <= set::CAPACITY. // Postcondition: The set returned is the union of b1 and b2. // // VALUE SEMANTICS for the set class: // Assignments and the copy constructor may be used with set objects. #ifndef HW3_SET_H #define HW3_SET_H #include "bag1.h" class set { public: // TYPEDEFS and MEMBER CONSTANTS typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; // CONSTRUCTOR set( ) { used = 0; } // MODIFICATION MEMBER FUNCTIONS size_type erase(const value_type& target); bool erase_one(const value_type& target); void insert(const value_type& entry); void ints(const set& set1, const set& set2); void operator +=(const set& addend); void operator -=(const set& addend); // CONSTANT MEMBER FUNCTIONS size_type size( ) const { return used; } size_type count(const value_type& target) const; bool contains(const value_type &target) const; value_type data[CAPACITY]; // The array to store items size_type used; // How much of array is used }; // NONMEMBER FUNCTIONS for the set class set operator+(const set &s1, const set &s2); set operator-(const set &s1, const set &s2); #endif //HW3_SET_H