Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include<string>
- #include <algorithm>
- #include <interface.h>
- using namespace std;
- const int SIZE = 250;
- struct CarBrand
- {
- string brand;
- vector<string> colors;
- };
- struct CarInfo
- {
- char brand[SIZE];
- char color[SIZE];
- char fullName[SIZE];
- char licensePlate[SIZE];
- };
- int numberOfRecords(char* direction)
- {
- CarInfo item;
- ifstream file;
- file.open(direction);
- file.seekg(0, ios::end);
- int result = file.tellg() / sizeof(item);
- file.close();
- return result;
- }
- vector<CarInfo> getAllRecordsFromFile(char* direction)
- {
- int countOfRecords = numberOfRecords(direction);
- vector<CarInfo> arrayOfRecords(countOfRecords);
- fstream file(direction, ios::binary | ios::in);
- for (int i = 0; i < countOfRecords; i++)
- file.read(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
- file.close();
- return arrayOfRecords;
- }
- void whichColor(char* direction)
- {
- vector <string> brands;
- vector<CarInfo> car1 = getAllRecordsFromFile(direction);
- for (int j = 0; j < car1.size(); j++)
- if(find(brands.begin(),brands.end(), car1[j].brand)==brands.end()) // если элемент отсутствует в векторе (==), присутствует(!=)
- brands.push_back(car1[j].brand);
- vector<CarBrand> car(brands.size());
- for (int i = 0; i < car.size(); i++)
- {
- car[i].brand = brands[i];
- for (int j = 0; j < car1.size(); j++)
- {
- if (car1[j].brand == brands[i])
- car[i].colors.push_back(car1[j].color);
- }
- //sort(car[i].colors.begin(), car[i].colors.end());
- }
- for (int i = 0; i < car.size(); i++)
- {
- cout << car[i].brand << "\n";
- for (int j = 0; j < car[i].colors.size(); j++)
- cout << car[i].colors[j] << "\n";
- }
- int count=0, maxcount=1;
- vector<string> listOfColors;
- for (int k = 0; k < car.size(); k++)
- {
- listOfColors.clear();
- for (int i = 0; i < car[k].colors.size(); i++) // Перебираем все элементы массива.
- {
- count = 0; // Счетчик в 0.
- for (int j = i; j < car[k].colors.size(); j++) // Перебираем все элементы от i до конца.
- if (car[k].colors[i] == car[k].colors[j]) // Если элемент [i] совпадает с одним из последующих [j],
- count++;
- if (count == maxcount) // Если очередная буква встречается maxcount раз,
- listOfColors.push_back(car[k].colors[i]); // То занесём её в список.
- if (count > maxcount) // Если число больше максимального,
- {
- maxcount = count; // тогда оно максимальное.
- listOfColors.clear();
- listOfColors.push_back(car[k].colors[i]);
- }
- }
- cout << car[k].brand << " ";
- for (int i = 0; i < listOfColors.size(); i++)
- cout << listOfColors[i] << " ";
- cout << endl;
- }
- }
- void showRecords(char* direction)
- {
- int countOfRecords = numberOfRecords(direction);
- vector<CarInfo> arrayOfRecords = getAllRecordsFromFile(direction);
- for (int i = 0; i < countOfRecords; i++)
- {
- cout << "\n" << i + 1 << " item\n";
- cout << "brand " << arrayOfRecords[i].brand;
- cout << "\ncolor " << arrayOfRecords[i].color;
- cout << "\nfull name " << arrayOfRecords[i].fullName;
- cout << "\nlicense plate " << arrayOfRecords[i].licensePlate << "\n\n";
- }
- }
- void addRecordToBack(char* direction)
- {
- ofstream file;
- CarInfo item;
- int n = 0;
- cout << "Введите кол-во новых записей: "; n = Input(0, 10);
- file.open(direction, ios::binary | ios::out | ios::app);
- for (int i = 0; i < n; i++)
- {
- cin.clear();
- cout << i + 1 << " item\n";
- cout << "brand "; cin.getline(item.brand, SIZE, '\n');
- cout << "color "; cin.getline(item.color, SIZE, '\n');
- cout << "full name "; cin.getline(item.fullName, SIZE, '\n');
- cout << "license plate "; cin.getline(item.licensePlate, SIZE, '\n');
- file.write(reinterpret_cast<char*>(&item), sizeof(item));
- }
- file.close();
- }
- //
- void deleteRecord(char* direction)
- {
- int countOfRecords = numberOfRecords(direction), n, k = 0;
- vector<CarInfo> arrayOfRecords(countOfRecords - 1);
- cout << "Введите номер записи для удаления: "; n = Input(0, countOfRecords + 1);
- fstream file(direction, ios::binary | ios::in);
- for (int i = 0; i < countOfRecords; i++)
- {
- if (i != n - 1)
- {
- file.read(reinterpret_cast<char*>(&arrayOfRecords[k]), sizeof(arrayOfRecords[k]));
- k++;
- }
- }
- file.close();
- file.open(direction, ios::binary | ios::out);
- file.close();
- file.open(direction, ios::binary | ios::out);
- for (int i = 0; i < countOfRecords - 1; i++)
- file.write(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
- file.close();
- }
- void editRecord(char* direction)
- {
- int n, nField, k = 0;
- int countOfRecords = numberOfRecords(direction);
- vector<CarInfo> arrayOfRecords = getAllRecordsFromFile(direction);
- cout << "Кол-во записей: " << countOfRecords << "\n";
- cout << "Введите номер записи для редактирования: "; n = Input(0, countOfRecords + 1);
- cout << "\n 1 - brand, 2 - color, 3 - full name, 4 - license plate\n";
- cout << "Введите номер поля для редактирования: "; nField = Input(1, 5);
- switch (nField)
- {
- case 1:
- {
- cin.clear();
- cout << "brand "; cin.getline(arrayOfRecords[n - 1].brand, SIZE, '\n');
- break;
- }
- case 2:
- {
- cin.clear();
- cout << "color "; cin.getline(arrayOfRecords[n - 1].color, SIZE, '\n');
- break;
- }
- case 3:
- {
- cin.clear();
- cout << "full name "; cin.getline(arrayOfRecords[n - 1].fullName, SIZE, '\n');
- break;
- }
- case 4:
- {
- cin.clear();
- cout << "license plate "; cin.getline(arrayOfRecords[n - 1].licensePlate, SIZE, '\n');
- break;
- }
- }
- fstream file(direction, ios::binary | ios::out);
- for (int i = 0; i < countOfRecords; i++)
- file.write(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
- file.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment