Advertisement
Nofxthepirate

Integer Set Program

Jun 15th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. //This program compares sets of integers and includes IntegerSet.hpp, IntegerSet.cpp, and main.cpp for testing
  2.  
  3. //IntegerSet.hpp code
  4.  
  5. #ifndef INTEGERSET_HPP
  6. #define INTEGERSET_HPP
  7. #include <iostream>
  8. #include <vector>
  9.  
  10.  
  11. class IntegerSet{
  12.  
  13. public:
  14.     IntegerSet();
  15.     IntegerSet(std::vector<int>);
  16.  
  17.     IntegerSet unionOfSets(const IntegerSet&) const;
  18.     IntegerSet intersectionOfSets(const IntegerSet&) const;
  19.     bool isEqual(const IntegerSet&);
  20.     void insertElement(int);
  21.     void deleteElement(int);
  22.     std::string toString() const;
  23.  
  24. private:
  25.     std::vector<bool> boolSet;
  26.  
  27.  
  28. };
  29.  
  30. #endif // INTEGERSET_HPP
  31.  
  32.  
  33. //IntegerSet.cpp code
  34.  
  35. #include "IntegerSet.hpp"
  36.  
  37.  
  38. IntegerSet::IntegerSet()
  39.     : boolSet(101, false){
  40.     //empty body
  41.     }
  42. IntegerSet::IntegerSet(std::vector<int> intSet)
  43.     : boolSet(101, false){
  44.  
  45.         for (auto item: intSet){
  46.             boolSet[item] = true;
  47.         }
  48.     }
  49.  
  50. IntegerSet IntegerSet::unionOfSets(const IntegerSet& other) const{
  51.     IntegerSet unionSet{};
  52.  
  53.         for(int i{0}; i <= 100; i += 1){
  54.             if (boolSet[i] || other.boolSet[i]) {
  55.                 unionSet.boolSet[i] = true;
  56.             }
  57.         }
  58.     return unionSet;
  59. }
  60. IntegerSet IntegerSet::intersectionOfSets(const IntegerSet& other) const{
  61.     IntegerSet unionSet{};
  62.  
  63.         for(int i{0}; i <= 100; i += 1){
  64.             if (boolSet[i] && other.boolSet[i]) {
  65.                 unionSet.boolSet[i] = true;
  66.             }
  67.         }
  68.     return unionSet;
  69. }
  70.  
  71. bool IntegerSet::isEqual(const IntegerSet& other) {
  72.     return (boolSet == other.boolSet);
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79. void IntegerSet::insertElement(int num) {
  80.     boolSet[num] = true;
  81.  
  82.  
  83. }
  84. void IntegerSet::deleteElement(int num) {
  85.     boolSet[num] = false;
  86. }
  87.     std::string IntegerSet::toString() const{
  88.         std::string myString{""};
  89.         for(int i{0}; i <= 100; i += 1){
  90.             if (boolSet[i]){
  91.                 myString += std::to_string(i) + " " ;
  92.             }
  93.         }
  94.         return myString == ""? "---": myString;
  95.     }
  96.  
  97.  
  98. //main.cpp code
  99.  
  100. #include <iostream>
  101. #include "IntegerSet.hpp"
  102. using namespace std;
  103.  
  104. int main()
  105. {
  106.     vector<int> set1Vector{5,10,20,40,50,100};
  107.     vector<int> set2Vector{5,20,50,67,82,100};
  108.     vector<int> equalSet1Vector{1,2,3,4,5};
  109.     vector<int> equalSet2Vector{1,2,3,4,5};
  110.     IntegerSet set1{set1Vector};
  111.     IntegerSet set2{set2Vector};
  112.     IntegerSet equalSet1{equalSet1Vector};
  113.     IntegerSet equalSet2{equalSet2Vector};
  114.  
  115.     cout << equalSet1.isEqual(equalSet2) << endl;
  116.  
  117.     cout << "set1 initial values: " << set1.toString() << endl;
  118.     cout << "\n";
  119.     cout << "set2 initial values: " << set2.toString() << endl;
  120.     cout << endl;
  121.  
  122.     cout << "unionOfSets Output should be: \t5 10 20 40 50 67 82 100 \n" <<
  123.     "unionOfSets Output is: \t\t" << (set1.unionOfSets(set2)).toString() << endl;
  124.  
  125.     cout << "\nintersectionOfSets Output should be: \t5 20 50 100 \n"
  126.     << "intersectionOfSets Output is: \t\t"
  127.     << (set1.intersectionOfSets(set2)).toString() << endl;
  128.  
  129.     cout << "\nset2 before inserting 10: " << set2.toString() << endl;
  130.     set2.insertElement(10);
  131.     cout << "set2 after inserting 10: " << set2.toString() << endl;
  132.  
  133.     set2.deleteElement(20);
  134.     cout << "set2 after deleting 20: " << set2.toString() << endl;
  135.  
  136.     cout << "unionOfSets Output after additions/deletions should be: 5 10 20 40 50 67 82 100 \n" <<
  137.     "unionOfSets Output is: " << (set1.unionOfSets(set2)).toString() << endl;
  138.  
  139.     cout << "intersectionOfSets Output after additions/deletions should be: 5 10 50 100 \n" << "Output is: "
  140.     << (set1.intersectionOfSets(set2)).toString() << endl;
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement