Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Задача про множества - я тут использовал вектор булов, потому что так удобнее. Воспринимай это просто как массив булов( пометок ), по сути это и есть массив. Только его проще возвращать из функции.
- //Суть такая - каждая функция основывается на векторе, который каждый раз строится функцией build.
- #include <iostream>
- #include <vector>
- #include <string.h>
- class Set {
- private:
- std::vector<bool> build() const {
- std::vector<bool> ret(256);
- for(int i=0;i<sz;i++) ret[mem[i]] = 1;
- return ret;
- }
- public:
- Set(char *str, int s) {
- assign(str, s);
- }
- Set():mem(0), sz(0) {}
- void assign(char *str, int s) {
- sz = s;
- mem = new char[sz];
- for (int i=0;i<sz;i++) mem[i] = str[i];
- }
- ~Set() {
- delete[] mem;
- }
- Set(const Set& ot):sz(ot.sz) {
- mem = new char[sz];
- for (int i=0;i<sz;i++) mem[i] = ot.mem[i];
- }
- Set operator * (const Set& ot) {
- std::vector<bool> s1 = build(), s2 = ot.build();
- int col = 0;
- for (int i=0;i<256;i++) if (s1[i] && s2[i]) col++;
- char* mem = new char[col];
- col = 0;
- for (int i=0;i<256;i++) if (s1[i] && s2[i]) mem[col++] = i;
- Set ret(mem, col);
- delete[] mem;
- return ret;
- }
- Set operator + (const Set& ot) {
- std::vector<bool> s1 = build(), s2 = ot.build();
- int col = 0;
- for (int i=0;i<256;i++) if (s1[i] || s2[i]) col++;
- char* mem = new char[col];
- col = 0;
- for (int i=0;i<256;i++) if (s1[i] || s2[i]) mem[col++] = i;
- Set ret(mem, col);
- delete[] mem;
- return ret;
- }
- Set operator - (const Set& ot) {
- std::vector<bool> s1 = build(), s2 = ot.build();
- int col = 0;
- for (int i=0;i<256;i++) if (s1[i] && !s2[i]) col++;
- char* mem = new char[col];
- col = 0;
- for (int i=0;i<256;i++) if (s1[i] && !s2[i]) mem[col++] = i;
- Set ret(mem, col);
- delete[] mem;
- return ret;
- }
- bool operator == (const Set& ot) {
- std::vector<bool> s1 = build(), s2 = ot.build();
- bool ok = true;
- for (int i=0;ok &&i<256;i++) if (s1[i] != s2[i]) ok = false;
- return ok;
- }
- bool operator < (const Set& ot) {
- std::vector<bool> s1 = build(), s2 = ot.build();
- bool ok = true;
- for (int i=0;ok && i<256;i++) if (s1[i] && !s2[i]) ok = false;
- return ok;
- }
- friend std::ostream& operator <<(std::ostream &ot, const Set& a) {
- for (int i=0;i<a.sz;i++) ot << a.mem[i];
- return ot;
- }
- friend std::istream& operator >>(std::istream &is, Set &a) {
- char s[300];
- is >> s;
- a.assign(s, strlen(s));
- return is;
- }
- int size() const {
- return sz;
- }
- private:
- char *mem;
- int sz;
- };
- int main() {
- Set a,b;
- std::cin >> a >> b;
- Set inters = a * b;
- std::cout << "Intersection: " << inters << "\n";
- Set uni = a + b;
- std::cout << "Union: " << uni << "\n";
- Set diff = a - b;
- std::cout << "Diff: " << diff << "\n";
- bool is_equal = (a == b);
- bool is_subset = (a < b);
- std::cout << "Is equal: " << is_equal << "\n";
- std::cout << "Is subset: " << is_subset << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment