Last active
May 15, 2023 10:16
-
-
Save alirezaarzehgar/0393f35faab0d88d91d48ec272c07c02 to your computer and use it in GitHub Desktop.
students
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| using namespace std; | |
| class Student | |
| { | |
| private: | |
| int age, score; | |
| string name; | |
| public: | |
| int getAge() | |
| { | |
| return this->age; | |
| } | |
| bool setAge(int age) | |
| { | |
| if (age < 16) | |
| return false; | |
| this->age = age; | |
| return true; | |
| } | |
| int getScore() | |
| { | |
| return this->score; | |
| } | |
| bool setScore(int score) | |
| { | |
| if (score < 0) | |
| return false; | |
| this->score = score; | |
| return true; | |
| } | |
| string getName() | |
| { | |
| return this->name; | |
| } | |
| bool setName(string name) | |
| { | |
| if (name.size() == 0) | |
| return false; | |
| this->name = name; | |
| return true; | |
| } | |
| void show() | |
| { | |
| cout << "Name: " << this->name << endl | |
| << "Age: " << this->age << endl | |
| << "Score: " << this->score << endl; | |
| } | |
| }; | |
| class School | |
| { | |
| private: | |
| vector<Student> students; | |
| public: | |
| void addStudent(Student stu) | |
| { | |
| this->students.push_back(stu); | |
| } | |
| Student* searchByName(string name) | |
| { | |
| for (Student stu : this->students) { | |
| if (stu.getName() == name) { | |
| return new Student(stu); | |
| } | |
| } | |
| return NULL; | |
| } | |
| Student maxScore() | |
| { | |
| int max = -1; | |
| Student maxStu; | |
| for (Student stu : this->students) { | |
| if (stu.getScore() >= max) { | |
| max = stu.getScore(); | |
| maxStu = stu; | |
| } | |
| } | |
| return maxStu; | |
| } | |
| void sortByScore() | |
| { | |
| int stulen = this->students.size(); | |
| for (int i = 0; i < stulen; i++) { | |
| for (int j = 0; j < stulen; j++) { | |
| if (this->students[i].getScore() > this->students[j].getScore()) { | |
| Student tmp = this->students[i]; | |
| this->students[i] = this->students[j]; | |
| this->students[j] = tmp; | |
| } | |
| } | |
| } | |
| } | |
| void showAll() | |
| { | |
| for (Student stu : this->students) { | |
| stu.show(); | |
| } | |
| } | |
| }; | |
| int main() | |
| { | |
| School sch; | |
| int n; | |
| cout << "Enter number of students: "; | |
| cin >> n; | |
| for (int i = 0; i < n; i++) { | |
| int num; | |
| string name; | |
| Student stu; | |
| cout << "Enter name: "; | |
| cin >> name; | |
| if (!stu.setName(name)) { | |
| cout << "Invalid name! Try again." << endl; | |
| i--; | |
| continue; | |
| } | |
| cout << "Enter age: "; | |
| cin >> num; | |
| if (!stu.setAge(num)) { | |
| cout << "Invalid age! Try again." << endl; | |
| i--; | |
| continue; | |
| } | |
| cout << "Enter name: "; | |
| cin >> num; | |
| if (!stu.setScore(num)) { | |
| cout << "Invalid score! Try again." << endl; | |
| i--; | |
| continue; | |
| } | |
| sch.addStudent(stu); | |
| } | |
| string name; | |
| cout << "Search by name: "; | |
| cin >> name; | |
| try { | |
| sch.searchByName(name)->show(); | |
| } catch (exception e) { | |
| cout << "Student " << name << " not found" << endl; | |
| } | |
| cout << "Max score:" << endl; | |
| sch.maxScore().show(); | |
| cout << "Sort all students:" << endl; | |
| sch.sortByScore(); | |
| sch.showAll(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment