Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // syrykh_5.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- #include "pch.h"
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- using namespace std;
- /*
- написать условие задачи
- разбить на модули
- переписать удаление
- найти самый популярный цвет для каждой марки
- */
- const int SIZE = 250;
- struct CarInfo
- {
- char brand[SIZE];
- char color[SIZE];
- char fullName[SIZE];
- char licensePlate[SIZE];
- };
- int Input(int lowBorder, int highBorder)
- {
- int input;
- while (!(cin >> input && input >= lowBorder && input < highBorder))
- {
- cout << "\nВвод некорректен. Повторите ввод: ";
- cin.clear();
- while (cin.get() != '\n');
- }
- while (cin.get() != '\n');
- return input;
- }
- int numberOfRecords(char* direction)
- {
- CarInfo item;
- ifstream file;
- file.open(direction);
- //cout << file.is_open() << " " << sizeof(item);
- file.seekg(0, ios::end);
- int result = file.tellg() / sizeof(item);
- cout << result;
- file.close();
- return result;
- }
- void clearStream()
- {
- cin.clear();
- while (cin.get() != '\n');
- }
- void showRecords(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]));
- 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";
- }
- file.close();
- }
- 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++)
- {
- clearStream();
- 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();
- showRecords(direction);
- }
- //
- void deleteRecord(char* direction)
- {
- showRecords(direction);
- int countOfRecords = numberOfRecords(direction), n,k=0;
- vector<CarInfo> arrayOfRecords(countOfRecords);
- vector <CarInfo> arrayWithoutRecord(countOfRecords - 1);
- cout << "Введите номер записи для удаления: "; n=Input(0,countOfRecords+1);
- fstream file(direction, ios::binary | ios::in);
- for (int i = 0; i < countOfRecords; i++)
- file.read(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
- for (int i = 0; i < countOfRecords - 1; i++)
- {
- if (i != n - 1)
- {
- arrayWithoutRecord.push_back(arrayOfRecords[i]);
- //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*>(&arrayWithoutRecord[i]), sizeof(arrayWithoutRecord[i]));
- file.close();
- showRecords(direction);
- }
- void editRecord(char* direction)
- {
- showRecords(direction);
- int n, nField, k = 0; char temp[SIZE];
- int countOfRecords = numberOfRecords(direction);
- vector<CarInfo> arrayOfRecords(countOfRecords);
- fstream file(direction, ios::binary | ios::in | ios::out);
- for (int i = 0; i < countOfRecords; i++)
- file.read(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
- 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:
- {
- clearStream();
- cout << "brand "; cin.getline(arrayOfRecords[n - 1].brand, SIZE, '\n');
- break;
- }
- case 2:
- {
- clearStream();
- cout << "color "; cin.getline(arrayOfRecords[n - 1].color, SIZE, '\n');
- break;
- }
- case 3:
- {
- clearStream();
- cout << "full name "; cin.getline(arrayOfRecords[n - 1].fullName, SIZE, '\n');
- break;
- }
- case 4:
- {
- clearStream();
- cout << "license plate "; cin.getline(arrayOfRecords[n - 1].licensePlate, SIZE, '\n');
- break;
- }
- default:
- cout << "Введён неверный номер поля";
- break;
- }
- file.seekg(0);
- for (int i = 0; i < countOfRecords; i++)
- file.write(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
- file.close();
- showRecords(direction);
- }
- int main()
- {
- setlocale(0, ""); char repeat;
- char *direction = new char[SIZE];
- int nOperation = 0;
- cout << "Укажите абсолютный путь к файлу: ";
- cin.get(direction, SIZE);
- do
- {
- fstream file(direction, ios::binary | ios::in);
- if (file.is_open())
- {
- file.close();
- cout << "1 - показать все записи \n2 - добавить записи\n3 - редактировать запись \n4 - удалить запись\n";
- nOperation = Input(1, 5);
- switch (nOperation)
- {
- case 1:
- {
- showRecords(direction);
- break;
- }
- case 2:
- {
- addRecordToBack(direction);
- break;
- }
- case 3:
- {
- editRecord(direction);
- break;
- }
- case 4:
- {
- deleteRecord(direction);
- break;
- }
- }
- }
- else
- {
- cout << "Указан неверный путь к файлу.\n";
- clearStream();
- }
- cout << "Для завершения введите n\n";
- } while (cin >> repeat&& repeat != 'n');
- delete[] direction;
- return 0;
- //C:\onlyformydoggers\file with info.dat
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement