LAB 2 In this Lab you will be using a class I have created that simulates a Set of characters. You will be writing several C++ functions that perform set operations. These are the functions you will...

1 answer below »
Attached are the instructions and the starter files. I only need the setFunctions.cpp file to be completed so I can submit it.


LAB 2 In this Lab you will be using a class I have created that simulates a Set of characters. You will be writing several C++ functions that perform set operations. These are the functions you will need to write and test. 1)setUnion - The union of s1 and s2 2) setIntersection - The intersection of s1 and s2 3) setDiff - The Difference , s1 - s2 4) setSymDiff - The symmetric Difference of s1 and s2 5) isSubSet - True if s1 is a subset of s2 6) isProperSubSet - True if s1 is a proper subset of s2 7) PowerSet - The Power Set of s -> P(s) 8) isPartition - True if the set of sets represents a partition of the given set. You will find the declaration of these function in the file called setFunctions.h and will write the definitions in setFunctions.cpp You can use the tests in main.cpp and add your own tests there. I have included some tests to start. When you are sure that your functions work correctly you should turn in the file setFunctions.cpp The Set Class I have provided is a template class. The functions you will define will use the char version of the Set class (Set). The Set class has several member functions: cardinality() - returns the number of characters in the set insertElement(char) - adds an character to the set (duplicates will be ignored) removeElement(char) - removes the character specified if it exists and ignores otherwise isElement(char) - returns true if the specified character is in the Set. clear() - removes all the characters from the set. (empties the set ) subscript operator ( [int] ) - returns an character from the set. Indexes (0 -> cardinality - 1) equality ( == ) - two sets are equal if they contain all the same characters ( use the operator) less than ( < )="" -="" a="" set="" is="" less="" than="" if="" it="" has="" fewer="" characters="" or="" its="" smallest="" elements="" are="" smaller.="" blank="" page="" blank="" page="" __macosx/._starter="" files="" starter="" files/setfunctions.h="" #ifndef="" set_func_h="" #define="" set_func_h="" #include="" "set.h"=""> setUnion(const Set& s1, const Set& s2); // The union of s1 and s2 -> return Set setIntersection(const Set& s1, const Set& s2); // The intersection of s1 and s2 -> return Set setDiff(const Set& s1, const Set& s2); // The Difference , s1 - s2 -> return Set setSymDiff(const Set& s1, const Set& s2); // The symetric Difference of s1 and s2 -> return bool isSubSet(const Set& s1, const Set& s2); // True if s1 is a subset of s2 bool isProperSubSet(const Set& s1, const Set& s2); // True if s1 is a proper subset of s2 Set<>> PowerSet(const Set& s); // The Power Set of s -> P(s) bool isPartition(const Set<>>& p, const Set& s); // True if the sets in p are a partition of set s #endif __MACOSX/Starter files/._setFunctions.h Starter files/set.cpp #include "set.h" #include #include #include using namespace std; template Set::Set() : s() {} template Set::Set(const vector& v1) :s{v1.begin(), v1.end()} {} template Set::Set(const T a[], size_t size) : s{ a, a + size } {} template Set::Set(std::initializer_list l) : s{ l.begin(), l.end() } {} template Set::Set(const Set& s1) : s(s1.s.begin(), s1.s.end()) {} template size_t Set::cardinality() const { return s.size(); } template void Set::insertElement(T n) { s.insert(n); } template void Set::removeElement(T n) { s.erase(n); } template bool Set::isElement(T n) const { for (T elem : s) { if (elem == n) { return true; } } return false; } template void Set::clear() { s.clear(); } template bool Set::operator==(const Set& s1) const { return this->s == s1.s; } template bool Set::operator!=(const Set& s1) const { return !(*this == s1); } template bool Set::operator<(const>& s1) const { if (this->cardinality() == s1.cardinality()) { for (size_t i = 0; i < s1.cardinality();="" ++i)="" {="" if="" ((*this)[i]="">< s1[i])="" {="" return="" true;="" }="" }="" }="" else="" if="" (this-="">cardinality() < s1.cardinality())="" return="" true;="" return="" false;="" }="" template=""> T Set::operator[](size_t i) const { typename set::iterator beg; if (i < s.size()="" &&="" i="">= 0) { beg = s.begin(); advance(beg, i); } else throw out_of_range("Index out of Range"); return *beg; } template ostream& operator<(std::ostream& out,="" const="">& s1) { vector
v{ s1.s.begin(), s1.s.end() }; out < '{';="" for="" (size_t="" i="0;" i="">< v.size();="" ++i)="" {="" if="" (="" i="" !="0" )="" out="">< ",="" "="">< v[i];="" else="" out="">< v[i];="" }="" out="">< '}';="" return="" out;="" }="" __macosx/starter="" files/._set.cpp="" starter="" files/setfunctions.cpp="" #include="" "setfunctions.h"="" #include="" "set.h"="" #include="" "set.cpp"="" using="" namespace="" std;="" returns="" a="" set="" that="" is="" the="" union="" of="" the="" two="" sets="" s1="" and="" s2=""> setUnion(const Set& s1, const Set& s2) { Set result; // Add your code here return result; } // Returns a set that is the intersection of the two sets s1 and s2 Set setIntersection(const Set& s1, const Set& s2) { Set result; // Add your code here return result; } // Returns a set that is the s1 - s2 Set setDiff(const Set& s1, const Set& s2) { Set result; // Add your code here return result; } // Returns a set that is the symetric difference of the two sets s1 and s2 Set setSymDiff(const Set& s1, const Set& s2) { Set result; // Add your code here return result; } // Returns true if s1 is a subset of s2 bool isSubSet(const Set& s1, const Set& s2) { // Add your code here and return the correct truth value return true; } // Returns true if s1 is a proper subset of s2 bool isProperSubSet(const Set& s1, const Set& s2) { // Add your code here and return the correct truth value return true; } // Returns the Power Set of set s as a set of sets Set<>> PowerSet(const Set& s) { Set<>> result; // Add your code here return result; } // Returns true if the sets in p make up a Partition of set s bool isPartition(const Set<>>& p, const Set& s) { // Add your code here and return the correct truth value return true; } __MACOSX/Starter files/._setFunctions.cpp Starter files/main.cpp #include #include #include "set.h" #include "set.cpp" #include "setFunctions.h" using namespace std; int main() { vector v{'s','f','h','i','a','h'}; Set s1{ 's','f','g','h','h','i','a', 'b' }; Set s2{ 'e','d','a','g','b','h','i','a','h' }; Set s3{ 'a','b','c','d','e','f' }; Set s4{ 'a','b','c' }; Set s5{ 'd','e','f', 'g' }; Set<>> ss{ s4,s5 }; // Set of Sets s4 and s5 cout < boolalpha;="" makes="" the="" printing="" of="" a="" boolean="" print="" true="" or="" false="" instead="" of="" 1="" or="" 0="" testing="" your="" functions="" by="" printing="" the="" results="" of="" calling="" each.="" you="" will="" need="" to="" verify="" if="" they="" are="" correct.="" you="" should="" also="" make="" your="" own="" tests="" to="" fully="" verify="" your="" functions.="" cout="">< "sets="" s1="" and="" s2\n";="" cout="">< "s1=" << s1 << endl; cout << " s2=" << s2 << endl; cout << " the="" union="" of="" sets="" s1="" and="" s2\n";="" cout="">< setunion(s1,="" s2)="">< endl;="" cout="">< "\nthe="" intersection="" of="" sets="" s1="" and="" s2\n\n";="" cout="">< setintersection(s1,="" s2)="">< endl;="" cout="">< "the="" difference="" of="" sets="" s1="" and="" s2\n\n";="" cout="">< setdiff(s1,="" s2)="">< endl;="" cout="">< "the="" symetric="" difference="" of="" sets="" s1="" and="" s2\n\n";="" cout="">< setsymdiff(s1,="" s2)="">< endl;="" cout="">< "\nsets="" s3,="" s4,="" s5="" and="" ss\n";="" cout="">< "s3=" << s3 << endl; cout << " s4=" << s4 << endl; cout << " s5=" << s5 << endl; cout << " ss=" << ss << endl; cout << " \nis="" s3="" a="" subset="" of="" s3\n";="" cout="">< issubset(s3,="" s3)="">< endl;="" cout="">< "\nis="" s3="" a="" proper="" subset="" of="" s3\n";="" cout="">< ispropersubset(s3,="" s3)="">< endl;="" cout="">< "\nis="" s4="" a="" subset="" of="" s3\n";="" cout="">< issubset(s4,="" s3)="">< endl;="" cout="">< "\nis="" s4="" a="" proper="" subset="" of="" s3\n";="" cout="">< ispropersubset(s4,="" s3)="">< endl;="" cout="">< "\nis="" s5="" a="" subset="" of="" s2\n";="" cout="">< issubset(s5,="" s2)="">< endl;="" cout="">< "\nis="" s5="" a="" proper="" subset="" of="" s2\n";="" cout="">< ispropersubset(s5,="" s2)="">< endl;="" cout="">< "\ndo="" the="" sets="" in="" ss="" make="" a="" partition="" of="" s3\n";="" cout="">< ispartition(ss,="" s3)="">< endl;="" cout="">< "\nthe="" power="" set="" p(s3)\n";="" cout="">< powerset(s4)="">< endl;="" return="" 0;="" }="" __macosx/starter="" files/._main.cpp="" starter="" files/set.h="" #ifndef="" set_h="" #define="" set_h="" #include=""> #include #include #include template class Set { template friend std::ostream& operator<(std::ostream& out,="" const="">& s); public: Set(); Set(const std::vector& v1); Set(const T a[], size_t size); Set(std::initializer_list l); Set(const Set& s1); size_t cardinality() const; void insertElement(T n); void removeElement(T n); bool isElement(T n) const; void clear(); bool operator==(const Set& s1) const; bool operator!=(const Set& s1) const; bool operator<(const>& s1) const; T operator[](size_t i) const; private: std::set s; }; #endif __MACOSX/Starter files/._set.h

Answered 155 days AfterOct 28, 2021

Answer To: LAB 2 In this Lab you will be using a class I have created that simulates a Set of characters. You...

Aditya answered on Apr 01 2022
119 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here