Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3.  
  4. class Set
  5. {
  6. public:
  7.     Set();
  8.     Set(const Set&);
  9.     Set& operator=(const Set&);
  10.     ~Set();
  11.  
  12.     Set& operator+=(int);
  13.     Set operator+(int) const;
  14.    
  15.     Set& operator+=(const Set&);
  16.  
  17.     Set& operator-=(int);
  18.     Set& operator-=(const Set&);
  19.    
  20.     Set& operator*(const Set&);
  21.     //Set& operator*(int);
  22.    
  23.     Set& operator*=(const Set&);
  24.     Set operator%(const Set&);
  25.    
  26.     bool operator()(int) const;
  27.     bool operator!() const;
  28.  
  29.     bool operator==(const Set&) const;
  30.    
  31.     bool operator<(const Set&) const;
  32.     bool operator<=(const Set&) const;
  33.  
  34.     size_t getSize() const;
  35.  
  36.     int& operator[](size_t index);
  37.     const int& operator[](size_t index) const;
  38.  
  39.     friend std::ostream& operator<<(std::ostream &out, const Set&);
  40.  
  41. private:
  42.     int* elems;
  43.     size_t count;
  44. };
  45.  
  46. std::ostream& operator<<(std::ostream &out, const Set&);
  47.  
  48. //Set.cpp
  49. #include "Set.h"
  50.  
  51. Set& Set::operator+=(int x)
  52. {
  53.     //this->operator(elem) better
  54.     if ((*this)(x))
  55.         return *this;
  56.  
  57.     int* temp = new int[count + 1];
  58.     for (size_t i = 0; i < count; i++)
  59.     {
  60.         temp[i] = elems[i];
  61.     }
  62.     temp[count++] = x;
  63.     delete[] elems;
  64.     elems = temp;
  65.  
  66.     return *this;
  67. }
  68.  
  69. Set Set::operator+(int x) const
  70. {
  71.     Set result = *this;
  72.     result += x;
  73.     return result;
  74. }
  75.  
  76. Set& Set::operator+=(const Set &rhs)
  77. {
  78.     for (size_t i = 0; i < rhs.count; i++)
  79.     {
  80.         *this += rhs.elems[i];
  81.     }
  82.     return *this;
  83. }
  84.  
  85.  
  86. //Set& Set::operator-(int x) const;
  87. Set& Set::operator-=(int x)
  88. {
  89.     int* temp = new int[count - 1];
  90.     size_t w = 0;
  91.  
  92.     for (size_t r = 0; r < count; r++)
  93.     {
  94.         if (elems[r] != x)
  95.         {
  96.             temp[w++] = elems[r];
  97.         }
  98.     }
  99.  
  100.     delete[] elems;
  101.     --count;
  102.     elems = temp;
  103.  
  104.     return *this;
  105. }
  106.  
  107. Set& Set::operator-=(const Set& rhs)
  108. {
  109.     for (size_t i = 0; i < rhs.count; i++)
  110.     {
  111.         *this -= rhs.elems[i];
  112.     }
  113.     return *this;
  114. }
  115.  
  116. //A - (A - B)
  117. Set& Set::operator*=(const Set &rhs)
  118. {
  119.     for (size_t i = 0; i < count; i++)
  120.     {
  121.         if (!rhs(elems[i]))
  122.         {
  123.             *this -= elems[i--];
  124.         }
  125.     }
  126.     return *this;
  127. }
  128.  
  129. bool Set::operator==(const Set& rhs) const
  130. {
  131.     if (count != rhs.count)
  132.         return false;
  133.  
  134.     for (size_t i = 0; i < count; i++)
  135.     {
  136.         if (!rhs(elems[i]))
  137.             return false;
  138.     }
  139.     return true;
  140. }
  141.  
  142. bool Set::operator<(const Set& rhs) const
  143. {
  144.     if (count >= rhs.count)
  145.         return false;
  146.  
  147.     for (size_t i = 0; i < count; i++)
  148.     {
  149.         if (!rhs(elems[i]))
  150.             return false;
  151.     }
  152.     return true;
  153. }
  154.  
  155. bool Set::operator<=(const Set& rhs) const
  156. {
  157.     return (*this == rhs) || (*this < rhs);
  158. }
  159.  
  160. size_t Set::getSize() const
  161. {
  162.     return count;
  163. }
  164.  
  165. int& Set::operator[](size_t index)
  166. {
  167.     if (index >= count)
  168.         throw "Index out of range!";
  169.  
  170.     return elems[index];
  171. }
  172.  
  173. const int& Set::operator[](size_t index) const
  174. {
  175.     if (index >= count)
  176.         throw "Index out of range!";
  177.  
  178.     return elems[index];
  179. }
  180.  
  181. std::ostream& operator<<(std::ostream &out, const Set& set)
  182. {
  183.     out << set.count << std::endl;
  184.     for (size_t i = 0; i < set.count; i++)
  185.     {
  186.         out << set[i] << ' ';
  187.     }
  188.     return out;
  189. }
  190.  
  191. bool Set::operator()(int element) const
  192. {
  193.     for (size_t i = 0; i < count; i++)
  194.     {
  195.         if (elems[i] == element)
  196.             return true;
  197.     }
  198.  
  199.     return false;
  200. }
  201.  
  202. bool Set::operator!() const
  203. {
  204.     return !count;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement