Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <fstream>
- #include <sstream>
- #include <vector>
- using namespace std;
- struct Applicant {
- string surname{};
- string name{};
- string patronymic{};
- string gender{};
- string nationality{};
- int year{};
- int month{};
- int day{};
- int postal_code{};
- string country{};
- string region{};
- string district{};
- string city{};
- string street{};
- string building{};
- string apartment{};
- int mark1{};
- int mark2{};
- int mark3{};
- double threshold{};
- };
- void readApplicants(vector<Applicant>& applicants, const string& filename) {
- ifstream fin(filename);
- if (!fin.is_open()) {
- cout << "Не вдалося відкрити вихідний файл";
- return;
- }
- string line;
- while (getline(fin, line)) {
- stringstream ss(line);
- string field{};
- Applicant a{};
- getline(ss, a.surname, ';');
- getline(ss, a.name, ';');
- getline(ss, a.patronymic, ';');
- getline(ss, a.gender, ';');
- getline(ss, a.nationality, ';');
- getline(ss, field, ';');
- a.year = stoi(field);
- getline(ss, field, ';');
- a.month = stoi(field);
- getline(ss, field, ';');
- a.day = stoi(field);
- getline(ss, field, ';');
- a.postal_code = stoi(field);
- getline(ss, a.country, ';');
- getline(ss, a.region, ';');
- getline(ss, a.district, ';');
- getline(ss, a.city, ';');
- getline(ss, a.street, ';');
- getline(ss, a.building, ';');
- getline(ss, a.apartment, ';');
- getline(ss, field, ';');
- a.mark1 = stoi(field);
- getline(ss, field, ';');
- a.mark2 = stoi(field);
- getline(ss, field, ';');
- a.mark3 = stoi(field);
- getline(ss, field, ';');
- a.threshold = stod(field);
- applicants.push_back(a);
- }
- fin.close();
- }
- void printApplicants(vector<Applicant>& applicants) {
- cout << "Список усіх абітурієнтів:" << endl;
- for (int i = 0; i < applicants.size(); i++) {
- Applicant a = applicants[i];
- cout << a.surname << " " << a.name << " " << a.patronymic << " - Пороговий бал: " << a.threshold << endl;
- }
- }
- void printFilteredApplicants(vector<Applicant>& applicants) {
- cout << "\nАбітурієнти з пороговим балом > 4:" << endl;
- for (int i = 0; i < applicants.size(); i++) {
- Applicant a = applicants[i];
- if (a.threshold > 4.0) {
- cout << a.surname << " " << a.name << " " << a.patronymic << " - Пороговий бал: " << a.threshold << endl;
- }
- }
- }
- void saveFilteredApplicants(vector<Applicant>& applicants, const string& filename) {
- ofstream fout(filename);
- if (!fout.is_open()) {
- cout << "Не вдалося відкрити файл для запису!" << endl;
- return;
- }
- for (int i = 0; i < applicants.size(); i++) {
- Applicant a = applicants[i];
- if (a.threshold > 4.0) {
- fout << a.surname << ";" << a.name << ";" << a.patronymic << ";"
- << a.gender << ";" << a.nationality << ";"
- << a.year << ";" << a.month << ";" << a.day << ";"
- << a.postal_code << ";" << a.country << ";" << a.region << ";"
- << a.district << ";" << a.city << ";" << a.street << ";"
- << a.building << ";" << a.apartment << ";"
- << a.mark1 << ";" << a.mark2 << ";" << a.mark3 << ";"
- << a.threshold << endl;
- }
- }
- fout.close();
- cout << "\nФайл з абітурієнтами, які мають пороговий бал > 4, успішно збережено!"<< endl;
- }
- int main() {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- vector<Applicant> applicants;
- readApplicants(applicants, "D:/files/applicants.txt");
- printApplicants(applicants);
- printFilteredApplicants(applicants);
- saveFilteredApplicants(applicants, "D:/files/filtered_applicants.txt");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement