Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 3.49 KB | None | 0 0
  1.  
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <Windows.h>
  6.  
  7. using namespace std;
  8. struct Animal
  9. {
  10.     string form;
  11.     string breed;
  12.     string name;
  13.     int yB;
  14.     int cY;
  15.  
  16.     void Display()
  17.     {
  18.         cout << "Вид: " << form << ",Порода: "  << breed << ", Ім'я тварини: " << name << ", Рік народження: " << yB << ".\n";
  19.     }
  20. };
  21.  
  22. void LoadStruct(Animal& a, fstream& s)
  23. {
  24.     s.read((char*)& a, sizeof(a));
  25. }
  26. void SaveStruct(Animal& a, fstream& s)
  27. {
  28.     int p = s.tellg();
  29.     s.seekg(0, ios::end);
  30.     s.write((char*)& a, sizeof(a));
  31.     s.seekg(p, ios::beg);
  32. }
  33. int main()
  34. {
  35.     SetConsoleCP(1251);
  36.     SetConsoleOutputCP(1251);
  37.  
  38.    
  39.     string path;
  40.     cout << "Введіть шлях до//ім'я файлу : ";
  41.     cin >> path;
  42.  
  43.     fstream file(path, ios::binary | ios::out | ios::in);
  44.     if (!file.is_open())
  45.     {
  46.         cout << "Error opening file" << endl;
  47.         exit(-1);
  48.     }
  49.     while (true)
  50.     {
  51.  
  52.         cout << "1.Список тварин\n2.Cписок всіх тварин,відсортований по виду\n3.Cписок всіх тварин,молодше заданого віку\n4.Cписок всіх тварин заданого виду і породи\n5.Додати тварину\n0.Вихід\n\n";
  53.         int select = -1;
  54.         while (select > 5 || select < 0)
  55.         {
  56.             cout << "Введіть пункт меню : "; cin >> select;
  57.         }
  58.         Animal buff;
  59.         string breed;
  60.         string form;
  61.         int years;
  62.         switch (select)
  63.         {
  64.         case -1:
  65.         {
  66.             cout << "1.Список тварин\n2.Cписок всіх тварин,відсортований по виду\n3.Cписок всіх тварин,молодше заданого віку\n4.Cписок всіх тварин заданого виду і породи\n5.Додати тварину\n0.Вихід\n\n";
  67.             int select = -1;
  68.             while (select > 5 || select < 0)
  69.             {
  70.                 cout << "Введіть пункт меню : "; cin >> select;
  71.             }
  72.         }
  73.         case 1:
  74.         {
  75.             file.seekg(0, ios::beg);
  76.             while (!file.eof())
  77.             {
  78.                 LoadStruct(buff, file);
  79.                 buff.Display();
  80.                 buff = {};
  81.  
  82.             }
  83.             select = -1;
  84.             break;
  85.         }
  86.         case 3:
  87.         {
  88.             cout << "Ввеліть вік: ";
  89.             cin >> years;
  90.             file.seekg(0, ios::beg);
  91.             while (!file.eof())
  92.             {
  93.                 LoadStruct(buff, file);
  94.                 if (buff.cY < years)
  95.                     buff.Display();
  96.  
  97.             }
  98.             select = -1;
  99.             break;
  100.         }
  101.         case 2:
  102.         {
  103.             cout << "Введіть вид: ";
  104.             cin >> breed;
  105.             file.seekg(0, ios::beg);
  106.             while (!file.eof())
  107.             {
  108.                 LoadStruct(buff, file);
  109.                 if (buff.form == form)
  110.                     buff.Display();
  111.             }
  112.             select = -1;
  113.             break;
  114.         }
  115.         case 4:
  116.         {
  117.             cout << "Введіть вид: ";
  118.             getline(cin, form);
  119.             cout << "Введіть породу: ";
  120.             getline(cin, breed);
  121.             file.seekg(0, ios::beg);
  122.             while (!file.eof())
  123.             {
  124.                 LoadStruct(buff, file);
  125.                 if (buff.form == form && buff.breed == breed)
  126.                     buff.Display();
  127.             }
  128.             select = -1;
  129.             break;
  130.         }
  131.         case 5:
  132.         {
  133.             cout << "Вид тварини: ";
  134.             cin.ignore();
  135.             getline(cin, buff.form);
  136.             cout << "введіть породу тварини: ";
  137.             cin.ignore();
  138.             getline(cin, buff.breed);
  139.             cout << "Введіть ім'я тварини: ";
  140.             getline(cin, buff.name);
  141.             cout << "Введіть рік народження тварини: ";
  142.             cin >> buff.yB;
  143.             buff.cY = 2019 - buff.yB;
  144.             SaveStruct(buff, file);
  145.             select = -1;
  146.             file << "\n";
  147.             break;
  148.         }
  149.         case 0:
  150.         {
  151.             exit(0);
  152.             break;
  153.         }
  154.         }
  155.         select = -1;
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement