Advertisement
Lavig

Другий семестр. Лабораторна робота №16 (Завдання 1)

May 1st, 2025
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. struct Applicant {
  10.     string surname{};
  11.     string name{};
  12.     string patronymic{};
  13.     string gender{};
  14.     string nationality{};
  15.     int year{};
  16.     int month{};
  17.     int day{};
  18.     int postal_code{};
  19.     string country{};
  20.     string region{};
  21.     string district{};
  22.     string city{};
  23.     string street{};
  24.     string building{};
  25.     string apartment{};
  26.     int mark1{};
  27.     int mark2{};
  28.     int mark3{};
  29.     double threshold{};
  30. };
  31.  
  32. void readApplicants(vector<Applicant>& applicants, const string& filename) {
  33.     ifstream fin(filename);
  34.     if (!fin.is_open()) {
  35.         cout << "Не вдалося відкрити вихідний файл";
  36.         return;
  37.     }
  38.     string line;
  39.     while (getline(fin, line)) {
  40.         stringstream ss(line);
  41.         string field{};
  42.         Applicant a{};
  43.         getline(ss, a.surname, ';');
  44.         getline(ss, a.name, ';');
  45.         getline(ss, a.patronymic, ';');
  46.         getline(ss, a.gender, ';');
  47.         getline(ss, a.nationality, ';');
  48.         getline(ss, field, ';');
  49.         a.year = stoi(field);
  50.         getline(ss, field, ';');
  51.         a.month = stoi(field);
  52.         getline(ss, field, ';');
  53.         a.day = stoi(field);
  54.         getline(ss, field, ';');
  55.         a.postal_code = stoi(field);
  56.         getline(ss, a.country, ';');
  57.         getline(ss, a.region, ';');
  58.         getline(ss, a.district, ';');
  59.         getline(ss, a.city, ';');
  60.         getline(ss, a.street, ';');
  61.         getline(ss, a.building, ';');
  62.         getline(ss, a.apartment, ';');
  63.         getline(ss, field, ';');
  64.         a.mark1 = stoi(field);
  65.         getline(ss, field, ';');
  66.         a.mark2 = stoi(field);
  67.         getline(ss, field, ';');
  68.         a.mark3 = stoi(field);
  69.         getline(ss, field, ';');
  70.         a.threshold = stod(field);
  71.         applicants.push_back(a);
  72.     }
  73.     fin.close();
  74. }
  75. void printApplicants(vector<Applicant>& applicants) {
  76.     cout << "Список усіх абітурієнтів:" << endl;
  77.     for (int i = 0; i < applicants.size(); i++) {
  78.         Applicant a = applicants[i];
  79.         cout << a.surname << " " << a.name << " " << a.patronymic << " - Пороговий бал: " << a.threshold << endl;
  80.     }
  81. }
  82. void printFilteredApplicants(vector<Applicant>& applicants) {
  83.     cout << "\nАбітурієнти з пороговим балом > 4:" << endl;
  84.     for (int i = 0; i < applicants.size(); i++) {
  85.         Applicant a = applicants[i];
  86.         if (a.threshold > 4.0) {
  87.             cout << a.surname << " " << a.name << " " << a.patronymic << " - Пороговий бал: " << a.threshold << endl;
  88.         }
  89.     }
  90. }
  91. void saveFilteredApplicants(vector<Applicant>& applicants, const string& filename) {
  92.     ofstream fout(filename);
  93.     if (!fout.is_open()) {
  94.         cout << "Не вдалося відкрити файл для запису!" << endl;
  95.         return;
  96.     }
  97.     for (int i = 0; i < applicants.size(); i++) {
  98.         Applicant a = applicants[i];
  99.         if (a.threshold > 4.0) {
  100.             fout << a.surname << ";" << a.name << ";" << a.patronymic << ";"
  101.                 << a.gender << ";" << a.nationality << ";"
  102.                 << a.year << ";" << a.month << ";" << a.day << ";"
  103.                 << a.postal_code << ";" << a.country << ";" << a.region << ";"
  104.                 << a.district << ";" << a.city << ";" << a.street << ";"
  105.                 << a.building << ";" << a.apartment << ";"
  106.                 << a.mark1 << ";" << a.mark2 << ";" << a.mark3 << ";"
  107.                 << a.threshold << endl;
  108.         }
  109.     }
  110.     fout.close();
  111.     cout << "\nФайл з абітурієнтами, які мають пороговий бал > 4, успішно збережено!"<< endl;
  112. }
  113. int main() {
  114.     SetConsoleOutputCP(1251);
  115.     SetConsoleCP(1251);
  116.     vector<Applicant> applicants;
  117.     readApplicants(applicants, "D:/files/applicants.txt");
  118.     printApplicants(applicants);
  119.     printFilteredApplicants(applicants);
  120.     saveFilteredApplicants(applicants, "D:/files/filtered_applicants.txt");
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement