#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
struct Student {
std::string surname;
std::string name;
int age;
int course;
};
int main() {
std::vector<Student> students;
std::ifstream file("students.txt");
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
Student student;
std::sscanf(line.c_str(), "%s %s %d %d", &student.surname[0], &student.name[0], &student.age, &student.course);
if (student.surname[0] == 'Ф') {
students.push_back(student);
}
file.close();
} else {
std::cerr << "Unable to open file" << std::endl;
return 1;
std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.name < b.name;
});
for (const auto& student : students) {
std::cout << student.surname << " " << student.name << ", " << student.age << " лет, " << student.course << " курс" << std::endl;
return 0;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Verified answer
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
struct Student {
std::string surname;
std::string name;
int age;
int course;
};
int main() {
std::vector<Student> students;
std::ifstream file("students.txt");
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
Student student;
std::sscanf(line.c_str(), "%s %s %d %d", &student.surname[0], &student.name[0], &student.age, &student.course);
if (student.surname[0] == 'Ф') {
students.push_back(student);
}
}
file.close();
} else {
std::cerr << "Unable to open file" << std::endl;
return 1;
}
std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) {
return a.name < b.name;
});
for (const auto& student : students) {
std::cout << student.surname << " " << student.name << ", " << student.age << " лет, " << student.course << " курс" << std::endl;
}
return 0;
}