1. 程式人生 > >用C++ STL 實現成績管理系統

用C++ STL 實現成績管理系統

#include <iostream>
#include <sstream> //字串轉換型別
#include <list>    
#include <string>
#include <vector>
#include <fstream> //登入,列印成績 

using namespace std;

class Student
{
private:
    string m_strNO;         //學號
    string m_strName;       //姓名
    string m_strUniversity; //大學
int m_nTotal; //成績 public: Student() { m_strNO = "0"; m_strName = "0"; m_strUniversity = "0"; m_nTotal = 0; } Student(string strNO , string strName, string strUniversity, int nTotal) : m_strNO(strNO), m_strName(strName), m_strUniversity(strUniversity), m_nTotal(nTotal) { } bool
operator < (Student& s) { return m_strNO < s.GetNO(); } bool operator == (Student& s) { return m_strNO == s.GetNO(); } string GetNO() { return m_strNO; } string GetName() { return m_strName; } string GetUniversity() { return m_strUniversity; } int
GetTotal() { return m_nTotal; } }; class Log { private: int m_type; //學生登入或者老師登入 string m_strUser; //賬號 string m_strPassword; //密碼 public: Log() { m_type = 0; m_strUser = "0"; m_strPassword = "0"; } Log(int type,string strUser, string strPassword) : m_type(type), m_strUser(strUser), m_strPassword(strPassword){} bool operator < (Log& s) { return m_strUser < s.GetUser(); } bool operator == (Log& s) { return m_strUser == s.GetUser(); } int GetType() { return m_type; } string GetUser() { return m_strUser; } string GetPassword() { return m_strPassword; } }; ostream& operator << (ostream& os, Student& s) { os << s.GetNO() << "\t" << s.GetName() << "\t" << s.GetUniversity() << "\t" << s.GetTotal(); return os; } ostream& operator << (ostream& os, Log& s) { os << s.GetType() << "\t" << s.GetUser() << "\t" << s.GetPassword(); return os; } typedef list<Student> LISTSTUD; typedef LISTSTUD::iterator iterator; typedef vector<Log> VECTORLOG; class StudManage { private: VECTORLOG m_LogVector; //登入容器 LISTSTUD m_stlList; //學生連結串列 string strNO; //學號 string strName; //姓名 string strUniversity; //大學 int nTotal; //成績 int type; //學生登入或者老師登入 string strUser; //賬號 string strPassword; //密碼 public: void Log_init() { /*將檔案的型別、賬號和密碼插入容器中*/ ifstream All_user; All_user.open("Alluser.txt"); if (!All_user) { cout << "開啟檔案失敗" << endl; } while (All_user >> type >> strUser >> strPassword) { const Log s(type, strUser, strPassword); m_LogVector.push_back(s); } //for (VECTORLOG::iterator it = m_LogVector.begin(); it != m_LogVector.end(); it++) //{ // cout << *it << endl; //}//單元測試是否加入了登入的庫 All_user.close(); } bool Log_In(int type,string strUser,string strPassword) { /*從容器中查詢是否有匹配的值*/ stringstream m_ctoi; int m_1; int m_2; int m_3; int m_4; VECTORLOG::iterator it = m_LogVector.begin(); while (it != m_LogVector.end()) { Log& s = *it; m_ctoi << s.GetUser(); m_ctoi >> m_1; m_ctoi.clear(); m_ctoi << strUser; m_ctoi >> m_2; m_ctoi.clear(); m_ctoi << s.GetPassword(); m_ctoi >> m_3; m_ctoi.clear(); m_ctoi << strPassword; m_ctoi >> m_4; m_ctoi.clear(); if (type == s.GetType() && m_1 == m_2 && m_3 == m_4) { return true; } ++it; } if (it == m_LogVector.end()) { return false; } return true; } bool Add() //從 Allstudent表 中新增學生 { ifstream All_student; All_student.open("Allstudent.txt"); if (!All_student) { cout << "開啟檔案失敗" << endl; } while (All_student >> strNO >> strName >> strUniversity >> nTotal) { const Student s(strNO, strName, strUniversity, nTotal); m_stlList.push_back(s); } All_student.close(); return true; } bool Merge(StudManage& stud) // 如果有兩張表可以進行合併 { m_stlList.sort(); stud.m_stlList.sort(); //將兩張表分別排序 m_stlList.merge(stud.m_stlList);//歸併排序 m_stlList.unique();//去重 return true; } void Merge() //插入之後進行排序(去重) { fstream Merge_student; Merge_student.open("Allstudent.txt", ios::in | ios::out | ios::trunc); if (!Merge_student) { cout << "開啟檔案失敗" << endl; } m_stlList.sort();//排序 m_stlList.unique();//去重 for (LISTSTUD::iterator it = m_stlList.begin(); it != m_stlList.end(); it++) { Merge_student << *it << endl; } Merge_student.close(); } void Show_All() //顯示學生(教師功能) { for (LISTSTUD::iterator it = m_stlList.begin(); it != m_stlList.end(); it++) { cout << *it << endl; } } void Add_Student() //新增學生(教師功能) { fstream Add_student; Add_student.open("Allstudent.txt", ios::in | ios::out | ios::trunc); if (!Add_student) { cout << "開啟檔案失敗!!" << endl; } cin>> strNO >> strName >> strUniversity >> nTotal; const Student s(strNO,strName,strUniversity,nTotal); m_stlList.push_back(s); for (LISTSTUD::iterator it = m_stlList.begin(); it != m_stlList.end(); it++) { Add_student << *it << endl; } Add_student.close(); cout << "插入成功!" << endl; } void Delete_Student() //刪除學生(教師功能) { string Delete_student_No; /* 按學號刪除 */ cin >> Delete_student_No; stringstream m_ctoi; bool bFind = false; int m_1; int m_2; fstream Delete_stu; Delete_stu.open("Allstudent.txt", ios::in | ios::out | ios::trunc); if (!Delete_stu) { cout << "開啟檔案失敗!!" << endl; } LISTSTUD::iterator it = m_stlList.begin(); while (it != m_stlList.end()) { Student& s = *it; m_ctoi << s.GetNO(); m_ctoi >> m_1; m_ctoi.clear(); m_ctoi << Delete_student_No; m_ctoi >> m_2; m_ctoi.clear(); if (m_1 == m_2) // 字串轉換一下 { bFind = true; m_stlList.erase(it); cout << " 已經成功刪除! " << endl; Delete_stu.close(); break; } ++it; if (it == m_stlList.end()) { cout << " 對不起,沒有要刪除的同學資訊 " << endl; } } for (LISTSTUD::iterator it = m_stlList.begin(); it != m_stlList.end(); it++) { Delete_stu << *it << endl; } } void Show_Student(string strNO) //學生查詢個人成績(學生功能) { stringstream m_ctoi; bool bFind = false; int m_1; int m_2; LISTSTUD::iterator it = m_stlList.begin(); while (it != m_stlList.end()) { Student& s = *it; m_ctoi << s.GetNO(); m_ctoi >> m_1; m_ctoi.clear(); m_ctoi << strNO; m_ctoi >> m_2; m_ctoi.clear(); if (m_1 == m_2) // 字串轉換一下 { bFind = true; cout << *it << endl; break; } ++it; } if(it == m_stlList.end()) { system("cls"); cout << " 沒有這個同學 " << endl; } } void Printf_Grade()//列印所有人的成績(教師功能) { fstream printf_grade; printf_grade.open("Allstudent.txt", ios::in | ios::out | ios::trunc); if (!printf_grade) { cout << "開啟檔案失敗!!" << endl; } for (LISTSTUD::iterator it = m_stlList.begin(); it != m_stlList.end(); it++) { printf_grade << *it << endl; } printf_grade.close(); cout << "列印成功!!" << endl; }//列印成績 }; int main() { stringstream ctoi; stringstream itoc; int Log_in = 0; int Teacher_n = 0; int Student_n = 0; int Log_num = 0; string Student_No; string Log_Str_User; string Log_Str_Password; cout << "****************************************************" << endl; cout << "|****************學校成績管理系統******************|" << endl; cout << "| |" << endl; cout << "| 1.老師登入 2.學生登入 其他.退出 |" << endl; cout << "| |" << endl; cout << "| |" << endl; cout << "| 請輸出(1 或 2) : | "<< endl; cout << "****************************************************" << endl; /****************************建立原始表***********************************/ StudManage sm1; sm1.Add(); sm1.Merge(); sm1.Log_init(); /***************************************************************************/ while (1) { cin >> Log_in; switch (Log_in) { case 1: system("cls"); if(Log_num == 0) { for (Log_num = 1; Log_num < 4; Log_num++) { cout << "請輸入第" << Log_num << "次密碼(只能輸入3次):"<< endl ; cin >> Log_Str_User >> Log_Str_Password; if (sm1.Log_In(Log_in, Log_Str_User, Log_Str_Password)) { break; } }//(Log_num = 1; Log_num < 4; Log_num++) if (Log_num > 3) { cout << "對不起輸入超過限制,稍後重試!!" << endl; system("pause"); return 0; } }//(Log_num == 0) while (1) { system("cls"); cout << "*************************學校成績管理系統*************************" << endl; cout << "| 歡迎教師登入 |" << endl; cout << "|****************************************************************|" << endl; cout << "|1.插入成績 2.刪除成績 3.查詢成績 4.修改成績 5.列印成績 其他.退出|" << endl; cout << "| |" << endl; cout << "| |" << endl; cout << "| 請輸出(1 、2 、3、 4 或 5: ) : |" << endl; cout << "******************************************************************" << endl; cin >> Teacher_n; switch (Teacher_n) { case 1: system("cls"); cout << "*****************學校成績管理系統***************" << endl; cout << "************************************************" << endl; cout << " 請輸入學生的資訊(學號,姓名,學校,總分) :" << endl; sm1.Add_Student(); sm1.Merge(); Teacher_n = 0; system("pause"); break; case 2: system("cls"); cout << "******************學校成績管理系統**************" << endl; cout << "************************************************" << endl; cout << " 請輸入要刪除的學生(學號) :" << endl; sm1.Delete_Student(); Teacher_n = 0; system("pause"); break; case 3: system("cls"); cout << "****************學校成績管理系統***************" << endl; cout << "************************************************" << endl; sm1.Show_All(); Teacher_n = 0; system("pause"); break; case 4: break; case 5: system("cls"); sm1.Printf_Grade(); Teacher_n = 0; system("pause"); break; default: system("cls"); Teacher_n = 0; cout << "程式退出!!" << endl; system("pause"); return 0; }//while (1) } break; //case 1: case 2: system("cls"); if (Log_num == 0) { for (Log_num = 1; Log_num < 4; Log_num++) { cout << "請輸入第" << Log_num << "次密碼(只能輸入3次):" << endl; cin >> Log_Str_User >> Log_Str_Password; if (sm1.Log_In(Log_in, Log_Str_User, Log_Str_Password)) { break; } }//(Log_num = 1; Log_num < 4; Log_num++) if (Log_num > 3) { cout << "對不起輸入超過限制,稍後重試!!" << endl; system("pause"); return 0; } }//(Log_num == 0) while (1) { system("cls"); cout << "*************************學校成績管理系統***********************" << endl; cout << "| |" << endl; cout << "| 歡迎學生登入 |" << endl; cout << "|**************************************************************|" << endl; cout << "| |" << endl; cout << "| 1.查詢成績 其他.退出 |" << endl; cout << "| |" << endl; cout << "| 請輸出數字 1 : |" << endl; cout << "****************************************************************" << endl; cin >> Student_n; switch (Student_n) { case 1: cout << "請輸入查詢學號" << endl; cin >> Student_No; system("cls"); sm1.Show_Student(Student_No); Student_n = 0; system("pause"); break; default: Student_n = 0; system("pause"); return 0; } }// while (1) break; // case 2: default: cout << "程式退出!!" << endl; system("pause"); return 0; } // switch (n) }//while (1) cout << "程式退出!!" << endl; system("pause"); return 0; }

Allstudent.txt

1   1   1   1
10  hexin   shan    100
2   2   2   2
3   3   3   3
5   5   5   5
6   6   6   6
7   7   77  7
8   8   8   88

Alluser.txt

1 1 1
2 2 2