Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- using namespace std;
- struct Profile {
- string INN;
- string FIO;
- string gender;
- string dateOfBirth;
- friend int add_base(vector <Profile>& list);
- friend void output_spisok(vector <Profile>& list, int SIZE);
- friend void sort(vector <Profile>& list, int SIZE);
- friend void search(vector <Profile>& list, int SIZE);
- friend void inBinarFile(vector <Profile>& list, int SIZE);
- friend void outBinarFile(vector <Profile>& list, int SIZE);
- };
- int main()
- {
- setlocale(LC_ALL, "Russian");
- vector <Profile> list; // "векторный" массив структур(товаров)
- int choice; // переменная выбора действий интерфейса
- add_base(list);
- output_spisok(list, list.size());
- sort(list, list.size());
- output_spisok(list, list.size());
- search(list, list.size());
- inBinarFile(list, list.size());
- outBinarFile(list, list.size());
- }
- int add_base(vector <Profile>& list)
- {
- ifstream fin; // открываем поток считывания с файла
- Profile buffer;
- fin.open("Names.txt"); // открываем файл в котором хранятся названия товаров
- if (!fin.is_open()) // если файл не открыт, то его не существует
- {
- cout << "\nФайлы не существует!\n"; // выводим предупреждение
- }
- else // если файл "Names.txt" с названиями товаров открылся
- {
- int count = 0;
- while (!fin.eof() && count != 50) // пока файл имен не закончится
- {
- getline(fin, buffer.FIO);
- getline(fin, buffer.dateOfBirth); // считываем названия товаров/файлов
- getline(fin, buffer.gender);
- getline(fin, buffer.INN);
- list.push_back(buffer); // добавляем считанное название в новый элемент массива
- count++;
- }
- fin.close(); // закрываем файл в котором хранятся названия товаров
- cout << "\nДанные успешно считались!\n"; // выводим информацию об успехе
- }
- //remove("Names.txt");
- return 0; //нормально отработало
- }
- void output_spisok(vector <Profile>& list, int SIZE)
- {
- cout << "\nСписок профилей:" << endl;
- for (int i = 0; i < SIZE; i++)
- {
- cout << i + 1 << " профиль: " << endl;
- cout << "ФИО: " << list[i].FIO<< endl;
- cout << "Дата рождения: " << list[i].dateOfBirth<< endl;
- cout << "Пол: " << list[i].gender << endl;
- cout << "ИНН: " << list[i].INN << endl;
- cout << endl;
- }
- }
- void sort(vector <Profile>& list, int SIZE)
- {
- cout << "\nCортировка профилей по ФИО!\n";
- for (int i = 0; i < list.size(); i++) {
- for (int j = 0; j < list.size() - i - 1; j++) {
- if (list[j].FIO.compare(list[j + 1].FIO) > 0) {
- swap(list[j], list[j + 1]);
- }
- }
- }
- }
- void search(vector <Profile>& list, int SIZE)
- {
- string buffer; // будет хранить название искомого товара
- int k = 0; //счетчик
- cout << "\nВведите ИНН искомого человека: ";
- getline(cin, buffer); // вводим название искомого товара
- cout << endl;
- for (int i = 0; i < list.size(); i++) // в цикле сравниваем введенное название с названиями товаров
- {
- if (buffer == list[i].INN) // если введенное название = названию товара, то выводим его
- {
- cout << i + 1 << " профиль: " << endl;
- cout << "ФИО: " << list[i].FIO << endl;
- cout << "Дата рождения: " << list[i].dateOfBirth << endl;
- cout << "Пол: " << list[i].gender << endl;
- cout << "ИНН: " << list[i].INN << endl;
- cout << endl; // вывод информации о товаре
- k++; // увеличиваем счетчик
- }
- }
- if (k == 0) // если счетчик остался 0, то выводим предупреждение
- cout << "Товар с данным названием не найден!\n" << endl;
- }
- void inBinarFile(vector <Profile>& list, int SIZE)
- {
- cout << "Запись в бинарный файл!\n";
- ofstream f;
- f.open("Binary.dat", ios::binary);
- for(int i = 0; i < SIZE; i++)
- f.write(reinterpret_cast < char* > (&list[i]), sizeof(list[i]));
- f.close();
- cout << "Запись в бинарный файл прошла успешно!\n";
- }
- void outBinarFile(vector <Profile>& list, int SIZE)
- {
- cout << "\nЧтение бинарного файла!\n" << endl;
- ifstream in;
- in.open("Binary.dat", ios::binary);
- for (int i = 0; i < SIZE; i++)
- {
- in.read(reinterpret_cast <char*> (&list[i]), sizeof(list[i]));
- cout << i + 1 << " профиль: " << endl;
- cout << "ФИО: " << list[i].FIO << endl;
- cout << "Дата рождения: " << list[i].dateOfBirth << endl;
- cout << "Пол: " << list[i].gender << endl;
- cout << "ИНН: " << list[i].INN << endl;
- cout << endl;
- }
- in.close();
- cout << "Чтение с бинарного файла прошло успешно!\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement