您好,登錄后才能下訂單哦!
這篇文章主要介紹C++實現學生選課系統的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體內容如下
#include <iostream> #include <iomanip> #include <fstream> #include<Windows.h> #include<cstring> using namespace std; struct SubList/*某個學生所學的課程中的某一個 */ { int num; /*課程代號 */ SubList *next; /*指向下一個課程的指針*/ SubList() :num(-1), next(NULL){} /*構造函數*/ }; struct StuList /*課程中所選的學生*/ { int num; /* 學生的學號*/ float score; /*學生所得的該課程分數*/ StuList *next; /*下一個學生*/ StuList() :num(-1), score(0), next(NULL){} }; class Student { private: int Num; /*學號*/ char Name[20]; /*學生的姓名 */ int MaxSubNum; /*學生最多可以學五門課程 */ int FactSubNum; /* 學生實際所學的課程數目 */ SubList *Root; /*課程的指針*/ float SumXueFen; /*總的選課學分*/ float FactXueFen; /*實際獲得學分*/ float SumGrade; /*總成績*/ bool Update; /*是否需要更新信息*/ public: Student *next;/*讓所有學生的信息連接起來*/ SubList *GetSubPtr()const{ return Root; }/*獲取所選的課程表*/ Student() :Update(false), Root(new SubList()), FactXueFen(0), MaxSubNum(5), FactSubNum(0), SumXueFen(0), SumGrade(0),next(NULL){} void SetName(char N[]){ strcpy(Name, N); } /* 設置學生的姓名 */ void SetNum(int num){ Num = num; } /*設置學號*/ const char* GetName()const{ return Name; }/*得到學生的姓名 */ int GetNum()const{ return Num; } /*得到學號*/ int GetFactSubNum()const{ return FactSubNum; } /*得到實際選課數*/ bool FindSub(int num)const; /*查找是否已有此課程,如果有返回,如果沒有返回 */ void SetInfo(float sumXueFen, float factXueFen, float sumGrade) { SumXueFen = sumXueFen;/*這三條信息因為一起用 所以一起設置了*/ FactXueFen = factXueFen; SumGrade = sumGrade; } void SetUpdate(bool t){ Update = t; }/*是否更新信息的標志*/ float GetAveGrade(){ return SumGrade / FactSubNum; } /*學生課程的平均成績*/ void AddSub(int num); /*增加課程*/ bool IsFull()const{ return MaxSubNum == FactSubNum; } void ShowStuInfo(); /*輸出學生信息*/ void DelSub(int num); /*刪除課程 Manage類使用*/ ~Student()/*析構函數 不要也行影響不太大 一般程序,還不太會內存用完*/ { SubList *p = Root; while (p) { SubList*t = p->next; delete p; p = t; } } }; //課程類 class Subject { private: int MaxStuNum; /*最多學生數*/ int FactStuNum; /*實際學生數*/ float Credit; /*該課程的學分 */ char Name[20]; /* 該課程的名稱 */ int Num; /*課程的代號*/ StuList *Root; /* 學生名單 */ float AveGrade; /*該課程的平均成績 */ bool Update; /*是否更新信息*/ public: StuList *GetStuPtr()const{ return Root; }/*獲取選此課程的學生表*/ Subject*next; Subject() :Root(new StuList()), MaxStuNum(30), FactStuNum(0), Update(false),next(NULL){} float GetCredit()const{ return Credit; } //得到課程的學分 void SetCredit(float credit){ Credit = credit; } //設置學分 const char* GetName()const{ return Name; } //讀出課程的名稱 void SetName(char N[]){ strcpy(Name, N); } //設置課程的名稱 int GetNum()const{ return Num; } //讀出課程的代號 void SetNum(int num){ Num = num; } //設置課程的代號 int GetFactStuNum()const{ return FactStuNum; } //返回實際學生數 int GetMaxStuNum()const{ return MaxStuNum; }//最大學生數 void SetUpdate(bool t){ Update = t; } void SetAveGrade(float num){ AveGrade = num/FactStuNum; } //設置平均分 float GetAveGrade(){ return AveGrade; } //得到學生的平均成績 float GetStuScore(int num); //查找某個學生的成績 bool IsFull()const{ return MaxStuNum == FactStuNum; }//是否人數已滿 void DelStu(int num);//刪除學生 void AddStu(int num);//增加學生 ~Subject() { StuList *p = Root; while (p) { StuList*t = p->next; delete p; p = t; } } }; float Subject::GetStuScore(int num) { StuList*p = Root->next; while (p->num != num) p = p->next; return p->score; } void Student::ShowStuInfo() { cout << Num << " " << Name << " " << "選課數:" << FactSubNum << endl; cout << "\t\t 總成績" << SumGrade << " 平均分" << GetAveGrade() << endl; cout << "\t\t課程總學分" << SumXueFen << " 獲得學分" << FactXueFen << endl; system("pause"); } bool Student::FindSub(int num)const//查找是否已有此課程,如果有返回,如果沒有返回 { bool t = false; SubList *p = Root->next; while (!t && p) { if (p->num == num) t = true; p = p->next; } return t; } void Student::AddSub(int num)//給學生增加一門課 { SubList *s = new SubList(), *p = Root; while (p->next) p = p->next; p->next = s; s->num = num; FactSubNum++; Update = true; } void Student::DelSub(int num) { SubList*p = Root, *t; while (p->next->num != num) p = p->next; t = p->next; p->next = t->next; FactSubNum--; Update = true; delete t; } //////////////////////////Subject////////////////////////////////// void Subject::AddStu(int num)// { StuList *s = new StuList(), *p = Root; while (p->next) p = p->next; p->next = s; s->num = num; FactStuNum++; Update = true; } void Subject::DelStu(int num) { StuList*p = Root, *t; while (p->next->num != num) p = p->next; t = p->next; p->next = t->next; FactStuNum--; Update = true; delete t; } ////////////////////////////////////////////////////////// class Manage { private: Student*StuRoot; Subject*SubRoot; bool Update; public: Manage() :StuRoot(new Student()), SubRoot(new Subject()),Update(false){} Manage(const Manage&p){} void AddStu(); void AddSub(); Student*FindStu(int num); Subject*FindSub(int num); int ShowSub()const;//顯示可選課程 int ShowStu()const; void ShowStuSubInfo(Student*p); void DelStu(Student*p); void DelSub(Subject*p); void Start(); int menu(); int custom(); int server(); Student* password1(); bool password2(); void menu_1_1(Student*p); void menu_2_1(); void menu_2_2(); void menu_2_3(); void menu_2_4(); void menu_2_5(); void menu_2_6(); void menu_2_7(); void menu_2_8(); void menu_2_9(); void IsUpdate() { if (Update == true) { Student*p = StuRoot->next; Subject*q = SubRoot->next; while (q) { float sum = 0; StuList *t =q->GetStuPtr()->next; while (t) { sum += t->score; t = t->next; } q->SetAveGrade(sum); q->SetUpdate(false); q = q->next; } while (p) { float sum = 0, xuefen = 0, xuefen1 = 0; SubList *t = p->GetSubPtr()->next; while (t) { Subject*tt = FindSub(t->num); xuefen += tt->GetCredit(); float f = tt->GetStuScore(p->GetNum()); sum += f; if (f>= 60) xuefen1 += tt->GetCredit(); t = t->next; } p->SetInfo(xuefen, xuefen1, sum); p->SetUpdate(false); p= p->next; } Update = false; } } }; void Manage::DelStu(Student*p) { SubList *l = p->GetSubPtr()->next; while (l) { FindSub(l->num)->DelStu(p->GetNum()); l = l->next; } Student*t = StuRoot; while (t->next != p) t = t->next; t->next = p->next; delete p; Update = true; } void Manage::DelSub(Subject*p) { StuList *l = p->GetStuPtr()->next; while (l) { FindStu(l->num)->DelSub(p->GetNum()); l = l->next; } Subject*t = SubRoot; while (t->next != p) t = t->next; t->next = p->next; delete p; Update = true; } int Manage::ShowSub()const { Subject*p = SubRoot->next; int n = 0; while (p) { cout << p->GetNum() << " " << p->GetName() << " 學分:" << p->GetCredit() << endl; n++; p = p->next; } return n; } int Manage::ShowStu()const { Student*p = StuRoot->next; int n = 0; while (p) { cout << p->GetNum() << " " << p->GetName() << endl; n++; p = p->next; } return n; } Student*Manage::FindStu(int num) { Student*p = StuRoot->next; while (p) { if (p->GetNum() == num) break; p = p->next; } return p; } Subject*Manage::FindSub(int num) { Subject*p = SubRoot->next; while (p) { if (p->GetNum() == num) break; p = p->next; } return p; } //總菜單 int Manage::menu() { int k = 0, n; while (1) { system("cls"); cout << endl << endl; cout << "*******************************************\n" << "* *\n" << "* 學生選修課系統 *\n" << "* *\n" << "* 操作方式: *\n" << "* 1.選修課系統學生端 *\n" << "* *\n" << "* 2.選修課系統管理端 *\n" << "* 0. 退出 *\n" << "*******************************************\n" << endl; cout << "\n\t\t請選擇登入方式: "; cin >> n; if (n > -1 && n < 3) return n; else { cerr << "\n\n\t\t\t\t輸入有誤!\n" << endl; k++; } if (k == 3) { cerr << "\n\n\n\t\t~~提示~~:錯誤輸入次數超過三次,你將被強制退出!!\n\n" << endl; return 0; } system("pause"); } } //選修課系統端操作 int Manage::custom() { int n, k = 0; while (1) { system("cls");; cout << "\n\n\n" << "※※※※※※※※※※※※※※※※※※※※※※※※※※\n" << "※ 選修課系統學生端 ※\n" << "※ ※\n" << "※ 操作方式:. ※\n" << "※ 1、 學生選課 ※\n" << "※ 2、 學生情況 ※\n" << "※ 3、 選課情況 ※\n" << "※ 4、 退出系統 ※\n" << "※ ※\n" << "※※※※※※※※※※※※※※※※※※※※※※※※※※\n" << endl; cout << "\t\t\t請選擇操作方式: "; cin >> n; if (n > 0 && n < 5) return n; else { cerr << "\n\t\t\t\t輸入有誤!請重新輸入\n" << endl; k++; } if (k == 3) { system("cls"); cerr << "錯誤輸入超過三次!你將被強制退出!!\n" << endl; return 4; } system("pause"); } } int Manage::server() { int n, k = 0; while (1) { system("cls");; cout << "\n\n\n" << "※※※※※※※※※※※※※※※※※※※※※※※※※\n" << "※ ※\n" << "※ 選修課系統管理端 ※\n" << "※ ※\n" << "※ 操作方式: ※\n" << "※ 1.增加學生 2.增加課程 ※\n" << "※ 3.刪除學生 4.刪除課程 ※\n" << "※ 5.填寫成績 6.更改學分 ※\n" << "※ 7.學生情況 8.選課情況 ※\n" << "※ 9.保存數據 0.退出系統 ※\n" << "※ ※\n" << "※※※※※※※※※※※※※※※※※※※※※※※※※\n" << endl; cout << "\t\t 請選擇操作方式: " << endl; cin >> n; if (n > -1 && n < 10) return n; else { cerr << "\n\t\t\t\t輸入有誤!\n" << endl; k++; } if (k == 3) { cerr << "錯誤輸入超過三次!\n"; return 0; } system("pause"); } } Student* Manage::password1() { system("cls"); int k = 0; int num; while (1) { cout << "請輸入你的學號:" << endl; cin >> num; Student*p = FindStu(num); if (p == NULL) { cout << "無此學號!!請重新輸入 " << endl; k++; system("pause"); } else { cout << "你的名字為:" << p->GetName() << endl; cout << "請按任意鍵進入選課系統" << endl; return p; } if (k >= 3) { system("cls"); cerr << "\n\n\t\t\t輸入錯誤密碼超過三次!請按任意鍵退出.." << endl; return NULL; } } } //密碼檢查 bool Manage::password2() { int k = 0, i; const char A[] = "admin"; char B[10]; system("cls"); for (i = 0; i < 8; i++) cout << endl; while (1) { printf("\t\t\t請輸入管理員密碼:"); i = 0; cin >> B; if (strcmp(A, B) == 0) return true; else { k++; cerr << "\n\n\t\t\t密碼輸入錯誤!請重新輸入!\n" << endl; system("pause"); } if (k ==3) { system("cls"); cerr << "\n\n\t\t\t輸入錯誤密碼超過三次!請按任意鍵退出.." << endl; return false; } } } void Manage::Start() { int n = 1; while (n) { n = menu(); if (n == 1) { Student*p = password1(); while (p) { int c = custom(); switch (c) { case 1: menu_1_1(p); break;//學生選課 case 2:p->ShowStuInfo(); break;//學生情況 case 3:ShowStuSubInfo(p);; break;//選課情況 } IsUpdate(); if (c == 4) break;//退出系統 } if (p == NULL) n = 0; } else if (n == 2) { bool t = password2(); while (t) { int c = server(); switch (c) { case 1:menu_2_1(); break; //增加學生 case 2:menu_2_2(); break; //增加課程 case 3: menu_2_3(); break; //刪除學生 case 4:menu_2_4(); break; //刪除課程 case 5: menu_2_5(); break; //填寫成績 case 6: menu_2_6(); break; //更改學分 case 7:menu_2_7(); break; //學生情況 case 8:menu_2_8(); break; //選課情況 case 9: menu_2_9(); break; //保存數據 } IsUpdate(); if (c == 0) break; //退出系統 } if (!t) n = 0; } } } //學生端功能函數 void Manage::menu_1_1(Student*p) //學生選課 { if (p->IsFull()) cout << "你的課程已經選滿了" << endl; else { int num; if (ShowSub()==0) cout << "無課程可以選擇" << endl; else { cout << "請輸入你的選擇" << endl; cin >> num; if (p->FindSub(num)) cout << "此課程你已經選擇" << endl; else if (FindSub(num)->IsFull()) cout << "課程人數已滿" << endl; else { p->AddSub(num); FindSub(num)->AddStu(p->GetNum()); cout << "選課成功" << endl; } } } Update = true; system("pause"); } void Manage::ShowStuSubInfo(Student*p) { SubList*l = p->GetSubPtr()->next; cout << p->GetNum() << " " << p->GetName() << " 選課數" << p->GetFactSubNum() << endl; while (l) { Subject*s = FindSub(l->num); cout << "\t\t" << s->GetNum() << " " << s->GetName() << " " << s->GetCredit() << endl; float f = s->GetStuScore(p->GetNum()); cout << "\t\t 成績 " << f << endl; cout << "------------------------------" << endl; l = l->next; } } //管理端功能函數 void Manage::menu_2_1() //增加學生 { system("cls"); int num; char name[20]; cout << "\n\n\t\t\t\t增加學生操作\n" << endl; cout << "\n\n\t\t請輸入學生學號:"; cin >> num; if (FindStu(num)) cout << "學生已經存在" << endl; else { cout << "\n\n\t\t請輸入學生姓名:"; cin >> name; Student*p = new Student(); p->SetNum(num); p->SetName(name); Student *root = StuRoot; while (root->next) root = root->next; root->next = p; cout << endl << "\t\t增加學生操作成功,按任意鍵繼續" << endl; } system("pause"); } void Manage::menu_2_2() //增加課程 { system("cls"); int num; float credit; char name[20]; cout << "\n\n\t\t\t\t增加課程操作\n" << endl; cout << "\n\n\t\t 請輸入課程代號:"; cin >> num; if (FindSub(num)) cout << "\n\t\t此課程已經存在,按任意鍵繼續" << endl; else { cout << "\n\n\t\t請輸入課程名:"; cin >> name; cout << "請輸入課程學分" << endl; cin >> credit; Subject*p = new Subject(); p->SetNum(num); p->SetName(name); p->SetCredit(credit); Subject *root = SubRoot; while (root->next) root = root->next; root->next = p; cout << endl << "\t\t增加課程操作成功,按任意鍵繼續" << endl; } system("pause"); } void Manage::menu_2_3() //刪除學生 { system("cls"); cout << "\n\n\t\t\t\t刪除學生操作" << endl; if (ShowStu()==0) cout << "無學生" << endl; else { int num; cout << "\n\t請輸入要刪除的學生代學號:"; cin >> num; Student*p = FindStu(num); if (p == NULL) cout << "無此學生" << endl; else { DelStu(p); cout << "\n\t\t刪除學生操作成功,按任意鍵繼續.." << endl; } } system("pause"); } void Manage::menu_2_4() //刪除課程 { system("cls"); cout << "\n\n\t\t\t\t刪除課程操作" << endl; if (ShowSub()==0) cout << "無課程" << endl; else { int num; cout << "\n\t請輸入要刪除的課程代號:"; cin >> num; Subject*p = FindSub(num); if (p == NULL) cout << "無此課程" << endl; else { DelSub(p); cout << "\n\t\t刪除課程操作成功,按任意鍵繼續.." << endl; } } system("pause"); } void Manage::menu_2_5() //填寫成績 { system("cls"); cout << "\n\n\t\t\t\t 填寫成績操作\n" << endl; if (ShowSub()==0) cout << "\n\n\n\t\t對不起,暫時沒有任何選修課程。請按任意鍵繼續.." << endl; else { int num; cout << "請選擇課程代號" << endl; cin >> num; Subject*p = FindSub(num); if (p == NULL) cerr << "\n\t\t沒有此課程!!請按任意鍵繼續.." << endl; else { cout << p->GetNum() << " 課程名稱" << p->GetName() << " 選課人數" << p->GetFactStuNum() << endl; int n; cout << "請輸入1確認開始填寫成績,按其他鍵退出" << endl; cin >> n; if (n == 1) { StuList*q = p->GetStuPtr()->next; while (q) { Student*l = FindStu(q->num); cout << "\n\n\t\t請填寫" << q->num << " " << l->GetName() << "的學生成績\n" << endl; cin >> q->score; l->SetUpdate(true); q = q->next; } p->SetUpdate(true); } } } Update = true; system("pause"); } void Manage::menu_2_6() //更改學分 { system("cls"); cout << "\n\n\n\t\t\t\t更改學分操作\n" << endl; if (ShowSub()==0) cout << "\n\n\n\t\t對不起,暫時沒有任何課程。請按任意鍵繼續.." << endl; else { int num; cout << "請選擇課程代號" << endl; cin >> num; Subject*p = FindSub(num); if (p == NULL) cout << "無此課程" << endl; else { float n; cout << "\n\t\t\t原來學分為:" << p->GetCredit() << endl; cout << "\n\t\t\t現要更改為:"; cin >> n; p->SetCredit(n); StuList*q = p->GetStuPtr()->next; while (q) { FindStu(q->num)->SetUpdate(true); q = q->next; } cout << "\n\t\t更改課程學分成功,按任意鍵繼續" << endl; } } Update = true; system("pause"); } void Manage::menu_2_7() { Student *p = StuRoot->next; if (p == NULL) cout << "無記錄" << endl; else while (p) { ShowStuSubInfo(p); p = p->next; } system("pause"); } void Manage::menu_2_8() //選課情況 { system("cls"); cout << "\n\n\t\t\t\t選課情況操作" << endl; if (ShowSub()==0) cout << "\n\n\n\t\t對不起,暫時沒有課程!!請按任意鍵繼續.." << endl; else { int num; cout << "\n\t請輸入要查看的課程代號:"; cin >> num; Subject*p = FindSub(num); if (p == NULL) cout << "\n\t\t無此課程!!\t請按任意鍵繼續.." << endl; else { cout << p->GetNum() << " " << p->GetName() << " " << p->GetCredit() << endl; cout << "\t\t最大人數:" << p->GetMaxStuNum() << " " << "實際人數" << p->GetFactStuNum() << endl; StuList*q = p->GetStuPtr()->next; while (q) { cout <<q->num << " " << FindStu(q->num)->GetName() << " " << q->score << endl; q = q->next; } } } system("pause"); } /*保存數據 不過沒有讀取函數 這個功能就沒有 讀取懶的寫了... */ void Manage::menu_2_9() { Student*p = StuRoot->next; ofstream o("Student.txt",32); ofstream oo("SubList.txt", 32); while (p) { SubList*t=p->GetSubPtr()->next; o.write(reinterpret_cast<char*>(p), sizeof(*p)); while (t) { oo.write(reinterpret_cast<char*>(t), sizeof(*t)); t = t->next; } p = p->next; } o.close(); oo.close(); Subject*q = SubRoot->next; o.open("Subject.txt", 32); oo.open("StuList.txt", 32); while (q) { StuList*t = q->GetStuPtr()->next; o.write(reinterpret_cast<char*>(q), sizeof(*q)); while (t) { oo.write(reinterpret_cast<char*>(t), sizeof(*t)); t = t->next; } q = q->next; } o.close(); oo.close(); cout << "\n\n\n\t\t\t保存數據成功!按任意鍵繼續.." << endl; system("pause"); } int main() { Manage m; m.Start(); return 0; }
以上是“C++實現學生選課系統的方法”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。