Advertisement
SkeptaProgrammer

Untitled

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