bwukki

Untitled

Apr 16th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. //IntegerSet.cpp
  2. #include "IntegerSet.h"
  3.  
  4. IntegerSet::IntegerSet() {
  5.     //ctor
  6.     for (int i = 0; i < 100; i++) {
  7.         nums.push_back(0);
  8.     }
  9. }
  10.  
  11. IntegerSet::IntegerSet(std::vector<int> in) {
  12.     //ctor
  13.     if (in.size() < 100) {
  14.         throw std::invalid_argument("Vector size must = 100");
  15.     }
  16.     for (auto i : in) {
  17.         nums.push_back(i);
  18.     }
  19. }
  20.  
  21. IntegerSet::~IntegerSet()
  22. {
  23.     //dtor
  24. }
  25.  
  26. void IntegerSet::push (int in) {
  27.     nums.push_back(in);
  28. }
  29.  
  30. void IntegerSet::knock (int in, int loc) {
  31.     nums[loc] = in;
  32. }
  33.  
  34. void IntegerSet::deleteNum (int loc) {
  35.     nums[loc] = 0;
  36. }
  37.  
  38. void IntegerSet::printSet() {
  39.     for (auto x : nums) {
  40.         std::cout << x;
  41.     }
  42.     std::cout << std::endl;
  43. }
  44.  
  45. std::string IntegerSet::toString() {
  46.     std::ostringstream result{};
  47.     for (int i = 0; i < nums.size(); i++) {
  48.         if (nums[i] != 0) {
  49.             result << i+1 << " ";
  50.         }
  51.     }
  52.     return result.str();
  53. }
  54.  
  55. bool IntegerSet::isEqualTo(IntegerSet in) {
  56.     int iterations{0};
  57.     bool result{1};
  58.     if (nums.size() >= this->nums.size()){
  59.         iterations = this->nums.size();
  60.     }
  61.     else {
  62.         iterations = nums.size();
  63.     }
  64.     for (int i = 0; i < iterations; i++) {
  65.         //std::cout << "Set 1:" << this->nums[i] << " set 2: " << nums[i] << std::endl;
  66.         if (this->nums[i] != in.nums[i]) {
  67.             result = 0;
  68.         }
  69.     }
  70.     return result;
  71. }
  72.  
  73. IntegerSet IntegerSet::unionOfSets (IntegerSet in) {
  74.     int iterations{0};
  75.     IntegerSet result{};
  76.     if (this->nums.size() >= in.nums.size()){
  77.         iterations = this->nums.size();
  78.     }
  79.     else {
  80.         iterations = in.nums.size();
  81.     }
  82.     for (int i = 0; i < iterations; i++) {
  83.         if (this->nums[i] == 1 || in.nums[i] == 1) {
  84.             result.knock(1,i);
  85.         }
  86.         else {
  87.             result.knock(0,i);
  88.         }
  89.     }
  90.     return result;
  91. }
  92.  
  93. IntegerSet IntegerSet::intersectionOfSets(IntegerSet in) {
  94.     IntegerSet result{};
  95.     int iterations{0};
  96.         if (this->nums.size() >= in.nums.size()){
  97.         iterations = this->nums.size();
  98.     }
  99.     else {
  100.         iterations = in.nums.size();
  101.     }
  102.     for (int i = 0; i < iterations; i++) {
  103.         if (this->nums[i] == 1 && in.nums[i] == 1) {
  104.             result.knock(1,i);
  105.         }
  106.         else {
  107.             result.knock(0,i);
  108.         }
  109.     }
  110.     return result;
  111. }
Add Comment
Please, Sign In to add comment