Advertisement
NickAndNick

Database Students (Simple)

Nov 24th, 2022
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | Software | 0 0
  1. #include <algorithm>
  2. #include <array>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <string>
  7. #include <vector>
  8. using namespace std;
  9. struct Student {
  10.     array<int, 5> list{};
  11.     string id;
  12.     string name;
  13.     Student() = default;
  14.     Student(const array<int, 5>& list, const string& id, const string& name)
  15.         : list(list), id(id), name(name) {}
  16.     bool advanced(const int lower_limit)const {
  17.         for (auto score : list) {
  18.             if (score < lower_limit) return false;
  19.         }
  20.         return true;
  21.     }
  22.     void show()const {
  23.         cout << id << ' ' << name << " = ";
  24.         for (auto score : list) cout << score << ' ';
  25.         puts("");
  26.     }
  27. private:
  28.     friend ostream& operator<<(ostream& out, const Student& s) {
  29.         out << s.id << '\n' << s.name << '\n';
  30.         for (auto score : s.list) out << score << ' ';
  31.         out.put('\n');
  32.         return out;
  33.     }
  34.     friend bool operator<(const Student& a, const Student& b) {
  35.         return a.name < b.name;
  36.     }
  37. };
  38. class Students {
  39. public:
  40.     void add(const Student& student) {
  41.         students.push_back(student);
  42.     }
  43.     void add(Student&& student)noexcept {
  44.         students.emplace_back(student);
  45.     }
  46.     bool load(const string& path) {
  47.         ifstream inp(path);
  48.         if (!inp.is_open()) return false;
  49.         students.clear();
  50.         Student s;
  51.         string res;
  52.         while (getline(inp, s.id) && getline(inp, s.name) && getline(inp, res)) {
  53.             istringstream iss(res);
  54.             int score;
  55.             size_t i = 0;
  56.             while (iss >> score) {
  57.                 s.list[i] = score;
  58.                 ++i;
  59.             }
  60.             students.push_back(s);
  61.         }
  62.         inp.close();
  63.         return true;
  64.     }
  65.     bool save(const string& path) {
  66.         ofstream out(path);
  67.         if (!out.is_open()) return false;
  68.         for (const auto& student : students) out << student;
  69.         out.close();
  70.         return true;
  71.     }
  72.     Students select(const int lower_limit)const {
  73.         Students eag;
  74.         for (const auto& student : students) {
  75.             if (student.advanced(lower_limit)) eag.add(student);
  76.         }
  77.         return eag;
  78.     }
  79.     void streamline() {
  80.         sort(students.begin(), students.end());
  81.     }
  82.     void show()const {
  83.         for (const auto& student : students) student.show();
  84.     }
  85. private:
  86.     vector<Student> students;
  87. };
  88. int main() {
  89.     system("chcp 1251 > nul");
  90.     const string path{ "students.txt" };
  91.     Students students;
  92.     if (!students.load(path)) {
  93.         students.add(Student(array<int, 5>({ 5, 4, 5, 4, 4 }), "A4285", "Фролова А. Н."));
  94.         students.add(Student(array<int, 5>({ 4, 5, 4, 4, 5 }), "A4286", "Иванов И. С."));
  95.         students.add(Student(array<int, 5>({ 3, 4, 4, 4, 4 }), "A4287", "Зайцева Е. Ф."));
  96.         students.add(Student(array<int, 5>({ 4, 5, 5, 5, 4 }), "A4288", "Королёв Е. Р."));
  97.         students.add(Student(array<int, 5>({ 4, 3, 3, 4, 3 }), "A4289", "Титов И. В."));
  98.         students.add(Student(array<int, 5>({ 5, 4, 5, 5, 4 }), "A4290", "Егорова А. И."));
  99.         students.add(Student(array<int, 5>({ 3, 3, 4, 4, 5 }), "A4292", "Смирнова О. Г."));
  100.         students.add(Student(array<int, 5>({ 4, 4, 5, 4, 4 }), "A4293", "Павлов В. И."));
  101.         students.add(Student(array<int, 5>({ 5, 5, 5, 5, 5 }), "A4294", "Кац С. Я."));
  102.         students.add(Student(array<int, 5>({ 4, 5, 5, 4, 3 }), "A4296", "Новикова Г. Э."));
  103.         students.save(path);
  104.     }
  105.     auto excellent = students.select(4);
  106.     excellent.streamline();
  107.     excellent.save("excellent.txt");
  108.     excellent.show();
  109.     system("pause > nul");
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement