Advertisement
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 Students
- {
- char fullname[SIZE];
- int course;
- int group;
- char label[SIZE];
- };
- int numberOfRecords(char* direction)
- {
- Students item;
- ifstream file;
- file.open(direction);
- file.seekg(0, ios::end);
- int result = file.tellg() / sizeof(item);
- file.close();
- return result;
- }
- vector<Students> getAllRecordsFromFile(char* direction)
- {
- int countOfRecords = numberOfRecords(direction);
- vector<Students> records(countOfRecords);
- fstream file(direction, ios::binary | ios::in);
- for (int i = 0; i < countOfRecords; i++)
- file.read(reinterpret_cast<char*>(&records[i]), sizeof(records[i]));
- file.close();
- return records;
- }
- void transfer(char* direction)
- {
- int k = 0;
- vector<Students> records = getAllRecordsFromFile(direction);
- vector<Students> transfer(records.size());
- for (int i = 0; i < records.size(); i++)
- {
- if (!strcmp(records[i].label, "b") && records[i].course < 4)
- {
- records[i].course++;
- transfer[k] = records[i];
- k++;
- }
- if (!strcmp(records[i].label, "s") && records[i].course < 5)
- {
- records[i].course++;
- transfer[k] = records[i];
- k++;
- }
- if (!strcmp(records[i].label, "m") && records[i].course < 2)
- {
- records[i].course++;
- transfer[k] = records[i];
- k++;
- }
- }
- fstream file(direction, ios::binary | ios::in);
- file.close();
- file.open(direction, ios::binary | ios::out);
- for (int i = 0; i < k; i++)
- file.write(reinterpret_cast<char*>(&transfer[i]), sizeof(transfer[i]));
- file.close();
- }
- void showRecords(char* direction)
- {
- int countOfRecords = numberOfRecords(direction);
- vector<Students> records = getAllRecordsFromFile(direction);
- if (!records.empty())
- {
- for (int i = 0; i < countOfRecords; i++)
- {
- cout << "\n" << i + 1 << " студент\n";
- cout << "fullname " << records[i].fullname;
- cout << "\ncourse " << records[i].course;
- cout << "\ngroup " << records[i].group;
- cout << "\nlabel " << records[i].label << "\n\n";
- }
- }
- else cout << "записей нет\n";
- }
- void addRecordToBack(char* direction)
- {
- ofstream file;
- Students 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 << " студент\n";
- cout << "fullname "; cin.getline(item.fullname, SIZE, '\n');
- cout << "course "; item.course=Input(1,SIZE);
- cout << "group "; item.group=Input(1,SIZE);
- cin.clear();
- cout << "label "; cin.getline(item.label, SIZE, '\n');
- file.write(reinterpret_cast<char*>(&item), sizeof(item));
- }
- file.close();
- }
- //
- void deleteRecord(char* direction)
- {
- int countOfRecords = numberOfRecords(direction), n, k = 0;
- vector<Students> 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<Students> arrayOfRecords = getAllRecordsFromFile(direction);
- cout << "Кол-во записей: " << countOfRecords << "\n";
- cout << "Введите номер записи для редактирования: "; n = Input(0, countOfRecords + 1);
- cout << "\n 1 - fullname, 2 - course, 3 - group, 4 - label\n";
- cout << "Введите номер поля для редактирования: "; nField = Input(1, 5);
- switch (nField)
- {
- case 1:
- {
- cin.clear();
- cout << "fullname "; cin.getline(arrayOfRecords[n - 1].fullname, SIZE, '\n');
- break;
- }
- case 2:
- {
- cin.clear();
- cout << "course "; cin >> arrayOfRecords[n - 1].course; cout << endl;
- break;
- }
- case 3:
- {
- cin.clear();
- cout << "group "; cin >> arrayOfRecords[n - 1].group; cout << endl;
- break;
- }
- case 4:
- {
- cin.clear();
- cout << "label "; cin.getline(arrayOfRecords[n - 1].label, 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
Advertisement