Chương trình lý học sinh

Giới thiệu

  • Chương trình quản lý học sinh cơ bản bằng c++, sử dụng struct
  • Để lưu trữ tập hợp sinh viên mà chưa xác định số lượng trước trong bài hướng dẫn này sử dụng vector. Nó cho phép người dùng thêm một lượng tuỳ ý số lượng phần tử và vector sẽ tự quản lý việc cấp phát vùng nhớ (miễn vẫn còn RAM)

File main.cpp

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "student.hpp"
using namespace std;

int main(int argc, const char * argv[]) {

    string action = argv[1];
    if (action == "add"){
        string filePath = argv[2];
        ifstream ifs(filePath, ios::in);
        vector<Student> students;
        if(ifs.is_open()){
            string line = "";
            while(getline(ifs,line)){
                Student student = stringToStudent(line);
                students.push_back(student);
            }
        }
        ifs.close();
        if (saveStudents(students))
            cout << "successed!"<<endl;
        else
            cout << "failed!"<<endl;
    }
    if (action == "find"){
        string keyword = argv[2];
        vector<Student> students = loadStudents();
        vector<Student> foundStudents;
        for (size_t i=0; i<students.size(); i++) {
            if(students[i].id==keyword || students[i].name==keyword){
                foundStudents.push_back(students[i]);
            }
        }
        if(foundStudents.size()>0){
            cout << "Found Students:"<<endl;
            for (size_t i=0; i<foundStudents.size(); i++) {
                cout << (i+1) << " "<<studentToString(foundStudents[i])<<endl;
            }
        }else{
            cout << "Not found!"<<endl;
        }
    }
    if (action == "passed"){
        vector<Student> students = loadStudents();
        vector<Student> foundStudents;
        for (size_t i=0; i<students.size(); i++) {
            if(students[i].gpa>=5){

                foundStudents.push_back(students[i]);
            }
        }
        if(foundStudents.size()>0){
            cout << "Passed Students:"<<endl;
            for (size_t i=0; i<foundStudents.size(); i++) {
                cout << (i+1) << " "<<studentToString(foundStudents[i])<<endl;
            }
        }
    }

    return 0;
}

student.hpp

//
//  student.hpp
//  studentman
//
//  Created by Toan Nguyen on 3/22/20.
//  Copyright © 2020 Toan Nguyen. All rights reserved.
//

#ifndef student_hpp
#define student_hpp
#define SYMBOL '|'
#define FILESYSTEM "students.txt"
#include <string>
#include <vector>
using namespace std;
struct Student{
    string id;
    string name;
    float math;
    float liter;
    float eng;
    float gpa;
};
vector<string> splitString(const string&, char);
Student stringToStudent(const string&);
bool saveStudents(const vector<Student>);
string studentToString(const Student&);
vector<Student> loadStudents();
#endif /* student_hpp */

File Student.cpp

//
//  student.cpp
//  studentman
//
//  Created by Toan Nguyen on 3/22/20.
//  Copyright © 2020 Toan Nguyen. All rights reserved.
//

#include "student.hpp"
#include <iostream>
#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){
        //found
        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;
}
Student stringToStudent(const string& str){
    vector<string> parts = splitString(str, SYMBOL);
    Student student;
    student.id = parts[0];
    student.name = parts[1];
    student.math = stof(parts[2]);
    student.liter = stof(parts[3]);
    student.eng = stof(parts[4]);
    if (parts.size()==5){
        student.gpa = (student.math + student.liter + student.eng)/3;
    }else
        if (parts.size()==6){
            student.gpa = stof(parts[5]);
        }
    return student;
}

bool saveStudents(const vector<Student> students){
    ofstream ofs(FILESYSTEM,ios::out|ios::app);
    bool status = false;
    if (ofs.is_open()){
        for (size_t i=0; i<students.size(); i++) {
            string strStudent = studentToString(students[i]);
            ofs << strStudent<<endl;
        }
        status = true;
    }
    ofs.close();
    return status;
}
string studentToString(const Student& student){
    return student.id+SYMBOL+student.name+SYMBOL+to_string(student.math)+SYMBOL+to_string(student.liter)+SYMBOL+to_string(student.eng)+SYMBOL+to_string(student.gpa);
}
vector<Student> loadStudents(){
    ifstream ifs(FILESYSTEM,ios::in);
    vector<Student> students;
    if(ifs.is_open()){
        string line="";
        while(getline(ifs,line)){
            Student student = stringToStudent(line);
            students.push_back(student);
        }
    }
    ifs.close();
    return students;
}