Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This program compares sets of integers and includes IntegerSet.hpp, IntegerSet.cpp, and main.cpp for testing
- //IntegerSet.hpp code
- #ifndef INTEGERSET_HPP
- #define INTEGERSET_HPP
- #include <iostream>
- #include <vector>
- class IntegerSet{
- public:
- IntegerSet();
- IntegerSet(std::vector<int>);
- IntegerSet unionOfSets(const IntegerSet&) const;
- IntegerSet intersectionOfSets(const IntegerSet&) const;
- bool isEqual(const IntegerSet&);
- void insertElement(int);
- void deleteElement(int);
- std::string toString() const;
- private:
- std::vector<bool> boolSet;
- };
- #endif // INTEGERSET_HPP
- //IntegerSet.cpp code
- #include "IntegerSet.hpp"
- IntegerSet::IntegerSet()
- : boolSet(101, false){
- //empty body
- }
- IntegerSet::IntegerSet(std::vector<int> intSet)
- : boolSet(101, false){
- for (auto item: intSet){
- boolSet[item] = true;
- }
- }
- IntegerSet IntegerSet::unionOfSets(const IntegerSet& other) const{
- IntegerSet unionSet{};
- for(int i{0}; i <= 100; i += 1){
- if (boolSet[i] || other.boolSet[i]) {
- unionSet.boolSet[i] = true;
- }
- }
- return unionSet;
- }
- IntegerSet IntegerSet::intersectionOfSets(const IntegerSet& other) const{
- IntegerSet unionSet{};
- for(int i{0}; i <= 100; i += 1){
- if (boolSet[i] && other.boolSet[i]) {
- unionSet.boolSet[i] = true;
- }
- }
- return unionSet;
- }
- bool IntegerSet::isEqual(const IntegerSet& other) {
- return (boolSet == other.boolSet);
- }
- void IntegerSet::insertElement(int num) {
- boolSet[num] = true;
- }
- void IntegerSet::deleteElement(int num) {
- boolSet[num] = false;
- }
- std::string IntegerSet::toString() const{
- std::string myString{""};
- for(int i{0}; i <= 100; i += 1){
- if (boolSet[i]){
- myString += std::to_string(i) + " " ;
- }
- }
- return myString == ""? "---": myString;
- }
- //main.cpp code
- #include <iostream>
- #include "IntegerSet.hpp"
- using namespace std;
- int main()
- {
- vector<int> set1Vector{5,10,20,40,50,100};
- vector<int> set2Vector{5,20,50,67,82,100};
- vector<int> equalSet1Vector{1,2,3,4,5};
- vector<int> equalSet2Vector{1,2,3,4,5};
- IntegerSet set1{set1Vector};
- IntegerSet set2{set2Vector};
- IntegerSet equalSet1{equalSet1Vector};
- IntegerSet equalSet2{equalSet2Vector};
- cout << equalSet1.isEqual(equalSet2) << endl;
- cout << "set1 initial values: " << set1.toString() << endl;
- cout << "\n";
- cout << "set2 initial values: " << set2.toString() << endl;
- cout << endl;
- cout << "unionOfSets Output should be: \t5 10 20 40 50 67 82 100 \n" <<
- "unionOfSets Output is: \t\t" << (set1.unionOfSets(set2)).toString() << endl;
- cout << "\nintersectionOfSets Output should be: \t5 20 50 100 \n"
- << "intersectionOfSets Output is: \t\t"
- << (set1.intersectionOfSets(set2)).toString() << endl;
- cout << "\nset2 before inserting 10: " << set2.toString() << endl;
- set2.insertElement(10);
- cout << "set2 after inserting 10: " << set2.toString() << endl;
- set2.deleteElement(20);
- cout << "set2 after deleting 20: " << set2.toString() << endl;
- cout << "unionOfSets Output after additions/deletions should be: 5 10 20 40 50 67 82 100 \n" <<
- "unionOfSets Output is: " << (set1.unionOfSets(set2)).toString() << endl;
- cout << "intersectionOfSets Output after additions/deletions should be: 5 10 50 100 \n" << "Output is: "
- << (set1.intersectionOfSets(set2)).toString() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement