Advertisement
qberik

Untitled

Dec 23rd, 2021
1,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <coino.h>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. const int MAX = 10; //КОЛИЧЕСТВО СТУДЕНТОВ
  10.  
  11. enum {
  12.     NAME_SIZE = 30,
  13.     FAMILI_SIZE = 30,
  14.     GRUPPA_SIZE = 20,
  15. };
  16.  
  17. struct student {
  18.     char name[NAME_SIZE]{ "Данные не указаны" };
  19.     char famili[FAMILI_SIZE]{ "Данные не указаны" };
  20.     char gruppa[GRUPPA_SIZE]{ "Данные не указаны" };
  21.     int god_roz{};
  22.     int god_post{};
  23.     int kurs{};
  24. }student_list[MAX];
  25.  
  26. void not_ready_yet() {
  27.     cout << "\n функция обещает заработать 14.12.21\n";
  28. }
  29.  
  30. void case_input(int i) {
  31.  
  32.     cout << "\nВведите данные студента №" << i + 1;
  33.     cout << "\nВведите имя студента: ";
  34.     cin.ignore();
  35.     cin.getline(student_list[i].name, NAME_SIZE);
  36.     cout << "Введите фамилию: ";
  37.     //cin.ignore();
  38.     cin.getline(student_list[i].famili, FAMILI_SIZE);
  39.     cout << "Введите группу: ";
  40.    // cin.ignore();
  41.     cin.getline(student_list[i].gruppa, GRUPPA_SIZE);
  42.     cout << "Введите год рождения: ";
  43.     cin >> student_list[i].god_roz;
  44.     cout << "Введите год поступления: ";
  45.     cin >> student_list[i].god_post;
  46.     cout << "Введите курс: ";
  47.     cin >> student_list[i].kurs;
  48.  
  49. }
  50.  
  51. void case_ouput(int count) {
  52.     cout << "\nВыбрана команда ВЫВОДА данных\n";
  53.     if (count != 0) {
  54.         cout << "-----------------+-----------------------------+-----------------------------+-------------------+-----------------------------+-----------------------------+----------------------------+";
  55.         cout << "\nДанные студента №|";
  56.         cout << setw(NAME_SIZE) << "Имя|";
  57.         cout << setw(FAMILI_SIZE) << "Фамилия|";
  58.         cout << setw(GRUPPA_SIZE) << "Группа|";
  59.         cout << setw(NAME_SIZE) << "Год рождения|";
  60.         cout << setw(NAME_SIZE) << "Год поступления|";
  61.         cout << setw(NAME_SIZE) << "Курс|\n";
  62.         cout << "-----------------+-----------------------------+-----------------------------+-------------------+-----------------------------+-----------------------------+----------------------------+";
  63.         for (int i = 0; i < count; i++) {
  64.             cout << "\n" << setw(17) << i + 1 << "|";
  65.             cout << setw(NAME_SIZE - 1) << student_list[i].name << "|";
  66.             cout << setw(NAME_SIZE - 1) << student_list[i].famili << "|";
  67.             cout << setw(GRUPPA_SIZE - 1) << student_list[i].gruppa << "|";
  68.             cout << setw(NAME_SIZE - 1) << student_list[i].god_roz << "|";
  69.             cout << setw(NAME_SIZE - 1) << student_list[i].god_post << "|";
  70.             cout << setw(NAME_SIZE - 2) << student_list[i].kurs << "|\n";
  71.             cout << "-----------------+-----------------------------+-----------------------------+-------------------+-----------------------------+-----------------------------+----------------------------+";
  72.         }
  73.     }
  74.     else { cout << "Данные не были введены или удалены"; }
  75. }
  76.  
  77. int delete_data(int count) {
  78.     cout << "Выбрана команда УДАЛЕНИЯ данных\n";
  79.     for (int i = 0; i < count; i++) {
  80.         strcpy_s(student_list[i].name, "Данные не указаны");
  81.         strcpy_s(student_list[i].famili, "Данные не указаны");
  82.         strcpy_s(student_list[i].gruppa, "Данные не указаны");
  83.         student_list[i].god_roz = 0;
  84.         student_list[i].god_post = 0;
  85.         student_list[i].kurs = 0;
  86.     }  
  87.     cout << "Все данные успешно удалены\n";
  88.     count = 0;
  89.     return 0;
  90. }
  91.  
  92. void sort_data(int count_of_filled) {
  93.     char a;
  94.     int var_of_sort{};
  95.     cout << "Выберите по какому параметру проводить сортировку: \n\n";
  96.     cout << "1 - по имени\n";
  97.     cout << "1 - по фамилии\n";
  98.     cout << "3 - по группе\n";
  99.     cout << "4 - по году рождения\n";
  100.     cout << "5 - по году поступления\n";
  101.     cout << "6 - по курсу\n";
  102.     do
  103.     {
  104.         cout << "\nВведите порядковый номер вариантов сортировки: ";
  105.         cin >> a;
  106.         var_of_sort = a - '0';
  107.     } while (var_of_sort < 0 && var_of_sort > 6);
  108.     switch (var_of_sort){
  109.         case 1:{
  110.             student tmp;        // сортировка по алфавиту
  111.             for (int t = 0; t < count_of_filled; t++){
  112.                 for (int i = 0; i < count_of_filled; i++){
  113.                     if ( strcmp_s(student_list[i].name,  student_list[t].name) > 0 )
  114.                     {
  115.                         tmp = student_list[i];
  116.                         student_list[i] = student_list[t];
  117.                         student_list[t] = tmp;
  118.                     }
  119.                 }
  120.             }
  121.             cout << "\nСортировка проведена. Введите 2 для вывода\n";
  122.             break;
  123.         }
  124.         case 2: {
  125.             student tmp;        // сортировка по алфавиту
  126.             for (int t = 0; t < count_of_filled; t++){
  127.                 for (int i = 0; i < count_of_filled; i++){
  128.                     if ( strcmp_s(student_list[i].famili,  student_list[t].famili) > 0 )
  129.                     {
  130.                         tmp = student_list[i];
  131.                         student_list[i] = student_list[t];
  132.                         student_list[t] = tmp;
  133.                     }
  134.                 }
  135.             }
  136.             cout << "\nСортировка проведена. Введите 2 для вывода\n";
  137.             break;
  138.         }
  139.         case 3: {
  140.             student tmp;        // сортировка по алфавиту
  141.             for (int t = 0; t < count_of_filled; t++){
  142.                 for (int i = 0; i < count_of_filled; i++){
  143.                     if ( strcmp_s(student_list[i].gruppa,  student_list[t].gruppa) > 0 )
  144.                     {
  145.                         tmp = student_list[i];
  146.                         student_list[i] = student_list[t];
  147.                         student_list[t] = tmp;
  148.                     }
  149.                 }
  150.             }
  151.             cout << "\nСортировка проведена. Введите 2 для вывода\n";
  152.             break;
  153.         }
  154.         case 4: {
  155.             student tmp;      
  156.             for (int i = 0; i < count_of_filled; i++){
  157.                 for (int j = 0; j < count_of_filled; j++){
  158.                     if (student_list[i].god_roz < student_list[j].god_roz)
  159.                     {
  160.                         tmp = student_list[i];
  161.                         student_list[i] = student_list[j];
  162.                         student_list[j] = tmp;
  163.                     }
  164.                 }
  165.             }
  166.             cout << "\nСортировка проведена. Введите 2 для вывода\n";
  167.             break;
  168.         }
  169.         case 5: {
  170.             student tmp;        
  171.             for (int t = 0; t < count_of_filled; t++){
  172.                 for (int i = 0; i < count_of_filled; i++){
  173.                     if (student_list[i].god_post > student_list[t].god_post)
  174.                     {
  175.                         tmp = student_list[i];
  176.                         student_list[i] = student_list[t];
  177.                         student_list[t] = tmp;
  178.                     }
  179.                 }
  180.             }
  181.             cout << "\nСортировка проведена. Введите 2 для вывода\n";
  182.             break;
  183.         }
  184.         case 6: {
  185.             student tmp;  
  186.             for (int t = 0; t < count_of_filled; t++) {
  187.                 for (int i = 0; i < count_of_filled; i++) {
  188.                     if (student_list[i].kurs > student_list[t].kurs)
  189.                     {
  190.                         tmp = student_list[i];
  191.                         student_list[i] = student_list[t];
  192.                         student_list[t] = tmp;
  193.                     }
  194.                 }
  195.             }
  196.             cout << "\nСортировка проведена. Введите 2 для вывода\n";
  197.             break;
  198.         }
  199.         default: { cout << "\nВведено неверное значение. Попробуйте ещё раз. \n";
  200.             sort_data(count_of_filled);
  201.             break;
  202.         }
  203.     }
  204. }
  205.  
  206. int text_file_input() {
  207.     ifstream text_f("text_file.txt");
  208.     if (!text_f.is_open()) return 666;
  209.     int count{}, i{};
  210.     text_f >> count;
  211.     cout << "HELLO " << count << endl;
  212.     for (int I = 0; I < count; I++ ) {
  213.         text_f >> i; i--;
  214.         char tmp[100]; text_f.getline(tmp, 100);
  215.         text_f.getline(student_list[i].name,  NAME_SIZE );
  216.         text_f.getline(student_list[i].famili, FAMILI_SIZE);
  217.         text_f.getline(student_list[i].gruppa, GRUPPA_SIZE);
  218.         text_f >> student_list[i].god_roz;
  219.         text_f >> student_list[i].god_post;
  220.         text_f >> student_list[i].kurs;
  221.         text_f.getline(tmp, 100);
  222.     }
  223.     text_f.close();
  224.     return count;
  225. }
  226.  
  227. void text_file_ouput(int count) {
  228.     ofstream text_f("text_file.txt");
  229.     text_f << count << endl;
  230.     for (int i = 0; i < count; i++) {
  231.         text_f << i + 1 << endl;
  232.         cin.ignore();
  233.         text_f << student_list[i].name << endl;
  234.         text_f << student_list[i].famili << endl;
  235.         text_f << student_list[i].gruppa << endl;
  236.         text_f << student_list[i].god_roz << endl;
  237.         text_f << student_list[i].god_post << endl;
  238.         text_f << student_list[i].kurs << endl;
  239.     }
  240.     text_f.close();
  241. }
  242.  
  243. void counvert_bin_file() {
  244.     ifstream text_f("text_file.txt", ios::in);
  245.     ofstream bin_f("bin_file.bin", ios::binary | ios::out);
  246.     if (text_f.is_open() && bin_f.is_open()) {
  247.         int i{}, count{};
  248.         char tmp{};
  249.         text_f >> count;
  250.         //bin_f << count;
  251.         bin_f.write((char*)&count,sizeof(count));
  252.         for (int I = 0; I < count; I++ ) {
  253.             text_f >> i; i--;
  254.             char tmp[100]; text_f.getline(tmp, 100);
  255.             //cin.ignore();
  256.             text_f.getline(student_list[i].name,  NAME_SIZE );
  257.  
  258.             cout << student_list[i].name;
  259.             text_f.getline(student_list[i].famili, FAMILI_SIZE);
  260.             text_f.getline(student_list[i].gruppa, GRUPPA_SIZE);
  261.             //text_f >> student_list[i].name;
  262.             //cin.ignore();
  263.             //text_f >> student_list[i].famili;
  264.             //cin.ignore();
  265.             //text_f >> student_list[i].gruppa;
  266.             text_f >> student_list[i].god_roz;
  267.             text_f >> student_list[i].god_post;
  268.             text_f >> student_list[i].kurs;
  269.             text_f.getline(tmp, 100);
  270.  
  271.         }
  272.         bin_f.write((char*)&student_list, sizeof(student_list));
  273.     }
  274.     else cout << "\nError!!!\n";
  275.     text_f.close();
  276.     bin_f.close();
  277. }
  278.  
  279. int read_bin_file() {
  280.     ifstream fin("bin_file.bin", ios::in | ios::binary);
  281.     int index{};
  282.     char tmp{};
  283.     int count;
  284.     //fin >> count;
  285.     fin.read( (char*) &count, sizeof(count) );
  286.     cout << count;
  287.     for(int i = 0; i < count; i++) {
  288.         fin.read((char*)&student_list[i], sizeof(student_list[i]));
  289.     }
  290.     //fin.read((char*)&student_list, sizeof(student_list));
  291.     //while (fin >> tmp) {
  292.     //    fin.read((char*)&student_list[index], sizeof(student_list[index]));
  293.     //    index++;
  294.     //}
  295.     fin.close();
  296.     return count;
  297. }
  298. /*void Input_from_binary(int& countOfStudents, Student* students) {
  299.     ifstream fin;
  300.     fin.open("database.bin", ios::binary);
  301.  
  302.     if (fin.is_open()) {
  303.         fin.read((char*)&countOfStudents, sizeof(countOfStudents));
  304.  
  305.         for (int i = 0; i < countOfStudents; i++) {
  306.             fin.read((char*)&students[i], sizeof(students[i]));
  307.         }
  308.     }
  309.     else {
  310.         system("cls");
  311.         cout << "***Ошибка открытия файла***";
  312.         system("pause");
  313.     }
  314.  
  315.     fin.close();
  316. }*/
  317.  
  318. int working_on_files(int count_of_filled) {
  319.     int option;
  320.     bool f_exit = true;
  321.     while (f_exit != false) {
  322.         cout << "\nВыберите функцию из меню:\n";
  323.         cout << "--------------------------------------------";
  324.         cout << "\n1 - ввод информации из уже созданного текстового файла\n";
  325.         cout << "2 - ввод информации из уже созданного бинарного файла\n";
  326.         cout << "3 - вывод данных в файл(текстовый)\n";
  327.         cout << "4 - перевод содержимого текстового файла в бинарный\n";
  328.         cout << "0 - выход из подрограммы\n";
  329.         cout << "--------------------------------------------";
  330.         do {
  331.             cout << "\nВведите номер функции: ";
  332.             cin >> option;
  333.         } while (option < 0 || option > 4);
  334.         switch (option) {
  335.         case 1: {int count{}; count = text_file_input();
  336.             if (count >= count_of_filled) count_of_filled = count;
  337.                 break; }
  338.         case 2: {int count{}; count = read_bin_file();
  339.             if (count >= count_of_filled) count_of_filled = count;
  340.             break; }
  341.         case 3: text_file_ouput(count_of_filled);
  342.             break;
  343.         case 4: counvert_bin_file();
  344.             break;
  345.         case 0: f_exit = false;
  346.             break;
  347.         default: { cout << "\nВведено неверное значение. Попробуйте ещё раз. \n";
  348.             working_on_files(count_of_filled);
  349.             break;
  350.         }
  351.         }
  352.     }
  353.     return count_of_filled;
  354. }
  355.  
  356. void zapros(int count_of_filled) {
  357.     student tmp;
  358.     for (int t = 0; t < count_of_filled; t++)
  359.         for (int i = 0; i < count_of_filled; i++)
  360.             if (student_list[i].god_roz < student_list[t].god_roz)
  361.             {
  362.                 tmp = student_list[i];
  363.                 student_list[i] = student_list[t];
  364.                 student_list[t] = tmp;
  365.             }
  366.     cout << "Фамилии и группы двух наиболее младших по возрасту студентов\n";
  367.     for (int j = 0; j < 2; j++) {
  368.         cout << j + 1 << ". " << student_list[j].famili << "  | " << student_list[j].gruppa << endl;
  369.     }
  370. }
  371.  
  372.  
  373.  
  374.  
  375.  
  376. int return_menu(void) {
  377.     char tp[MAX];
  378.     int c;
  379.     cout << "\n\n1 - ввести данные группы студентов\n";
  380.     cout << "2 - вывести данные группы студентов в виде таблицы\n";
  381.     cout << "3 - изменение записи данных группы студентов\n";
  382.     cout << "4 - удаление записи данных группы студентов\n";
  383.     cout << "5 - сортировка данных\n";
  384.     cout << "6 - работа с файлами\n";
  385.     cout << "7 - вывод данных по запросу(вывести фамилии и группы двух наиболее младших по возрасту студентов)\n";
  386.     cout << "0 - выход из программы\n";
  387.     do
  388.     {
  389.         cout << "\nВведите порядковый номер функции: ";
  390.         cin >> tp;
  391.         c = *tp - '0';
  392.         cout << c;
  393.     } while (c < 0 && c > 10);
  394.     return c;
  395. }
  396.  
  397. int main() {
  398.     setlocale(0, "");
  399.     int command{}, count_of_filled{};
  400.     int c{};
  401.     bool flag = true;
  402.     cout << "Здравствуйте!\n";
  403.     while (flag != false) {
  404.         command = return_menu();
  405.         switch (command) {
  406.             case 1: {
  407.  
  408.                 cout << "\nВыбрана команда ВВОДА данных\n";
  409.                 cout << "Введите количество студентов, данные которых вы хотите ввести: \n";
  410.                 cin >> c;
  411.                 for (int i = 0; i < c; i++) {
  412.                     case_input(i); count_of_filled++;
  413.                 }
  414.                 break;
  415.             }
  416.             case 2: case_ouput(count_of_filled); break;
  417.             case 3: {
  418.                 int number{};
  419.                 cout << "Введите номер студента, данные которого вы хотите изменить: ";
  420.                 cin >> number;
  421.                 cout << "\nВыбрана команда ИЗМЕНЕНИЯ данных студента №" << number << "\n";
  422.                 case_input(number - 1);
  423.                 break;
  424.             }
  425.             case 4: {count_of_filled = delete_data(count_of_filled); break; }
  426.             case 5: sort_data(count_of_filled); break;
  427.             case 6: count_of_filled = working_on_files(count_of_filled); break;
  428.             case 7: zapros(count_of_filled); break;
  429.             case 0: cout << "Выбрана команда ВЫХОД\n"; flag = false; break;
  430.             default: { cout << "\nВведено неверное значение. Попробуйте ещё раз. \n";
  431.                 return_menu();
  432.                 break;
  433.             }
  434.         }
  435.     }
  436.     return 0;
  437. }
  438.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement