Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <array>
- #include <fstream>
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <vector>
- using namespace std;
- struct Student {
- array<int, 5> list{};
- string id;
- string name;
- Student() = default;
- Student(const array<int, 5>& list, const string& id, const string& name)
- : list(list), id(id), name(name) {}
- bool advanced(const int lower_limit)const {
- for (auto score : list) {
- if (score < lower_limit) return false;
- }
- return true;
- }
- void show()const {
- cout << id << ' ' << name << " = ";
- for (auto score : list) cout << score << ' ';
- puts("");
- }
- private:
- friend ostream& operator<<(ostream& out, const Student& s) {
- out << s.id << '\n' << s.name << '\n';
- for (auto score : s.list) out << score << ' ';
- out.put('\n');
- return out;
- }
- friend bool operator<(const Student& a, const Student& b) {
- return a.name < b.name;
- }
- };
- class Students {
- public:
- void add(const Student& student) {
- students.push_back(student);
- }
- void add(Student&& student)noexcept {
- students.emplace_back(student);
- }
- bool load(const string& path) {
- ifstream inp(path);
- if (!inp.is_open()) return false;
- students.clear();
- Student s;
- string res;
- while (getline(inp, s.id) && getline(inp, s.name) && getline(inp, res)) {
- istringstream iss(res);
- int score;
- size_t i = 0;
- while (iss >> score) {
- s.list[i] = score;
- ++i;
- }
- students.push_back(s);
- }
- inp.close();
- return true;
- }
- bool save(const string& path) {
- ofstream out(path);
- if (!out.is_open()) return false;
- for (const auto& student : students) out << student;
- out.close();
- return true;
- }
- Students select(const int lower_limit)const {
- Students eag;
- for (const auto& student : students) {
- if (student.advanced(lower_limit)) eag.add(student);
- }
- return eag;
- }
- void streamline() {
- sort(students.begin(), students.end());
- }
- void show()const {
- for (const auto& student : students) student.show();
- }
- private:
- vector<Student> students;
- };
- int main() {
- system("chcp 1251 > nul");
- const string path{ "students.txt" };
- Students students;
- if (!students.load(path)) {
- students.add(Student(array<int, 5>({ 5, 4, 5, 4, 4 }), "A4285", "Фролова А. Н."));
- students.add(Student(array<int, 5>({ 4, 5, 4, 4, 5 }), "A4286", "Иванов И. С."));
- students.add(Student(array<int, 5>({ 3, 4, 4, 4, 4 }), "A4287", "Зайцева Е. Ф."));
- students.add(Student(array<int, 5>({ 4, 5, 5, 5, 4 }), "A4288", "Королёв Е. Р."));
- students.add(Student(array<int, 5>({ 4, 3, 3, 4, 3 }), "A4289", "Титов И. В."));
- students.add(Student(array<int, 5>({ 5, 4, 5, 5, 4 }), "A4290", "Егорова А. И."));
- students.add(Student(array<int, 5>({ 3, 3, 4, 4, 5 }), "A4292", "Смирнова О. Г."));
- students.add(Student(array<int, 5>({ 4, 4, 5, 4, 4 }), "A4293", "Павлов В. И."));
- students.add(Student(array<int, 5>({ 5, 5, 5, 5, 5 }), "A4294", "Кац С. Я."));
- students.add(Student(array<int, 5>({ 4, 5, 5, 4, 3 }), "A4296", "Новикова Г. Э."));
- students.save(path);
- }
- auto excellent = students.select(4);
- excellent.streamline();
- excellent.save("excellent.txt");
- excellent.show();
- system("pause > nul");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement