Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.85 KB | None | 0 0
  1. class Multime
  2. {
  3.     Multime();
  4.     Multime(const Multime & other);
  5.     Multime(std::initializer_list<int> other);
  6.     ~Multime();
  7.     Multime& operator=(const Multime & other);
  8.  
  9.     void multime();
  10.     Multime reuniune(const Multime & rhs) const;
  11.     Multime intersectie(const Multime & rhs) const;
  12.     Multime diferenta(const Multime & rhs) const;
  13.    
  14.     void insert(const int & value);
  15.    
  16.     int & operator[](size_t pos);
  17.     const int & operator[](size_t pos) const;
  18.  
  19.     friend istream& operator<<(istream & file, Multime & obj);
  20.     friend ostream& operator>>(ostream & file, Multime & obj);
  21.  
  22. private:
  23.     int n;
  24.     int * a;
  25. };
  26.  
  27. Multime::Multime()
  28. {
  29.     n = 0;
  30. }
  31.  
  32. Multime::~Multime()
  33. {
  34.     if (a != nullptr)
  35.     {
  36.         delete[] a;
  37.     }
  38. }
  39.  
  40. Multime::Multime(const Multime & other)
  41. {
  42.     n = other.n;
  43.     a = new int[n];
  44.  
  45.     for (int i=0; i<n; ++i)
  46.     {
  47.         a[i] = other.a[i];
  48.     }
  49. }
  50.  
  51. Multime::Multime(std::initializer_list<int> other)
  52. {
  53.     n = other.size();
  54.     a = new int(n)
  55.     int i=0;
  56.     for (auto it=other.begin(); it!=other.end(); ++it, ++i)
  57.     {
  58.         a[i] = *it;
  59.     }
  60. }
  61.  
  62. Multime& Multime::operator=(const Multime & other)
  63. {
  64.     if (a != nullptr)
  65.     {
  66.         delete[] a;
  67.     }
  68.  
  69.     n = other.n;
  70.     a = new int[n];
  71.  
  72.     for (int i=0; i<n; ++i)
  73.     {
  74.         a[i] = other.a[i];
  75.     }
  76.  
  77.     return *this;
  78. }
  79.  
  80. void Multime::multime()
  81. {
  82.     for(int i=0; i<n; ++i)
  83.     {
  84.         for(int j=i+1; j<n; )
  85.         {
  86.             if(a[i] == a[j])
  87.             {
  88.                 for(int k=j; k<n-1; ++k)
  89.                 {
  90.                     a[k] = a[k+1];
  91.                 }
  92.  
  93.                 --n;
  94.             }
  95.             else
  96.             {
  97.                 ++j;
  98.             }
  99.         }
  100.     }
  101.  
  102.     for(int i=0; i<n-1; i++)
  103.     {
  104.         for(int j=i+1; j<n; j++)
  105.         {
  106.             if(a[i] > a[j])
  107.             {
  108.                 swap(a[i], a[j]);
  109.             }
  110.         }
  111.     }
  112.  
  113.     int * tmp = new int[n];
  114.     for (int i=0; i<n; i++)
  115.     {
  116.         tmp[i] = a[i];
  117.     }
  118.     delete[] a;
  119.     a = tmp;
  120.  
  121.     // cout << "\n";
  122.     // for(int i=0; i<n; ++i)
  123.     // {
  124.     //     cout<< a[i] <<" ";
  125.     // }
  126. }
  127. Multime Multime::reuniune(const Multime & rhs) const
  128. {
  129.     const Multime & lhs = *this;
  130.  
  131.     Multime answer;
  132.     answer.n = lhs.n + rhs.n;
  133.     answer.a = new int[answer.n];
  134.  
  135.     for (int i=0; i<lhs.n; ++i)
  136.     {
  137.         answer.a[i] = lhs.a[i];
  138.     }
  139.     for (int i=0; i<rhs.n; ++i)
  140.     {
  141.         answer.a[lhs.n+i] = rhs.a[i];
  142.     }
  143.  
  144.     answer.multime();
  145.     return answer;
  146. }
  147.  
  148. Multime Multime::intersectie(const Multime & rhs) const
  149. {
  150.     const Multime & lhs = *this;
  151.  
  152.     Multime answer;
  153.     answer.n = max(lhs.n, rhs.n);
  154.     answer.a = new int[answer.n];
  155.  
  156.     int i=0,j=0;
  157.     answer.n = 0;
  158.     while(i<lhs.n && j<rhs.n)
  159.     {
  160.         if(lhs.a[i] < rhs.a[j])
  161.         {
  162.            i++;
  163.         }
  164.         else if(rhs.a[i] < lhs.a[j])
  165.         {
  166.            j++;
  167.         }
  168.         else
  169.         {
  170.             answer.a[answer.n++] = rhs.a[j++];
  171.             i++;
  172.         }
  173.     }
  174.  
  175.     int * tmp = new int[answer.n];
  176.     for (int i=0; i<answer.n; ++i)
  177.     {
  178.         tmp[i] = answer.a[i];
  179.     }
  180.  
  181.     delete[] answer.a;
  182.     answer.a = tmp;
  183.  
  184.     return answer;
  185. }
  186.  
  187. Multime Multime::diferenta(const Multime & rhs) const
  188. {
  189.     const Multime & lhs = *this;
  190.     Multime answer;
  191.     answer.n = lhs.n;
  192.     answer.a = new int[answer.n];
  193.     answer.n = 0;
  194.     int i,j;
  195.     for (i=0; i<lhs.n; i++)
  196.     {
  197.         ok=0;
  198.         j=0;
  199.         while (j < rhs.n)
  200.         {
  201.             if(lhs.a[i] == rhs.a[j])
  202.             {
  203.                 ok=1;
  204.             }
  205.             else
  206.             {
  207.                 j++;
  208.             }
  209.         }
  210.         if (ok == 0)
  211.         {
  212.             answer.a[answer.n++] = lhs.a[i];
  213.         }
  214.     }
  215.    
  216.     int * tmp = new int[answer.n];
  217.     for (int i=0; i<answer.n; ++i)
  218.     {
  219.         tmp[i] = answer.a[i];
  220.     }
  221.  
  222.     delete[] answer.a;
  223.     answer.a = tmp;
  224.  
  225. }
  226.  
  227. void Multime::insert(int value)
  228. {
  229.     int * tmp = new int[n+1];
  230.     for (int i=0; i<n; ++i)
  231.     {
  232.         tmp[i] = a[i];
  233.     }
  234.    
  235.     tmp[n++] = value;
  236.  
  237.     delete[] a;
  238.     a = tmp;
  239. }
  240.  
  241. int & Multime::operator[](size_t pos)
  242. {
  243.     return a[i];
  244. }
  245.  
  246. const int & Multime::operator[](size_t pos) const
  247. {
  248.     return a[i];
  249. }
  250.  
  251. istream& operator<<(istream & file, Multime & obj)
  252. {
  253.     cin>>obj.n;
  254.  
  255.     if (obj.a != nullptr)
  256.     {
  257.         delete[] obj.a;
  258.     }
  259.  
  260.     obj.a = new int[obj.n];
  261.  
  262.     for(int i=0; i<obj.n; i++)
  263.     {
  264.         cin >> obj.a[i];
  265.     }
  266.  
  267.     return file;
  268. }
  269.  
  270. ostream& operator<<(ostream & file, Multime & obj)
  271. {
  272.     file << obj.n << ": ";
  273.  
  274.     for(int i=0; i<obj.n; i++)
  275.     {
  276.         cout << obj.a[i];
  277.     }
  278.  
  279.     return file;
  280.  
  281.  
  282. int main()
  283. {
  284.     Multime a, b;
  285.     cin >> a;
  286.     cin >> b;
  287.    
  288.     Multime reuniune = a.reuniune(b):
  289.     Multime intersectie = a.intersectie(b);
  290.     Multime diferenta = a.diferenta(b);
  291.    
  292.     cout << reuniune << endl;
  293.     cout << intersectie << endl;
  294.     cout << diferenta << endl;
  295.    
  296.     return 0;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement