Advertisement
gasaichan

students_parser

Oct 20th, 2018
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.21 KB | None | 0 0
  1. // В папке проекта обязательно должны быть файлы student.txt и data.dat. В student.txt клиенты записываются в формате
  2. // Фамилия Факультет Группа Средняя оценка Список дисциплин
  3. // Pirozhkov MPiTK  25 4 1 1 0 1 1
  4. // Roflanov PrIt 21 3.7 0 0 1 0 1
  5. // Papich EKT 22 4.9 0 0 0 1 1
  6. //
  7. //
  8. //
  9. //
  10. #include <iostream>
  11. #include <Windows.h>
  12. #include <string>
  13. #include <fstream>
  14. #include <vector>
  15. #include <algorithm>
  16.  
  17. using namespace std;
  18.  
  19. struct Student {
  20.     string last_name;
  21.     string faculty;
  22.     int group;
  23.     float avg_mark;
  24.     bool disciplines[5];
  25.     /*
  26.         discipline[0] - Математика
  27.         discipline[1] - Физика
  28.         discipline[2] - Информатика
  29.         discipline[3] - Иностранный язык
  30.         discipline[4] - Русский язык
  31.     */
  32.  
  33.  
  34.     bool operator<(Student obj) {
  35.         return true ? last_name.compare(obj.last_name) < 0 : false;
  36.     }
  37.  
  38.     string to_string() {
  39.         string output = "Фамилия студента: " + last_name + "; Факультет: " + faculty + "; Группа: " + std::to_string(group) + "; Средняя оценка: " + std::to_string(avg_mark) + "; Выбранные дисциплины: \n";
  40.         string math = disciplines[0] == 1 ? "Математика\n" : "";
  41.         string physics = disciplines[1] == 1 ? "Физика\n" : "";
  42.         string informatics = disciplines[2] == 1 ? "Информатика\n" : "";
  43.         string foreign_language = disciplines[3] == 1 ? "Иностранный язык\n" : "";
  44.         string russian_language = disciplines[4] == 1 ? "Русский язык\n" : "";
  45.  
  46.         return output + math + physics + informatics + foreign_language + russian_language;
  47.     }
  48. };
  49.  
  50.  
  51. bool comparator(Student o1, Student o2) {
  52.     return o1.last_name.compare(o2.last_name);
  53. }
  54.  
  55. vector<Student> get_students_from_file(string path) {
  56.     ifstream input_file;
  57.     vector<Student> students;
  58.     input_file.open(path, ifstream::in);
  59.     if (!input_file) {
  60.         cout << "Error opening file" << endl;
  61.         return students;
  62.     }
  63.  
  64.     else {
  65.         while (!input_file.eof()) {
  66.             Student current_student;
  67.             input_file >> current_student.last_name;
  68.             input_file >> current_student.faculty;
  69.             input_file >> current_student.group;
  70.             input_file >> current_student.avg_mark;
  71.             input_file >> current_student.disciplines[0];
  72.             input_file >> current_student.disciplines[1];
  73.             input_file >> current_student.disciplines[2];
  74.             input_file >> current_student.disciplines[3];
  75.             input_file >> current_student.disciplines[4];
  76.             students.push_back(current_student);
  77.         }
  78.  
  79.         input_file.close();
  80.         return students;
  81.     }
  82.     return students;
  83. }
  84.  
  85. void  write_bytes(string path, vector<Student> students) {
  86.     ofstream output_file;
  87.     size_t students_size = students.size();
  88.     output_file.open(path, ios::binary || fstream::out);
  89.     if (!output_file) {
  90.         cout << "Error opening output file!" << endl;
  91.     }
  92.     else {
  93.         output_file.write(reinterpret_cast<const char *>(&students_size), sizeof(students_size));
  94.         output_file.write(reinterpret_cast<const char *>(&students[0]), students.size() * sizeof Student);
  95.         output_file.close();
  96.     }
  97. }
  98.  
  99. vector<Student> get_bytes(string path) {
  100.     size_t students_size;
  101.     ifstream input_file;
  102.     input_file.open(path, ios::binary);
  103.     //Student Student;
  104.     if (!input_file) {
  105.         cout << "Error opening file!" << endl;
  106.     }
  107.     else {
  108.         input_file.read(reinterpret_cast<char *>(&students_size), sizeof(students_size));
  109.         vector<Student> students(students_size);
  110.  
  111.         input_file.read(reinterpret_cast<char *>(&students[0]), sizeof(Student) * students_size);
  112.         return students;
  113.     }
  114. }
  115.  
  116.  
  117. int main()
  118. {
  119.     setlocale(LC_ALL, "Russian");
  120.     string path = "student.txt";
  121.     string byte_path = "data.dat";
  122.  
  123.     vector<Student> students;
  124.     vector<Student> students_from_binary;
  125.  
  126.  
  127.     int discipline;
  128.     int avg;
  129.  
  130.     int choice;
  131.     while (true) {
  132.         cout << "1. Считать список студентов из файла, указанного в переменной path" << endl;
  133.         cout << "2. Вывести список студентов на экран" << endl;
  134.         cout << "3. Очистить экран" << endl;
  135.         cout << "4. Записать список студентов в бинарный файл, указанный в переменной byte_path" << endl;
  136.         cout << "5. Считать клиентов из бинарного файла и вывести на экран" << endl;
  137.         cout << "6. Отсортировать студентов в алфавитном порядке" << endl;
  138.         cout << "7. Вывести на экран студентов, желающих изучать дисциплину X со средним баллом не меньше Y" << endl;
  139.         cout << "8. Выход" << endl;
  140.         cout << "Ваш выбор: ";
  141.         cin >> choice;
  142.  
  143.         switch (choice) {
  144.         case 1:
  145.             students = get_students_from_file(path);
  146.             system("CLS");
  147.             break;
  148.         case 2:
  149.             system("CLS");
  150.             for (auto i : students) {
  151.                 cout << i.to_string() << endl;
  152.             }
  153.             break;
  154.         case 3:
  155.             system("CLS");
  156.             break;
  157.         case 4:
  158.             write_bytes(byte_path, students);
  159.             system("CLS");
  160.             break;
  161.         case 5:
  162.             system("CLS");
  163.             students_from_binary = get_bytes(byte_path);
  164.             for (auto i : students_from_binary) {
  165.                 cout << i.to_string() << endl;
  166.             }
  167.             break;
  168.         case 6:
  169.             sort(students.begin(), students.end());
  170.             for (auto i : students_from_binary) {
  171.                 cout << i.to_string() << endl;
  172.             }
  173.             break;
  174.         case 7:
  175.             /*
  176.             discipline[0] - Математика
  177.             discipline[1] - Физика
  178.             discipline[2] - Информатика
  179.             discipline[3] - Иностранный язык
  180.             discipline[4] - Русский язык
  181.             */
  182.             cout << "Введите искомую дисциплину.\n0 - Математика\n1-Физика\n2-Информатика\n3-Иностранный язык\n4-Русский язык\n";
  183.             cin >> discipline;
  184.             cout << "Введите искомый средний балл: ";
  185.             cin >> avg;
  186.             system("CLS");
  187.             for (auto i : students) {
  188.                 if (i.avg_mark >= avg && i.disciplines[discipline] == 1) {
  189.                     cout << i.to_string() << endl;
  190.                 }
  191.             }
  192.             break;
  193.         case 8:
  194.             system("EXIT");
  195.             break;     
  196.         }
  197.     }
  198.  
  199.         system("PAUSE");
  200.         return 0;
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement