Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- class Set
- {
- public:
- Set();
- Set(const Set&);
- Set& operator=(const Set&);
- ~Set();
- Set& operator+=(int);
- Set operator+(int) const;
- Set& operator+=(const Set&);
- Set& operator-=(int);
- Set& operator-=(const Set&);
- Set& operator*(const Set&);
- //Set& operator*(int);
- Set& operator*=(const Set&);
- Set operator%(const Set&);
- bool operator()(int) const;
- bool operator!() const;
- bool operator==(const Set&) const;
- bool operator<(const Set&) const;
- bool operator<=(const Set&) const;
- size_t getSize() const;
- int& operator[](size_t index);
- const int& operator[](size_t index) const;
- friend std::ostream& operator<<(std::ostream &out, const Set&);
- private:
- int* elems;
- size_t count;
- };
- std::ostream& operator<<(std::ostream &out, const Set&);
- //Set.cpp
- #include "Set.h"
- Set& Set::operator+=(int x)
- {
- //this->operator(elem) better
- if ((*this)(x))
- return *this;
- int* temp = new int[count + 1];
- for (size_t i = 0; i < count; i++)
- {
- temp[i] = elems[i];
- }
- temp[count++] = x;
- delete[] elems;
- elems = temp;
- return *this;
- }
- Set Set::operator+(int x) const
- {
- Set result = *this;
- result += x;
- return result;
- }
- Set& Set::operator+=(const Set &rhs)
- {
- for (size_t i = 0; i < rhs.count; i++)
- {
- *this += rhs.elems[i];
- }
- return *this;
- }
- //Set& Set::operator-(int x) const;
- Set& Set::operator-=(int x)
- {
- int* temp = new int[count - 1];
- size_t w = 0;
- for (size_t r = 0; r < count; r++)
- {
- if (elems[r] != x)
- {
- temp[w++] = elems[r];
- }
- }
- delete[] elems;
- --count;
- elems = temp;
- return *this;
- }
- Set& Set::operator-=(const Set& rhs)
- {
- for (size_t i = 0; i < rhs.count; i++)
- {
- *this -= rhs.elems[i];
- }
- return *this;
- }
- //A - (A - B)
- Set& Set::operator*=(const Set &rhs)
- {
- for (size_t i = 0; i < count; i++)
- {
- if (!rhs(elems[i]))
- {
- *this -= elems[i--];
- }
- }
- return *this;
- }
- bool Set::operator==(const Set& rhs) const
- {
- if (count != rhs.count)
- return false;
- for (size_t i = 0; i < count; i++)
- {
- if (!rhs(elems[i]))
- return false;
- }
- return true;
- }
- bool Set::operator<(const Set& rhs) const
- {
- if (count >= rhs.count)
- return false;
- for (size_t i = 0; i < count; i++)
- {
- if (!rhs(elems[i]))
- return false;
- }
- return true;
- }
- bool Set::operator<=(const Set& rhs) const
- {
- return (*this == rhs) || (*this < rhs);
- }
- size_t Set::getSize() const
- {
- return count;
- }
- int& Set::operator[](size_t index)
- {
- if (index >= count)
- throw "Index out of range!";
- return elems[index];
- }
- const int& Set::operator[](size_t index) const
- {
- if (index >= count)
- throw "Index out of range!";
- return elems[index];
- }
- std::ostream& operator<<(std::ostream &out, const Set& set)
- {
- out << set.count << std::endl;
- for (size_t i = 0; i < set.count; i++)
- {
- out << set[i] << ' ';
- }
- return out;
- }
- bool Set::operator()(int element) const
- {
- for (size_t i = 0; i < count; i++)
- {
- if (elems[i] == element)
- return true;
- }
- return false;
- }
- bool Set::operator!() const
- {
- return !count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement