Quản lý sinh viên với terminal

Nội dung:

Viết chương trình quản lý lớp học với một số yêu cầu sau (nhập lệnh từ terminal):

find KEY (với KEY là mssv hoặc tên sinh viên) và in ra thông tin của sinh viên

add ma_lop mssv hoten toan van anh (thêm sinh viên vào lớp trùng với ma_lop,
nếu ma_lop là lớp chưa tồn tại thì tại lớp đó và thêm sinh viên vào)

top ma_lop N (in ra N sinh viên điểm trung bình cao nhất tại lớp có mã là ma_lop)

export ma_lop path (nếu có ma_lop thì xuât danh sách thông tin sinh viên
trong lớp có ma_lop ở file được quy định trong path, nếu không có ma_lop thì xuất tất cả sinh viên của tất các lớp ra file quy định trong path)
Biết rằng dữ liệu của phần mềm được lưu trữ theo cấu như sau:
Lop.txt
Ma_lop|TenLop
HTTT2020|He thong thong tin khoa 2020
HTTT2020.txt
MSSV|Ten|Toan|Van|Anh
1000|Nguyen van a| 1.2|5|10

Source:

//
 //  Class.hpp
 //  DemoQLHS
 //
 //  Created by Toan Nguyen on 10/1/20.
 //
 

 #ifndef Class_hpp
 #define Class_hpp
 

 #include <string>
 #include <vector>
 using namespace std;
 class Student;
 class Class{
 private:
     string id;
     string name;
     
     static int incSortComp(const Student* ,const Student*);
 public:
     Class(string,string);
     Class(string);
     
     void set(string,string);
     void save()const;
     string getID()const;
     vector<Student*> getAllStudents() const;
     vector<Student*> top(int N) const;
     
     
     static vector<Class*> getAllClasses();
     static Class* loadClass(string);
 };
 

 #endif /* Class_hpp */
 

//
 //  Class.cpp
 //  DemoQLHS
 //
 //  Created by Toan Nguyen on 10/1/20.
 //
 

 #include "Class.hpp"
 #include "Student.hpp"
 #include "ultils.hpp"
 #include <algorithm>
 #include <iostream>
 using namespace std;
 Class::Class(string id, string name){
     this->set(id, name);
 }
 Class::Class(string str){
     vector<string> substrs = splitString(str, '|');
     this->set(substrs[0], substrs[1]);
 }
 void Class::set(string id, string name){
     this->id = id;
     this->name = name;
 }
 vector<Class*> Class::getAllClasses(){
     vector<string> lines = readAllLinesFromFile("lop.txt");
     vector<Class*> classes;
     for (int i=0; i<lines.size(); i++) {
         classes.push_back(new Class(lines[i]));
     }
     return classes;
 }
 

 vector<Student*> Class::getAllStudents()const{
     vector<Student*> students;
     string filename = this->id + ".txt";
     vector<string> strStudents = readAllLinesFromFile(filename);
     for (int i=0; i < strStudents.size(); i++) {
         students.push_back(new Student(strStudents[i]));
     }
     
     
     return students;
 }
 

 string Class::getID()const{
     return this->id;
 }
 void Class::save()const{
     saveString(this->id+"|"+this->name
                , "lop.txt");
 }
 vector<Student*> Class::top(int N)const{
     vector<Student*> students = Class::getAllStudents();
     sort(students.begin(), students.end(), Class::incSortComp);
     N = std::min(N, (int)students.size());
     vector<Student*> tops;
     for (int i=0; i<N; i++) {
         tops.push_back(students[i]);
     }
     return tops;
 }
 int Class::incSortComp(const Student* std1,const Student* std2){
     return std1->getSum() > std2->getSum();
 }
 

 Class *Class::loadClass(std::string clsId) {
     vector<Class*> classes = Class::getAllClasses();
     for (Class* cls : classes) {
         if(cls->getID()==clsId)
             return cls;
     }
     return NULL;
 }
 
//
 //  Program.hpp
 //  DemoQLHS
 //
 //  Created by Toan Nguyen on 10/1/20.
 //
 

 #ifndef Program_hpp
 #define Program_hpp
 #include <vector>
 #include <string>
 #include "Student.hpp"
 #include "Class.hpp"
 using namespace std;
 class Program{
 public:
     static vector<Student*> find(string key);
     static void printStudent(const vector<Student*> &students);
     static void insertStudent(const Student&);
     static vector<Student*> top(int,string);
 

 };
 

 #endif /* Program_hpp */
 

//
 //  Program.cpp
 //  DemoQLHS
 //
 //  Created by Toan Nguyen on 10/1/20.
 //
 

 #include "Program.hpp"
 #include <iostream>
 using namespace  std;
 vector<Student*> Program::find(string key){
     vector<Student*> foundStudents;
     vector<Class*> classes = Class::getAllClasses();
     for (int i =0; i< classes.size(); i++) {
         vector<Student*> students = classes[i]->getAllStudents();
         for (int j =0; j< students.size(); j++) {
             if(students[j]->getID() == key || students[j]->getName() == key){
                 foundStudents.push_back(students[j]);
             }
         }
     }
     
     for(int i=0; i< classes.size();i++){
         delete classes[i];
     }
     return foundStudents;
 }
 

 void Program::printStudent(const vector< Student * > &students) {
     for (int i=0; i < students.size(); i++) {
         cout << students[i]->toString() << endl;
     }
 }
 void Program::insertStudent(const Student& s){
     s.save();
     
 }
 vector<Student*> Program::top(int N, string clsId){
     Class* cls = Class::loadClass(clsId);
     return cls->top(N);
     
 }
 

//
 //  Student.hpp
 //  DemoQLHS
 //
 //  Created by Toan Nguyen on 10/1/20.
 //
 

 #ifndef Student_hpp
 #define Student_hpp
 #include <string>
 #include "Class.hpp"
 using namespace std;
 class Student{
 private:
     string name;
     string id;
     float math;
     float liter;
     float eng;
     Class * cls;
 public:
     Student(string);
     string getName()const;
     string getID()const;
     string toString()const;
     const Class* getClass()const;
     void setClass(Class*);
     void save() const;
     float getSum()const;
 };
 

 #endif /* Student_hpp */
 

//
 //  Student.cpp
 //  DemoQLHS
 //
 //  Created by Toan Nguyen on 10/1/20.
 //
 

 #include "Student.hpp"
 #include "ultils.hpp"
 Student::Student(string str){
     vector<string> substrs = splitString(str, '|');
     this->id = substrs[0];
     this->name = substrs[1];
     this->math = stof(substrs[2]);
     this->liter = stof(substrs[3]);
     this->eng = stof(substrs[4]);
 }
 

 string Student::getName()const{return this->name;}
 string Student::getID()const{return this->id;}
 string Student::toString()const{
     return this->getID() + "|" + this->getName()+"|"+to_string(this->math)+"|"+to_string(this->liter)+"|"+to_string(this->eng);
 }
 const Class* Student::getClass()const{
     return this->cls;
 }
 void Student::setClass(Class* cls){
     this->cls = cls;
 }
 void Student::save()const{
     vector<Class*> classes = Class::getAllClasses();
     bool isExisted = false;
     for (int i=0; i<classes.size(); i++) {
         if(classes[i]->getID()==this->cls->getID()){
             isExisted = true;
             break;
         }
     }
     if(!isExisted){
         this->cls->save();
     }
     saveString(this->toString(),this->cls->getID()+".txt");
 }
 float Student::getSum()const{
     return this->math + this->liter + this->eng;
 }
 

//
 //  ultils.hpp
 //  DemoQLHS
 //
 //  Created by Toan Nguyen on 10/1/20.
 //
 

 #ifndef ultils_hpp
 #define ultils_hpp
 #include <vector>
 #include <string>
 using namespace std;
 vector<string> splitString(const string& str, char c);
 vector<string> readAllLinesFromFile(const string& file);
 void saveString(const string& str, const std::string& file);
 

 #endif /* ultils_hpp */
 

//
 //  ultils.cpp
 //  DemoQLHS
 //
 //  Created by Toan Nguyen on 10/1/20.
 //
 

 #include "ultils.hpp"
 #include <string>
 #include <fstream>
 using namespace std;
 vector<string> splitString(const string& str, char c){
     vector<string> parts;
     size_t start = 0;
     size_t symbolPos = str.find(c,start);
     while(symbolPos != string::npos){
         string part = str.substr(start,symbolPos-start);
         parts.push_back(part);
         start = symbolPos+1;
         symbolPos = str.find(c,start);
     }
     string lastPart = str.substr(start,str.length()-start);
     parts.push_back(lastPart);
     return parts;
 }
 vector<string> readAllLinesFromFile(const string& file){
     ifstream ifs(file, ios::in);
     vector<string> lines;
         if(ifs.is_open()){
             string line = "";
             while(getline(ifs,line)){
                 lines.push_back(line);
             }
         }
     
     return lines;
 }
 void saveString(const string& str, const std::string& file){
     ofstream ofs;
     ofs.open(file,ios::out|ios::app);
     if(ofs.is_open()){
         ofs << str;
     }
     ofs.close();
 }
 

//
 //  main.cpp
 //  DemoQLHS
 //
 //  Created by Toan Nguyen on 10/1/20.
 //
 

 #include "Program.hpp"
 #include <iostream>
 using namespace std;
 int main(int argc, char** argv){
     string action = argv[1];
     if(action == "find"){
         string key = argv[2];
         vector<Student*> students = Program::find(key);
         Program::printStudent(students);
         for (int i=0; i<students.size(); i++) {
             delete students[i];
         }
     }
     if(action == "add"){
         Student s(argv[2]);
         s.setClass(new Class(argv[3]));
         Program::insertStudent(s);
     }
     if(action == "top"){
         int N = stoi(argv[2]);
         string clsId = argv[3];
         vector<Student*> students =Program::top(N,clsId);
         Program::printStudent(students);
         for (int i=0; i<students.size(); i++) {
             delete students[i];
         }
     }
     return 0;
 }