Filage

Files2

Apr 9th, 2024
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. struct Student {
  9.     string name;
  10.     string secondname;
  11.     string surname;
  12.     string sex;
  13.     unsigned int course = 0;
  14.     unsigned int groupNum = 0;
  15.     unsigned int grade[5] = {0, 0, 0, 0, 0};
  16. };
  17.  
  18. int takeNum(int Min, int Max) {
  19.     const string error = "Проверьте корректность введнных данных!\n";
  20.     bool isIncorrect;
  21.     int num;
  22.     do {
  23.         isIncorrect = false;
  24.         cin >> num;
  25.         if (cin.fail()) {
  26.             isIncorrect = true;
  27.             cout << error;
  28.             cin.clear();
  29.             while (cin.get() != '\n');
  30.         }
  31.         if (!isIncorrect && cin.get() != '\n') {
  32.             cin.clear();
  33.             while (cin.get() != '\n');
  34.             cout << error;
  35.             isIncorrect = true;
  36.         }
  37.         if (!isIncorrect && (num < Min || num > Max)) {
  38.             isIncorrect = true;
  39.             cout << error;
  40.         }
  41.     } while (isIncorrect);
  42.     return num;
  43. }
  44.  
  45. string wayToFile() {
  46.     fstream checkFile;
  47.     string way;
  48.     bool isIncorrect;
  49.     do {
  50.         isIncorrect = false;
  51.         cout << "Введите путь к файлу: ";
  52.         cin >> way;
  53.         checkFile.open(way);
  54.         if (!checkFile.is_open()) {
  55.             cout << "Проверьте правильность и нахождение вашего файла по заданному пути\n";
  56.             isIncorrect = true;
  57.         }
  58.         else {
  59.             isIncorrect = false;
  60.         }
  61.         checkFile.close();
  62.     } while (isIncorrect);
  63.     return way;
  64. }
  65.  
  66. void createFile() {
  67.     fstream F;
  68.     string fileName;
  69.     cout << "Введите имя файла: ";
  70.     cin >> fileName;
  71.     F.open(fileName, ios::out | ios::binary);
  72.     F.close();
  73.     cout << "Файл успешно создан!\n";
  74. }
  75.  
  76. void addStudent(Student student[], int& count) {
  77.     bool female;
  78.     fstream F;
  79.     string fileName = wayToFile();
  80.     F.open(fileName, ios::in | ios::binary);
  81.     int i = 0;
  82.     while (F.read((char*)&student[i], sizeof(student[i])))
  83.         i++;
  84.     F.close();
  85.     count = i;
  86.     F.open(fileName, ios::out | ios::binary | ios::app);
  87.     cout << "Введите имя студента: ";
  88.     cin >> student[count].name;
  89.     cout << "Введите фамилию студента: ";
  90.     cin >> student[count].secondname;
  91.     cout << "Введите отчество студента: ";
  92.     cin >> student[count].surname;
  93.     cout << "Введите пол студента(нажмите 0 - М, 1 - Ж): ";
  94.     female = takeNum(0, 1);
  95.     if(female == 0)
  96.         student[count].sex = "male";
  97.     else
  98.         student[count].sex = "female";
  99.     cout << "Введите курс студента: ";
  100.     student[count].course = takeNum(1,5);
  101.     cout << "Введите группу студента: ";
  102.     student[count].groupNum = takeNum(1, 99999);
  103.     cout << "Введите оценки студента по пяти предметам: \n";
  104.     for (int i = 0; i < 5; i++) {
  105.         cout << i << " предмет = : ";
  106.         student[count].grade[i] = takeNum(1, 10);
  107.     }
  108.     F.write((char*)&student[count], sizeof(student[count]));
  109.     F.close();
  110.     count++;
  111.     system("cls");
  112. }
  113.  
  114. void viewFile(Student* student, int& count){
  115.     system("cls");
  116.     fstream F;
  117.     string fileName = wayToFile();
  118.     F.open(fileName, ios::in | ios::binary);
  119.     int i = 0;
  120.     cout << setw(20) << right << "Имя студента |"; // таблица setw смещение границы
  121.     cout << setw(20) << right << "Фамилия студента|";
  122.     cout << setw(20) << right << "Отчество студента|";
  123.     cout << setw(14) << right << "Пол студента|";
  124.     cout << setw(14) << right << "Курс студента|";
  125.     cout << setw(14) << right << "Группа студента|";
  126.     cout << setw(20) << right << "Успеваемость студента|\n";
  127.     while (F.read((char*)&student[i], sizeof(student[i]))) {
  128.         cout << setw(19) << right << student[i].name;
  129.         cout << setw(20) << right << student[i].secondname;
  130.         cout << setw(20) << right << student[i].surname;
  131.         cout << setw(14) << right << student[i].sex;
  132.         cout << setw(14) << right << student[i].course;
  133.         cout << setw(14) << right << student[i].groupNum;
  134.         cout << setw(14);
  135.         for (int j = 0; j < 5; j++)
  136.             cout << student[i].grade[j] << " ";
  137.         cout << endl;
  138.         i++;
  139.     }
  140.     count = i;
  141.     F.close();
  142. }
  143.  
  144. void deleteStudent(Student student[], int& count) {
  145.     fstream F;
  146.     string fileName = wayToFile();
  147.     string secname;
  148.     int i = 0;
  149.     cout << "Введите фамилию студента, которого хотите удалить: ";
  150.     cin >> secname;
  151.     while (i < count && student[i].secondname != secname)
  152.         i++;
  153.     if (i == count) {
  154.         cout << "Студент не найден!\n";
  155.         return;
  156.     }
  157.     F.open(fileName, ios::out | ios::binary);
  158.     for (int j = i + 1; j < count; j++)
  159.         student[j - 1] = student[j];
  160.     count--;
  161.     for (int l = 0; l < count; l++)
  162.         F.write((char*)&student[l], sizeof(student[l]));
  163.     F.close();
  164.     system("cls");
  165.     cout << "Студент удален!\n";
  166. }
  167.  
  168. void editStudentInformation(Student*& student, int count) {
  169.     fstream F;
  170.     string fileName = wayToFile();
  171.     bool female;
  172.     int choice;
  173.     string secname;
  174.     bool isExists = false;
  175.     cout << "Введите фамилию студента, информацию которого вы хотите изменить: ";
  176.     cin >> secname;
  177.     for (int i = 0; i < count; i++) {
  178.         if (student[i].secondname == secname) {
  179.             isExists = true;
  180.             cout << "Студент найден!\n";
  181.             while (true) {
  182.                 cout << "Можно изменить:\n";
  183.                 cout << "1. Имя\n";
  184.                 cout << "2. Фамилию\n";
  185.                 cout << "3. Отчество\n";
  186.                 cout << "4. Пол\n";
  187.                 cout << "5. Курс\n";
  188.                 cout << "6. Номер группы\n";
  189.                 cout << "7. Успеваемость\n";
  190.                 cout << "8. Выход.\n";
  191.                 cout << "Введите номер выбранного действия: ";
  192.                 choice = takeNum(1, 2147483647);
  193.                 switch (choice) {
  194.                 case 1:
  195.                     cout << "Введите имя студента: ";
  196.                     cin >> student[i].name;
  197.                     break;
  198.                 case 2:
  199.                     cout << "Введите фамилию студента: ";
  200.                     cin >> student[i].secondname;
  201.                     break;
  202.                 case 3:
  203.                     cout << "Введите отчество студента: ";
  204.                     cin >> student[i].surname;
  205.                     break;
  206.                 case 4:
  207.                     cout << "(нажмите 0 - М, 1 - Ж): ";
  208.                     female = takeNum(0, 1);
  209.                     if (female == 0)
  210.                         student[i].sex = "male";
  211.                     else
  212.                         student[i].sex = "female";
  213.                     break;
  214.                 case 5:
  215.                     cout << "Введите курс студента: ";
  216.                     student[i].course = takeNum(1, 5);
  217.                     break;
  218.                 case 6:
  219.                     cout << "Введите группу студента: ";
  220.                     student[i].groupNum = takeNum(1, 99999);
  221.                     break;
  222.                 case 7:
  223.                     cout << "Введите оценки студента по пяти предметам: \n";
  224.                     for(int j = 0; j < 5; j++) {
  225.                         cout << j + 1 << " предмет = ";
  226.                         student[i].grade[j] = takeNum(1, 10);
  227.                     }
  228.                     break;
  229.                 case 8:
  230.                     F.open(fileName, ios::out | ios::binary);
  231.                     for (int l = 0; l < count; l++)
  232.                         F.write((char*)&student[l], sizeof(student[l]));
  233.                     F.close();
  234.                     return;
  235.                 default:
  236.                     cout << "Ошибка ввода! Введите ту цифру, которое есть в меню\n";
  237.                     break;
  238.                 }
  239.             }
  240.         }
  241.     }
  242.     if (!isExists)
  243.         cout << "Такого студента не существует.\n";
  244. }
  245.  
  246. void createExcellentStudentFile(Student student[], int count) {
  247.     bool isExists = false;
  248.     int mark;
  249.     fstream F;
  250.     string fileName = wayToFile();
  251.     F.open(fileName, ios::in | ios::binary);
  252.     int i = 0;
  253.     while (F.read((char*)&student[i], sizeof(student[i])))
  254.         i++;
  255.     F.close();
  256.     count = i;
  257.     for (int i = 0; i < count; i++) {
  258.         mark = 0;
  259.         for (int j = 0; j < 5; j++)
  260.             if (student[i].grade[j] > 8)
  261.                 mark++;
  262.         if (mark == 5) {
  263.             if (!isExists) {
  264.                 cout << "Введите имя файла, в который хотите записать отличников: ";
  265.                 cin >> fileName;
  266.                 F.open(fileName, ios::out | ios::binary);
  267.             }
  268.             F.write((char*)&student[i], sizeof(student[i]));
  269.             isExists = true;
  270.         }
  271.     }
  272.     if (isExists) {
  273.         cout << "Отличники успешно записаны в новый файл!\n";
  274.     }
  275.     else {
  276.         cout << "Отличников среди студентов не найдено!\n";
  277.     }
  278.     F.close();
  279. }
  280.  
  281. void menu_Program(Student student[]) {
  282.     int count = 0;
  283.     while (true) {
  284.         int choice;
  285.         cout << "Текстовое меню:\n";
  286.         cout << "1. Добавление студента\n";
  287.         cout << "2. Удаление студента\n";
  288.         cout << "3. Корректировка информации о студенте\n";
  289.         cout << "4. Просмотр содержимого файла\n";
  290.         cout << "5. Создать файл из всех отличников\n";
  291.         cout << "6. Создать файл\n";
  292.         cout << "7. Выход\n";
  293.         cout << "Введите номер выбранного действия: ";
  294.         choice = takeNum(1, 7);
  295.         switch (choice) {
  296.         case 1:
  297.             if (count > 31) {
  298.                 cout << "Достигнуто максимальное значение студентов в группе";
  299.             }
  300.             else
  301.                 addStudent(student, count);
  302.             break;
  303.         case 2:
  304.             deleteStudent(student, count);
  305.             break;
  306.         case 3:
  307.             editStudentInformation(student, count);
  308.             break;
  309.         case 4:
  310.             viewFile(student, count);
  311.             break;
  312.         case 5:
  313.             createExcellentStudentFile(student, count);
  314.             break;
  315.         case 6:
  316.             createFile();
  317.             break;
  318.         case 7:
  319.             cout << "Программа завершена!";
  320.             return;
  321.         default:
  322.             cout << "Ошибка ввода! Введите то число, которое есть в меню\n";
  323.             break;
  324.         }
  325.     }
  326. }
  327.  
  328. int main() {
  329.     setlocale(LC_ALL, "Rus");
  330.     Student student[32];
  331.     menu_Program(student);
  332. }
Advertisement
Add Comment
Please, Sign In to add comment