Advertisement
SkeptaProgrammer

Untitled

Jun 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <fstream>
  5. #include<string>
  6. #include <algorithm>
  7. #include "interface.h"
  8. using namespace std;
  9.  
  10. const int SIZE = 250;
  11.  
  12. struct Students
  13. {
  14.     char fullname[SIZE];
  15.     int course;
  16.     int group;
  17.     char label[SIZE];
  18. };
  19.  
  20.  
  21.  
  22. int numberOfRecords(char* direction)
  23. {
  24.     Students item;
  25.     ifstream file;
  26.     file.open(direction);
  27.     file.seekg(0, ios::end);
  28.     int result = file.tellg() / sizeof(item);
  29.     file.close();
  30.     return result;
  31. }
  32.  
  33. vector<Students> getAllRecordsFromFile(char* direction)
  34. {
  35.     int countOfRecords = numberOfRecords(direction);
  36.     vector<Students> records(countOfRecords);
  37.     fstream file(direction, ios::binary | ios::in);
  38.     for (int i = 0; i < countOfRecords; i++)
  39.         file.read(reinterpret_cast<char*>(&records[i]), sizeof(records[i]));
  40.     file.close();
  41.     return records;
  42. }
  43.  
  44. void transfer(char* direction)
  45. {
  46.     int k = 0;
  47.     vector<Students> records = getAllRecordsFromFile(direction);
  48.     vector<Students> transfer(records.size());
  49.     for (int i = 0; i < records.size(); i++)
  50.     {
  51.         if (!strcmp(records[i].label, "b") && records[i].course < 4)
  52.         {
  53.             records[i].course++;
  54.             transfer[k] = records[i];
  55.             k++;
  56.         }
  57.         if (!strcmp(records[i].label, "s") && records[i].course < 5)
  58.         {
  59.             records[i].course++;
  60.             transfer[k] = records[i];
  61.             k++;
  62.         }
  63.         if (!strcmp(records[i].label, "m") && records[i].course < 2)
  64.         {
  65.             records[i].course++;
  66.             transfer[k] = records[i];
  67.             k++;
  68.         }
  69.     }
  70.     fstream file(direction, ios::binary | ios::in);
  71.     file.close();
  72.     file.open(direction, ios::binary | ios::out);
  73.     for (int i = 0; i < k; i++)
  74.         file.write(reinterpret_cast<char*>(&transfer[i]), sizeof(transfer[i]));
  75.     file.close();
  76.  
  77.  
  78. }
  79.  
  80. void showRecords(char* direction)
  81. {
  82.     int countOfRecords = numberOfRecords(direction);
  83.     vector<Students> records = getAllRecordsFromFile(direction);
  84.     if (!records.empty())
  85.     {
  86.         for (int i = 0; i < countOfRecords; i++)
  87.         {
  88.             cout << "\n" << i + 1 << "  студент\n";
  89.             cout << "fullname " << records[i].fullname;
  90.             cout << "\ncourse " << records[i].course;
  91.             cout << "\ngroup " << records[i].group;
  92.             cout << "\nlabel " << records[i].label << "\n\n";
  93.         }
  94.     }
  95.     else cout << "записей нет\n";
  96. }
  97.  
  98. void addRecordToBack(char* direction)
  99. {
  100.     ofstream file;
  101.     Students item;
  102.     int n = 0;
  103.     cout << "Введите кол-во новых записей: "; n = Input(0, 10);
  104.     file.open(direction, ios::binary | ios::out | ios::app);
  105.     for (int i = 0; i < n; i++)
  106.     {
  107.         cin.clear();
  108.         cout << i + 1 << " студент\n";
  109.         cout << "fullname "; cin.getline(item.fullname, SIZE, '\n');
  110.         cout << "course "; item.course=Input(1,SIZE);
  111.         cout << "group "; item.group=Input(1,SIZE);
  112.         cin.clear();
  113.         cout << "label "; cin.getline(item.label, SIZE, '\n');
  114.         file.write(reinterpret_cast<char*>(&item), sizeof(item));
  115.     }
  116.     file.close();
  117. }
  118. //
  119.  
  120.  
  121.  
  122. void deleteRecord(char* direction)
  123. {
  124.     int countOfRecords = numberOfRecords(direction), n, k = 0;
  125.     vector<Students> arrayOfRecords(countOfRecords - 1);
  126.     cout << "Введите номер записи для удаления: "; n = Input(0, countOfRecords + 1);
  127.     fstream file(direction, ios::binary | ios::in);
  128.     for (int i = 0; i < countOfRecords; i++)
  129.     {
  130.         if (i != n - 1)
  131.         {
  132.             file.read(reinterpret_cast<char*>(&arrayOfRecords[k]), sizeof(arrayOfRecords[k]));
  133.             k++;
  134.         }
  135.     }
  136.     file.close();
  137.     file.open(direction, ios::binary | ios::out);
  138.     file.close();
  139.     file.open(direction, ios::binary | ios::out);
  140.     for (int i = 0; i < countOfRecords - 1; i++)
  141.         file.write(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
  142.     file.close();
  143. }
  144.  
  145. void editRecord(char* direction)
  146. {
  147.     int n, nField, k = 0;
  148.     int countOfRecords = numberOfRecords(direction);
  149.     vector<Students> arrayOfRecords = getAllRecordsFromFile(direction);
  150.     cout << "Кол-во записей: " << countOfRecords << "\n";
  151.     cout << "Введите номер записи для редактирования: "; n = Input(0, countOfRecords + 1);
  152.     cout << "\n 1 - fullname, 2 - course, 3 - group, 4 - label\n";
  153.     cout << "Введите номер поля для редактирования: "; nField = Input(1, 5);
  154.  
  155.     switch (nField)
  156.     {
  157.     case 1:
  158.     {
  159.         cin.clear();
  160.         cout << "fullname "; cin.getline(arrayOfRecords[n - 1].fullname, SIZE, '\n');
  161.         break;
  162.     }
  163.     case 2:
  164.     {
  165.         cin.clear();
  166.         cout << "course "; cin >> arrayOfRecords[n - 1].course; cout << endl;
  167.         break;
  168.     }
  169.     case 3:
  170.     {
  171.         cin.clear();
  172.         cout << "group "; cin >> arrayOfRecords[n - 1].group; cout << endl;
  173.         break;
  174.     }
  175.     case 4:
  176.     {
  177.         cin.clear();
  178.         cout << "label "; cin.getline(arrayOfRecords[n - 1].label, SIZE, '\n');
  179.         break;
  180.     }
  181.     }
  182.     fstream file(direction, ios::binary | ios::out);
  183.     for (int i = 0; i < countOfRecords; i++)
  184.         file.write(reinterpret_cast<char*>(&arrayOfRecords[i]), sizeof(arrayOfRecords[i]));
  185.     file.close();
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement