(C++)(OOP): Đọc - ghi file Quản lí sinh viên


| Đề bài:

Xây dựng chương trình quản lý sinh viên – Đọc ghi file thông tin sinh viên và tính điểm trung bình.
Chương trình lưu dưới tên QLSV.exe
File dữ liệu chứa danh sách các sinh viên và điểm (toán, văn, anh) của họ
Format: MSSV|Họ tên|Điểm toán|Điểm văn|Điểm anh
Tham khảo file dữ liệu DATA.txt
19A7304|Nguyen Van A|7.0|4.2|2.1
23B2135|Le Ngoc X|3.4|7.8|10
25C1002|Nguyen Van Huan|7|7.7|6.4
20D8459|Nguyen Lan Mai|3.4|1.6|5.6
13G3597|Hoang Thanh Phong|3.5|4.8|7.0
18F7569|Vo Ngoc Thien Phuc|9.0|7.2|6.4
17E2709|Tran Thi B|6.4|2.5|8.9
21Y5908|Nguyen Van A|4.6|7.7|8.4
23B2808|Pham Nhat Loan|6|8.8|9.2
19S5631|Phan Nhu Quynh|4.6|6.4|3

Từ terminal, khi nhập:
·       ./QLSV.exe add <file.txt>
Ví dụ: ./QLSV.exe add DATA.txt
            +) Thực hiện ghi vào file DSSV.txt
                        Với format: MSSV|Họ tên|Điểm toán|Điểm văn|Điểm anh|Điểm trung bình
                        ví dụ:
                          19A7304|Nguyen Van A|9.0|4.2|7.5|6.9
                          17E2709|Tran Thi B|6.4|2.5|5.0|4.6333 
+) Đồng thời in ra màn hình terminal "Successed!" nếu ghi thành công vào file DSSV.txt, ngược lại: "Failed!"

·       ./QLSV.exe find <MSSV> hoặc ./QLSV.exe find <Họ tên>
            +) In ra màn hình terminal (các) dòng thông tin cá nhân và điểm (bao gồm điểm TB) của sinh viên tìm kiếm tương ứng
      với format: STT. MSSV|Họ tên|Điểm toán|Điểm văn|Điểm anh|Điểm trung bình
ví dụ: ./QLSV.exe find 23B2135
                                    >>>>> 1. 23B2135|Le Ngoc X|3.4|7.8|10|7.0666
                        ví dụ: ./QLSV find "Nguyen Van A"
                                    >>>>> 1. 15A0981|Nguyen Van A|6.2|9.8|7.3|7.766666
                                                  2. 17C6517|Nguyen Van A|2.3|8.0|4.5|4.93333

·       ./QLSV.exe passed
+) In ra màn hình terminal các dòng thông tin cá nhân và điểm (bao gồm điểm TB) của sinh viên có điểm TB trên 5
            với format: STT. MSSV|Họ tên|Điểm toán|Điểm văn|Điểm anh|Điểm trung bình
                        ví dụ: ./QLSV.exe passed
                                    >>>> 1. 19A7304|Nguyen Van A|9.0|4.2|7.5
                                               2. 23B2135|Le Ngoc X|3.4|7.8|10|7.0666
                                              3. 15A0981|Nguyen Van A|6.2|9.8|7.3|7.766666




| Source Code

main.cpp

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

// hàm kiểm tra đã ghi file thành công chưa
bool checkWritten(vector<Student>& list)
{
       bool status = false;
       ofstream fileout(FILESYSTEM, ios_base::out | ios_base::app);
       if (fileout.is_open())
       {
              for (size_t i = 0; i < list.size(); i++)
              {
                      list[i].WriteFile(fileout);
              }
              status = true;
       }
       fileout.close();
       return status;
}

// lưu lại mảng các sinh viên
vector<Student> loadStudent()
{
       ifstream f_in(FILESYSTEM, ios_base::in);
       vector<Student> studentlist;
       if (f_in.is_open())
       {
              while (!f_in.eof())
              {
                      Student student;
                      student.ReadFileOut(f_in);
                      studentlist.push_back(student);
              }
       }
       f_in.close();
       return studentlist;
}

int main(int argcconst charargv[])
{
       vector<Student> list; //mảng list chứa thông tin các sinh viên
       string action = argv[1];
       if (action == "add")
       {
              string filePath = argv[2];
              ifstream filein(filePath, ios_base::in);
              if (filein.is_open())
              {
                      while (!filein.eof())
                      {
                             Student student; //khai báo 1 SV kiểu class
                             student.ReadFileIn(filein);
                             list.push_back(student); //thêm thông tin 1 SV vào mảng list
                      }
              }
              filein.close();

       if (checkWritten(list))
              cout << "Successed!" << endl;
       else cout << "Failed!" << endl;
       }

       if (action == "find")
       {
              vector<Student> list = loadStudent();
              vector <Student> foundlist; //mảng foundlist chứa thông tin sinh viên thoả yêu cầu
              string keyword = argv[2];
              for (size_t i = 0; i < list.size(); i++)
              {
                      if (list[i].FindStudent(keyword))
                      {
                             foundlist.push_back(list[i]);
                      }
              }

              if (foundlist.size() > 0)
              {
                      cout << "Found Students:\n";
                      for (size_t i = 0; i < foundlist.size(); i++)
                      {
                             cout << i + 1 << ". ";
                             foundlist[i].PrintInfo();
                             cout << endl;
                      }
              }
              else cout << "Not found!\n";

       }
       if (action == "passed")
       {
              vector<Student> list = loadStudent();
              vector <Student> foundlist;
              for (size_t i = 0; i < list.size(); i++)
              {
                      if (list[i].PassedStudent())
                      {
                             foundlist.push_back(list[i]);
                      }
              }
              if (foundlist.size() > 0)
              {
                      cout << "Passed Students:\n";
                      for (size_t i = 0; i < foundlist.size(); i++)
                      {
                             cout << i + 1 << ". ";
                             foundlist[i].PrintInfo();
                             cout << endl;
                      }
              }
              else cout << "Not found any passed student.\n";
             
       }
       system("pause");
       return 0;
}

student.hpp

#ifndef Student_h
#define Student_h
constexpr auto SYMPOL = '|';
constexpr auto FILESYSTEM = "OUTPUT.txt";
#include<string>
#include<vector>
using namespace std;

class Student
{
private:
       string id;
       string name;
       float math;
       float liter;
       float eng;
       float gpa;
public:

       Student();
       ~Student();
       void ReadFileIn(ifstream&);
       void ReadFileOut(ifstream&);
       void WriteFile(ofstream&);
       bool FindStudent(string);
       void PrintInfo();
       bool PassedStudent();

};
#endif

student.cpp

#include "student.h"
#include<iostream>
#include<fstream>
using namespace std;

Student::Student()
{
       this->id = id;
       this->name = name;
       this->math = math;
       this->liter = liter;
       this->eng = eng;
       this->gpa = 0;
}

Student::~Student()
{
}

//hàm đọc file danh sách sinh viên (chưa có điểm TB)
void Student::ReadFileIn(ifstream& filein)
{
              char x;
              getline(filein, id, SYMPOL);
              getline(filein, name, SYMPOL);
              filein >> math;
              filein >> x;
              filein >> liter;
              filein >> x;
              filein >> eng;
              gpa = (math + liter + eng) / 3;
}

//hàm đọc file danh sách sinh viên (có điểm TB)
void Student::ReadFileOut(ifstream& filein)
{
       char x;
       getline(filein, id, SYMPOL);
       getline(filein, name, SYMPOL);
       filein >> math;
       filein >> x;
       filein >> liter;
       filein >> x;
       filein >> eng;
       filein >> x;
       filein >> gpa;
}

//hàm ghi file danh sách sinh viên
void Student::WriteFile(ofstream& fileout)
{
       fileout << id << SYMPOL << name << SYMPOL << math << SYMPOL << liter << SYMPOL << eng << SYMPOL << gpa;
}

//hàm tìm sinh viên theo tên và id
bool Student::FindStudent(string keyword)
{
       if ((_stricmp(id.c_str(), keyword.c_str()) == 0) || (_stricmp(name.c_str(), keyword.c_str())) == 0)
              return true;
       else return false;
}

// hàm ghi thông tin 1 sinh viên
void Student::PrintInfo()
{
       cout << id + SYMPOL + name + SYMPOL + to_string(math) + SYMPOL + to_string(liter) + SYMPOL + to_string(eng) + SYMPOL + to_string(gpa);
}

// hàm tìm sinh viên có điểm tb trên 5
bool Student::PassedStudent()
{
       if (gpa > 5) return trueelse return false;
}


Nhận xét