Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <Windows.h>
- using namespace std;
- struct Animal
- {
- string form;
- string breed;
- string name;
- int yB;
- int cY;
- void Display()
- {
- cout << "Вид: " << form << ",Порода: " << breed << ", Ім'я тварини: " << name << ", Рік народження: " << yB << ".\n";
- }
- };
- void LoadStruct(Animal& a, fstream& s)
- {
- s.read((char*)& a, sizeof(a));
- }
- void SaveStruct(Animal& a, fstream& s)
- {
- int p = s.tellg();
- s.seekg(0, ios::end);
- s.write((char*)& a, sizeof(a));
- s.seekg(p, ios::beg);
- }
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- string path;
- cout << "Введіть шлях до//ім'я файлу : ";
- cin >> path;
- fstream file(path, ios::binary | ios::out | ios::in);
- if (!file.is_open())
- {
- cout << "Error opening file" << endl;
- exit(-1);
- }
- while (true)
- {
- cout << "1.Список тварин\n2.Cписок всіх тварин,відсортований по виду\n3.Cписок всіх тварин,молодше заданого віку\n4.Cписок всіх тварин заданого виду і породи\n5.Додати тварину\n0.Вихід\n\n";
- int select = -1;
- while (select > 5 || select < 0)
- {
- cout << "Введіть пункт меню : "; cin >> select;
- }
- Animal buff;
- string breed;
- string form;
- int years;
- switch (select)
- {
- case -1:
- {
- cout << "1.Список тварин\n2.Cписок всіх тварин,відсортований по виду\n3.Cписок всіх тварин,молодше заданого віку\n4.Cписок всіх тварин заданого виду і породи\n5.Додати тварину\n0.Вихід\n\n";
- int select = -1;
- while (select > 5 || select < 0)
- {
- cout << "Введіть пункт меню : "; cin >> select;
- }
- }
- case 1:
- {
- file.seekg(0, ios::beg);
- while (!file.eof())
- {
- LoadStruct(buff, file);
- buff.Display();
- buff = {};
- }
- select = -1;
- break;
- }
- case 3:
- {
- cout << "Ввеліть вік: ";
- cin >> years;
- file.seekg(0, ios::beg);
- while (!file.eof())
- {
- LoadStruct(buff, file);
- if (buff.cY < years)
- buff.Display();
- }
- select = -1;
- break;
- }
- case 2:
- {
- cout << "Введіть вид: ";
- cin >> breed;
- file.seekg(0, ios::beg);
- while (!file.eof())
- {
- LoadStruct(buff, file);
- if (buff.form == form)
- buff.Display();
- }
- select = -1;
- break;
- }
- case 4:
- {
- cout << "Введіть вид: ";
- getline(cin, form);
- cout << "Введіть породу: ";
- getline(cin, breed);
- file.seekg(0, ios::beg);
- while (!file.eof())
- {
- LoadStruct(buff, file);
- if (buff.form == form && buff.breed == breed)
- buff.Display();
- }
- select = -1;
- break;
- }
- case 5:
- {
- cout << "Вид тварини: ";
- cin.ignore();
- getline(cin, buff.form);
- cout << "введіть породу тварини: ";
- cin.ignore();
- getline(cin, buff.breed);
- cout << "Введіть ім'я тварини: ";
- getline(cin, buff.name);
- cout << "Введіть рік народження тварини: ";
- cin >> buff.yB;
- buff.cY = 2019 - buff.yB;
- SaveStruct(buff, file);
- select = -1;
- file << "\n";
- break;
- }
- case 0:
- {
- exit(0);
- break;
- }
- }
- select = -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement