Advertisement
80LK

LR20 AKT

Nov 14th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. const string BDFIle = "StudBD.txt";
  8.  
  9. class StudentApplication {
  10. private:
  11.     int getCountStudent() {
  12.         ifstream bd(BDFIle);
  13.         if (!bd)
  14.             return 0;
  15.        
  16.         int g = 0;
  17.         string a;
  18.        
  19.         while (bd >> a) {
  20.             for (int i = 0; i < 5; i++)
  21.                 bd >> a;
  22.  
  23.             g++;
  24.         }
  25.  
  26.         bd.close();
  27.  
  28.         return g;
  29.     }
  30. public:
  31.     string name, surname, patronymic, group, phone;
  32.     int year;//Курс
  33.     bool addInBD() {
  34.         ofstream bd(BDFIle, ios::app);
  35.  
  36.             bd  << surname << " "
  37.                 << name << " "
  38.                 << name << " "
  39.                 << group << " "
  40.                 << year << " "
  41.                 << phone << endl;
  42.  
  43.         bd.close();
  44.  
  45.         return true;
  46.     }
  47.  
  48.     bool search(string search, bool byGroup) {
  49.         ifstream bd(BDFIle);
  50.  
  51.         if (!bd) return false;
  52.  
  53.         StudentApplication stud;
  54.         int print = 0;
  55.  
  56.         while (bd >> stud.surname) {
  57.             bd >> stud.name;
  58.             bd >> stud.patronymic;
  59.             bd >> stud.group;
  60.             bd >> stud.year;
  61.             bd >> stud.phone;
  62.  
  63.             if ((byGroup && stud.group == search) || (!byGroup && stud.surname == search)) {
  64.                 print++;
  65.  
  66.                 setlocale(0, ".866");
  67.                 cout << print << ") "
  68.                      << stud.surname << " "
  69.                      << stud.patronymic << " - "
  70.                      << stud.group << "("
  71.                      << stud.year << ")" << endl;
  72.  
  73.                 setlocale(0, ".1251");
  74.                 cout << "    Телефон: " << stud.phone << endl;
  75.             }
  76.         }
  77.  
  78.         return print != 0;
  79.     }
  80.    
  81.     bool searchBySurname(string surname) {
  82.         return search(surname, false);
  83.     }
  84.     bool searchByGroup(string group) {
  85.         return search(group, true);
  86.     }
  87.  
  88.     bool printListStudents() {
  89.         int a = getCountStudent();
  90.        
  91.         if (a == 0)
  92.             return false;
  93.  
  94.         StudentApplication *studs = new StudentApplication[a];
  95.         ifstream bd(BDFIle);
  96.         int i = 0;
  97.         /* Чтение */
  98.         while (bd >> studs[i].surname) {
  99.             bd >> studs[i].name;
  100.             bd >> studs[i].patronymic;
  101.             bd >> studs[i].group;
  102.             bd >> studs[i].year;
  103.             bd >> studs[i].phone;
  104.             i++;
  105.         }
  106.  
  107.         /* Сортировка */
  108.         for (i = 0; i < a-1; i++) {
  109.             for (int j = 0; j < a - i - 1; j++) {
  110.                 if (studs[j].surname > studs[j + 1].surname) {
  111.                     StudentApplication s = studs[j];
  112.                     studs[j] = studs[j + 1];
  113.                     studs[j+1] = s;
  114.                 }
  115.             }
  116.         }
  117.        
  118.         /* Вывод */
  119.         for (i = 0; i < a; i++) {
  120.             setlocale(0, ".866");
  121.             cout << i + 1 << ") "
  122.                  << studs[i].surname << " "
  123.                  << studs[i].name << " "
  124.                  << studs[i].patronymic << " - "
  125.                  << studs[i].group << "(" << studs[i].year << ")" << endl;
  126.             setlocale(0, ".1251");
  127.             cout << "    Телефон: " << studs[i].phone << endl;
  128.         }
  129.  
  130.         bd.close();
  131.         delete []studs;
  132.  
  133.         return true;
  134.     }
  135. };
  136.  
  137. void openMenu();
  138. void printListStudent();
  139. void openNewStudent();
  140. void openSearchStudent(bool);
  141.  
  142. int main()
  143. {
  144.     setlocale(0, ".1251");
  145.  
  146.     openMenu();
  147.  
  148.     return 0;
  149. }
  150.  
  151. void openMenu() {
  152.     int menu;
  153.  
  154.     cout << "Меню:" << endl
  155.          << "1 - Добавить запись о студенте" << endl
  156.          << "2 - Поиск студентов по фамилии" << endl
  157.          << "3 - Поиск студентов по группе" << endl
  158.          << "4 - Список студентов" << endl
  159.          << "0 - Завершить работу" << endl;
  160.    
  161.     cin >> menu;
  162.  
  163.     switch (menu)
  164.     {
  165.         case 1:
  166.             openNewStudent();
  167.         break;
  168.         case 2:
  169.             openSearchStudent(false);
  170.         break;
  171.         case 3:
  172.             openSearchStudent(true);
  173.         break;
  174.         case 4:
  175.             printListStudent();
  176.         break;
  177.         case 0:
  178.             cout << "Завершение работы." << endl;
  179.         break;
  180.         default:
  181.             openMenu();
  182.             break;
  183.     }
  184. }
  185.  
  186. void printListStudent() {
  187.     cout << "Список студентов:" << endl;
  188.     StudentApplication s;
  189.     bool a = s.printListStudents();
  190.     if (a == false)
  191.         cout << "Студентов нет." << endl;
  192.  
  193.     openMenu();
  194. }
  195.  
  196. void openNewStudent() {
  197.     cout << "Добавить запись о студенте:" << endl
  198.          << "0 - Выход" << endl;
  199.  
  200.     StudentApplication stud;
  201.     cout << "Введите фамилию: ";
  202.     cin >> stud.surname;
  203.     if (stud.surname == "0") {
  204.         openMenu();
  205.         return;
  206.     }
  207.  
  208.     cout << "Введите имя: ";
  209.     cin >> stud.name;
  210.     if (stud.name== "0") {
  211.         openMenu();
  212.         return;
  213.     }
  214.  
  215.     cout << "Введите отчество: ";
  216.     cin >> stud.patronymic;
  217.     if (stud.patronymic == "0") {
  218.         openMenu();
  219.         return;
  220.     }
  221.  
  222.     cout << "Введите группу: ";
  223.     cin >> stud.group;
  224.     if (stud.group == "0") {
  225.         openMenu();
  226.         return;
  227.     }
  228.  
  229.     cout << "Введите курс: ";
  230.     cin >> stud.year;
  231.     if (stud.year == 0) {
  232.         openMenu();
  233.         return;
  234.     }
  235.  
  236.     cout << "Введите телефон: ";
  237.     cin >> stud.phone;
  238.     if (stud.phone == "0") {
  239.         openMenu();
  240.         return;
  241.     }
  242.  
  243.     stud.addInBD();
  244.     openNewStudent();
  245. }
  246.  
  247. void openSearchStudent(bool byGroups) {
  248.     string search;
  249.     cout << "Поиск студентов по ";
  250.  
  251.     if (byGroups)
  252.         cout << "группе:";
  253.     else
  254.         cout << "фамилии:";
  255.  
  256.     cout << endl << "0 - Выход" << endl;
  257.  
  258.     cout << "Введите ";
  259.  
  260.     if (byGroups)
  261.         cout << "группу: ";
  262.     else
  263.         cout << "фамилию: ";
  264.  
  265.     cin >> search;
  266.     if (search == "0") {
  267.         openMenu();
  268.         return;
  269.     }
  270.  
  271.     cout << "Список студентов:" << endl;
  272.     StudentApplication stud;
  273.     bool a;
  274.     if (byGroups)
  275.         a = stud.searchByGroup(search);
  276.     else
  277.         a = stud.searchBySurname(search);
  278.  
  279.     if (a == false)
  280.         cout << "Студентов не найдено." << endl;
  281.  
  282.     openSearchStudent(byGroups);
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement