makrusak

Сеты для Насти.

Dec 23rd, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. //Задача про множества - я тут использовал вектор булов, потому что так удобнее. Воспринимай это просто как массив булов( пометок ), по сути это и есть массив. Только его проще возвращать из функции.
  2. //Суть такая - каждая функция основывается на векторе, который каждый раз строится функцией build.
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <string.h>
  7.  
  8. class Set {
  9.   private:
  10.     std::vector<bool> build() const {
  11.       std::vector<bool> ret(256);
  12.       for(int i=0;i<sz;i++) ret[mem[i]] = 1;
  13.       return ret;
  14.     }
  15.   public:
  16.    
  17.     Set(char *str, int s) {
  18.       assign(str, s);
  19.     }
  20.  
  21.     Set():mem(0), sz(0) {}
  22.  
  23.     void assign(char *str, int s) {
  24.       sz = s;
  25.       mem = new char[sz];
  26.       for (int i=0;i<sz;i++) mem[i] = str[i];
  27.     }
  28.  
  29.     ~Set() {
  30.       delete[] mem;
  31.     }
  32.  
  33.     Set(const Set& ot):sz(ot.sz) {
  34.       mem = new char[sz];
  35.       for (int i=0;i<sz;i++) mem[i] = ot.mem[i];
  36.     }
  37.  
  38.     Set operator * (const Set& ot) {
  39.       std::vector<bool> s1 = build(), s2 = ot.build();
  40.       int col = 0;
  41.       for (int i=0;i<256;i++) if (s1[i] && s2[i]) col++;
  42.       char* mem = new char[col];
  43.       col = 0;
  44.       for (int i=0;i<256;i++) if (s1[i] && s2[i]) mem[col++] = i;
  45.       Set ret(mem, col);
  46.       delete[] mem;
  47.       return ret;
  48.     }
  49.  
  50.     Set operator + (const Set& ot) {
  51.       std::vector<bool> s1 = build(), s2 = ot.build();
  52.       int col = 0;
  53.       for (int i=0;i<256;i++) if (s1[i] || s2[i]) col++;
  54.       char* mem = new char[col];
  55.       col = 0;
  56.       for (int i=0;i<256;i++) if (s1[i] || s2[i]) mem[col++] = i;
  57.       Set ret(mem, col);
  58.       delete[] mem;
  59.       return ret;
  60.     }
  61.  
  62.     Set operator - (const Set& ot) {
  63.       std::vector<bool> s1 = build(), s2 = ot.build();
  64.       int col = 0;
  65.       for (int i=0;i<256;i++) if (s1[i] && !s2[i]) col++;
  66.       char* mem = new char[col];
  67.       col = 0;
  68.       for (int i=0;i<256;i++) if (s1[i] && !s2[i]) mem[col++] = i;
  69.       Set ret(mem, col);
  70.       delete[] mem;
  71.       return ret;
  72.     }
  73.  
  74.     bool operator == (const Set& ot) {
  75.       std::vector<bool> s1 = build(), s2 = ot.build();
  76.       bool ok = true;
  77.       for (int i=0;ok &&i<256;i++) if (s1[i] != s2[i]) ok = false;
  78.       return ok;
  79.     }
  80.  
  81.     bool operator < (const Set& ot) {
  82.       std::vector<bool> s1 = build(), s2 = ot.build();
  83.       bool ok = true;
  84.       for (int i=0;ok && i<256;i++) if (s1[i] && !s2[i]) ok = false;
  85.       return ok;
  86.     }
  87.  
  88.     friend std::ostream& operator <<(std::ostream &ot, const Set& a) {
  89.       for (int i=0;i<a.sz;i++) ot << a.mem[i];
  90.       return ot;
  91.     }
  92.  
  93.     friend std::istream& operator >>(std::istream &is, Set &a) {
  94.       char s[300];
  95.       is >> s;
  96.       a.assign(s, strlen(s));
  97.       return is;
  98.     }
  99.  
  100.     int size() const {
  101.       return sz;
  102.     }
  103.  
  104.   private:
  105.     char *mem;
  106.     int sz;
  107. };
  108.  
  109. int main() {
  110.   Set a,b;
  111.   std::cin >> a >> b;
  112.   Set inters = a * b;
  113.   std::cout << "Intersection: " << inters << "\n";
  114.   Set uni = a + b;
  115.   std::cout << "Union: " << uni << "\n";
  116.   Set diff = a - b;
  117.   std::cout << "Diff: " << diff << "\n";
  118.   bool is_equal = (a == b);
  119.   bool is_subset = (a < b);
  120.   std::cout << "Is equal: " << is_equal << "\n";
  121.   std::cout << "Is subset: " << is_subset << "\n";
  122.   return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment