Advertisement
Infiniti_Inter

olya classs

Dec 26th, 2019
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. ifstream in("input.txt");
  8. ofstream out("output.txt");
  9.  
  10. string IntToString(int a)
  11. {
  12.     string res = "";
  13.     while (a)
  14.     {
  15.         res += a % 10 + '0';
  16.         a /= 10;
  17.     }
  18.     reverse(res.begin(), res.end());
  19.     return res.length() > 0 ? res : "0";
  20. }
  21. string Type(int t)
  22. {
  23.     return t ? "Slat" : "Sweet";
  24. }
  25.  
  26. class Cookie
  27. {
  28. private:
  29.     string name;
  30.     int weight;
  31.     int type = 0;//0 - сладкое
  32.     string producer;
  33.     static int count1;
  34.     static int count2;
  35.  
  36. public:
  37.     Cookie() {};
  38.     Cookie(string name, int weight, string producer, int type = 0)
  39.     {
  40.         this->name = name;
  41.         this->weight = weight;
  42.         this->producer = producer;
  43.         this->type = type;
  44.         if (type == 1)
  45.             count2++;
  46.         else
  47.             count1++;
  48.     }
  49.     void setName(string name) { this->name = name; }
  50.     void setWeight(int weight) { this->weight = weight; }
  51.     void setType(int type)
  52.     {
  53.         if (this->type == 0)
  54.             count1--;
  55.         else
  56.             count2--;
  57.         if (type == 0)
  58.             count1++;
  59.         else
  60.             count2++;
  61.         this->type = type;
  62.     }
  63.     void setProducer(string producer) { this->producer = producer; }
  64.     string ToPrint()
  65.     {
  66.         return name + " " + IntToString(weight) + " " + producer + " " + Type(type) + "\n";
  67.     }
  68.  
  69.     bool operator !=(Cookie object)
  70.     {
  71.         if (this->producer != object.producer)
  72.             return true;
  73.         if (this->type != object.type)
  74.             return true;
  75.         if (this->name != object.name)
  76.             return true;
  77.         if (this->weight != object.weight)
  78.             return true;
  79.         return false;
  80.     }
  81.  
  82.     bool operator <(Cookie object)
  83.     {
  84.         if (this->producer < object.producer)
  85.             return true;
  86.         if (this->producer == object.producer && this->type > object.type)
  87.             return true;
  88.         if (this->producer == object.producer && this->type == object.type &&
  89.             this->name < object.name)
  90.             return true;
  91.         if (this->producer == object.producer && this->type == object.type &&
  92.             this->name == object.name && this->weight > object.weight)
  93.             return true;
  94.         return false;
  95.     }
  96.     static string Count()
  97.     {
  98.         return "Sweet : " + IntToString(count1) + "\nSalt : " + IntToString(count2) + "\n";
  99.     }
  100.  
  101. };
  102. int Cookie::count1 = 0;
  103. int Cookie::count2 = 0;
  104.  
  105.  
  106. int main()
  107. {
  108.    
  109.     //массив из 5 элементов класса
  110.     int n = 5;
  111.     Cookie * a = new Cookie[5];
  112.     for (int i = 0; i < n; ++i)
  113.     {
  114.         string name, prod;
  115.         int w, t;
  116.         in >> name >> w >> prod >> t;
  117.         a[i] = Cookie(name, w, prod, t);
  118.     }
  119.     out << Cookie::Count();
  120.  
  121.  
  122.     // соритровка, проверяем оператор <
  123.     for (int i = 0; i < n; ++i)
  124.         for (int j = n - 1; j > i; --j)
  125.             if (a[j] < a[j - 1])
  126.             {
  127.                 Cookie temp = a[j];
  128.                 a[j] = a[j - 1];
  129.                 a[j - 1] = temp;
  130.             }
  131.  
  132.     for (int i = 0; i < n; ++i)
  133.         out << a[i].ToPrint();
  134.     out << "\n";
  135.  
  136.     Cookie b = Cookie("Drow", 222, "Anger");
  137.     Cookie c = Cookie("Drow", 444, "Anger");
  138.     out << (b != c ? "true" : "false") << "\n";
  139.     out << "\n" << Cookie::Count() << "\n";
  140.     b.setType(1);
  141.     c.setType(1);
  142.     c.setWeight(222);
  143.     out << (b != c ? "true" : "false") << "\n";
  144.    
  145.     out << "\n" << Cookie::Count() << "\n";
  146.     /*
  147. Hleo 500 Forest 1
  148. Lego 230 QweenP 0
  149. DrowF 222 Anger 1
  150. LoGo 1999 Soft 0
  151. Drefer 124 Slada 1
  152. */
  153.    
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement