Advertisement
Ansaid

Lab 4 SIAOD

Oct 27th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. struct Profile {
  9.     string INN;
  10.     string FIO;
  11.     string gender;
  12.     string dateOfBirth;
  13.  
  14.     friend int add_base(vector <Profile>& list);
  15.     friend void output_spisok(vector <Profile>& list, int SIZE);
  16.     friend void sort(vector <Profile>& list, int SIZE);
  17.     friend void search(vector <Profile>& list, int SIZE);
  18.     friend void inBinarFile(vector <Profile>& list, int SIZE);
  19.     friend void outBinarFile(vector <Profile>& list, int SIZE);
  20.  
  21. };
  22.  
  23. int main()
  24. {
  25.     setlocale(LC_ALL, "Russian");
  26.     vector <Profile> list; // "векторный" массив структур(товаров)
  27.     int choice;             // переменная выбора действий интерфейса
  28.     add_base(list);
  29.     output_spisok(list, list.size());
  30.     sort(list, list.size());
  31.     output_spisok(list, list.size());
  32.     search(list, list.size());
  33.     inBinarFile(list, list.size());
  34.     outBinarFile(list, list.size());
  35. }
  36.  
  37. int add_base(vector <Profile>& list)
  38. {
  39.     ifstream fin; // открываем поток считывания с файла
  40.     Profile buffer;
  41.  
  42.     fin.open("Names.txt"); // открываем файл в котором хранятся названия товаров
  43.     if (!fin.is_open())    // если файл не открыт, то его не существует
  44.     {
  45.         cout << "\nФайлы не существует!\n"; // выводим предупреждение
  46.  
  47.     }
  48.     else   // если файл "Names.txt" с названиями товаров открылся
  49.     {
  50.         int count = 0;
  51.         while (!fin.eof() && count != 50) // пока файл имен не закончится
  52.         {
  53.             getline(fin, buffer.FIO);
  54.             getline(fin, buffer.dateOfBirth); // считываем названия товаров/файлов
  55.             getline(fin, buffer.gender);
  56.             getline(fin, buffer.INN);
  57.             list.push_back(buffer);   // добавляем считанное название в новый элемент массива
  58.             count++;
  59.         }
  60.         fin.close(); // закрываем файл в котором хранятся названия товаров
  61.         cout << "\nДанные успешно считались!\n"; // выводим информацию об успехе
  62.     }
  63.  
  64.     //remove("Names.txt");
  65.  
  66.     return 0; //нормально отработало
  67. }
  68.  
  69. void output_spisok(vector <Profile>& list, int SIZE)
  70. {
  71.     cout << "\nСписок профилей:" << endl;
  72.     for (int i = 0; i < SIZE; i++)
  73.     {
  74.         cout << i + 1 << " профиль: " << endl;
  75.         cout << "ФИО: " << list[i].FIO<< endl;
  76.         cout << "Дата рождения: " << list[i].dateOfBirth<< endl;
  77.         cout << "Пол: " << list[i].gender << endl;
  78.         cout << "ИНН: " << list[i].INN << endl;
  79.         cout << endl;
  80.     }
  81. }
  82.  
  83. void sort(vector <Profile>& list, int SIZE)
  84. {
  85.     cout << "\nCортировка профилей по ФИО!\n";
  86.     for (int i = 0; i < list.size(); i++) {
  87.         for (int j = 0; j < list.size() - i - 1; j++) {
  88.             if (list[j].FIO.compare(list[j + 1].FIO) > 0) {
  89.                 swap(list[j], list[j + 1]);
  90.             }
  91.         }
  92.     }
  93. }
  94.  
  95. void search(vector <Profile>& list, int SIZE)
  96. {
  97.     string buffer;  // будет хранить название искомого товара
  98.     int k = 0;     //счетчик
  99.     cout << "\nВведите ИНН искомого человека: ";
  100.     getline(cin, buffer);        // вводим название искомого товара
  101.     cout << endl;
  102.     for (int i = 0; i < list.size(); i++) // в цикле сравниваем введенное название с названиями товаров
  103.     {
  104.         if (buffer == list[i].INN)        // если введенное название = названию товара, то выводим его
  105.         {
  106.             cout << i + 1 << " профиль: " << endl;
  107.             cout << "ФИО: " << list[i].FIO << endl;
  108.             cout << "Дата рождения: " << list[i].dateOfBirth << endl;
  109.             cout << "Пол: " << list[i].gender << endl;
  110.             cout << "ИНН: " << list[i].INN << endl;
  111.             cout << endl; // вывод информации о товаре
  112.             k++;                 // увеличиваем счетчик
  113.         }
  114.     }
  115.  
  116.     if (k == 0) // если счетчик остался 0, то выводим предупреждение
  117.         cout << "Товар с данным названием не найден!\n" << endl;
  118. }
  119.  
  120. void inBinarFile(vector <Profile>& list, int SIZE)
  121. {
  122.     cout << "Запись в бинарный файл!\n";
  123.     ofstream f;
  124.     f.open("Binary.dat", ios::binary);
  125.     for(int i = 0; i < SIZE; i++)
  126.         f.write(reinterpret_cast < char* > (&list[i]), sizeof(list[i]));
  127.     f.close();
  128.     cout << "Запись в бинарный файл прошла успешно!\n";
  129. }
  130.  
  131. void outBinarFile(vector <Profile>& list, int SIZE)
  132. {
  133.     cout << "\nЧтение бинарного файла!\n" << endl;
  134.     ifstream in;
  135.     in.open("Binary.dat", ios::binary);
  136.     for (int i = 0; i < SIZE; i++)
  137.     {
  138.         in.read(reinterpret_cast <char*> (&list[i]), sizeof(list[i]));
  139.         cout << i + 1 << " профиль: " << endl;
  140.         cout << "ФИО: " << list[i].FIO << endl;
  141.         cout << "Дата рождения: " << list[i].dateOfBirth << endl;
  142.         cout << "Пол: " << list[i].gender << endl;
  143.         cout << "ИНН: " << list[i].INN << endl;
  144.         cout << endl;
  145.     }
  146.     in.close();
  147.     cout << "Чтение с бинарного файла прошло успешно!\n";
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement