日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

学生管理系统(C++)

發(fā)布時間:2025/3/21 c/c++ 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学生管理系统(C++) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

引言

學(xué)習(xí)c++后一直想寫個學(xué)生管理系統(tǒng),趁假期花了一天終于肝出了完整代碼,讓我對c++的理解更深了一層;寫代碼時真正體會到了內(nèi)存操作的困難(想學(xué)java),但是還是通過幾個巧妙的方法解決了內(nèi)存問題,并在n次debug后終于調(diào)試出了完整代碼,接下來我來介紹一下整個系統(tǒng);
整個系統(tǒng)面向的對象是學(xué)生,所以就先寫了一個學(xué)生類,并賦予了基本屬性:學(xué)號,姓名,班級職責(zé),還有顯示個人信息的一個行為,又把學(xué)生分為三部分,普通學(xué)生班干部班長,當(dāng)然這三類都具有學(xué)生的特性,所以我們可以通過繼承學(xué)生類來獲得相同的屬性和行為;
想好了對象就要想一下具體實現(xiàn)的功能,該系統(tǒng)分為七大功能,分別是:

0.退出管理程序
1.增加學(xué)生信息
2.顯示學(xué)生信息
3.刪除學(xué)生信息
4.修改學(xué)生信息
5.查找學(xué)生信息
6.清空所有數(shù)據(jù)

為了實現(xiàn)這些功能,設(shè)計了一個管理員類StudentManager,將這些行為封裝在一起,然后依次實現(xiàn)就行了;
這時就需要考慮一些細節(jié)了,在增加學(xué)生信息時,我們該如何增加呢?我在StudentManager這個類中增添了一個屬性,一個vector的容器來存放增加的學(xué)生,這時就有問題了,學(xué)生既然分成了三大類,我們并不知道每次到底增加的是哪一類學(xué)生,那么vector到底該定位存放哪種學(xué)生類型呢?
這時面向?qū)ο笕筇匦灾坏亩鄳B(tài)的優(yōu)勢就顯現(xiàn)了出來,因為普通學(xué)生班干部班長這三類已經(jīng)繼承了學(xué)生類,所以我們只需要將學(xué)生類寫為抽象類即可(把顯示個人信息函數(shù)showinfo寫為純虛函數(shù)),這樣我們vector容器只需要記錄學(xué)生類的指針,通過多態(tài)的特性就可以實現(xiàn)同時存放普通學(xué)生班干部班長這三類的指針了;

構(gòu)思完成后就可以寫代碼了,在寫的過程中也遇到過很多問題,說幾個主要的
1,因為我想要將學(xué)生信息每次更新后都可以存放到文件中,所以我不僅僅需要寫一個save保存函數(shù),還需要一個文件初始化函數(shù)init_std,因為一旦這次的代碼運行成功后,學(xué)生信息就會存放到文件中,但是下一次運行就會將這次的學(xué)生信息覆蓋,于是我們只要在程序開始運行時文件存在的情況下調(diào)用這個init函數(shù),將文件原本的信息先讀取到并存放到vector中,就可以實現(xiàn)對上一次信息的保存;這里都是些空間分配的問題,都是小細節(jié),就不提了(鬧心)
2,這個代碼中的幾個地方我一直避免使用delete釋放內(nèi)存,因為一旦delete就會出現(xiàn)一個warning警告,因為數(shù)據(jù)存放到了堆區(qū),我猜測是抽象類Student和繼承它的子類析構(gòu)函數(shù)的問題,所以我先在Student中加了一個純虛析構(gòu)函數(shù),又分別在下面幾個類中重寫了析構(gòu)函數(shù),雖然解決了warning,但是代碼一走到delete就無法正常運行了;但是在最后還要清空所有數(shù)據(jù),這當(dāng)然包括分配的內(nèi)存空間,還有什么辦法?我突然想到了vector容器有一個swap函數(shù),這個函數(shù)功能是交換兩個vector容器的內(nèi)容,但是我們可以用它的特點壓縮內(nèi)存,當(dāng)然這需要用一個巧妙的方法實現(xiàn),感興趣的可以了解一下,我是在clean中使用了該功能

vector<Student*>(*this -> m_StdArray).swap(*this -> m_StdArray);

完成了內(nèi)存的釋放;
3,其他的問題還有很多,但是現(xiàn)在看來都也沒有什么大不了的,只要在寫代碼時能夠細心點,一定要可以熟練度的使用指針,在代碼中我通過vector來存放增加的學(xué)生,這個相當(dāng)于一個二級指針數(shù)組,所以再空間分配和調(diào)用時多多少少出現(xiàn)了很多小問題,浪費了我很多時間,尤其是加上this指針后,我很多時候就是蒙圈狀態(tài),但是最后還是調(diào)試出來了😊;

總結(jié):

這是我第一次寫這種對我來說稍稍大點的項目,讓我感觸最深的就是在完成整個代碼中需要統(tǒng)籌全局,定義的每一個類都需要恰到好處,類與類之間的聯(lián)系都需要嚴謹考慮,代碼的每個函數(shù)都是非常好寫的,但是又該如何整合起來?函數(shù)之間又有什么樣的聯(lián)系?每個函數(shù)又該放到哪個位置?我需要多定義那些變量?這些都是需要我考慮的;這比平時做題所需要考慮的方面多了好多好多,尤其是面向?qū)ο缶幊讨蟹庋b起來就是一個整體,所以需要更全面的考慮和數(shù)次的調(diào)試;還有對內(nèi)存的操控也更加困難,這就需要扎實的基本功了;我不能保證現(xiàn)在我代碼的完美,但是對我來說這是一次非常好的歷練,當(dāng)然如果有代碼方面的問題也歡迎在評論區(qū)提出來。

注:我是在vscode上編譯運行的,所有功能正常,但是在dev上部分功能就會出現(xiàn)問題,所以要注意下編譯環(huán)境;

代碼如下:

#include <iostream> #include<fstream> #include<cstring> #include<vector> using namespace std;#define FILENAME "stdFILE.txt" //定義生成文件名稱//學(xué)生類(抽象類) class Student { public:virtual void showInfo() = 0; //顯示個人信息// virtual ~Student() = 0; //定義純虛析構(gòu)函數(shù)保證釋放堆區(qū)內(nèi)存時不會發(fā)生內(nèi)存泄漏string StId; //學(xué)號string m_name; //姓名string m_Dept; //職責(zé) }; //普通學(xué)生類 class Nomalstuden : public Student { public:Nomalstuden(string Id, string name, string dep);void showInfo(); //顯示個人信息// ~Nomalstuden() {// delete this;// } }; //班長類 class ClassPresident : public Student { public: ClassPresident(string Id, string name, string dep);void showInfo ();// ~ClassPresident() {// delete this;// } }; //班干部類 class Classleader : public Student { public: Classleader(string Id, string name, string dep);void showInfo ();// ~Classleader() {// delete this;// } };//管理員類 class StudentManager { public:StudentManager();//構(gòu)造函數(shù)void Show_Menu(); //打印菜單界面void Exit_System();//退出系統(tǒng)void Addinfo(); //增加學(xué)生信息void save(); //將學(xué)生信息保存到文件中void init_Std(); //初始化學(xué)生void show_Std(); //顯示學(xué)生信息void del_Std(); //刪除學(xué)生信息void mod_Std(); //修改學(xué)生信息void find_Std(); //查找學(xué)生信息void clean_File(); //清空文件int IsExist(string id); //判斷學(xué)號為id的學(xué)生信息是否存在,并返回該學(xué)生下標(biāo)~StudentManager();//析構(gòu)函數(shù)vector<Student*> *m_StdArray; //存放增加的學(xué)生信息bool m_fileIsEmpty; //標(biāo)記文件是否為空 };//學(xué)生類純虛析構(gòu)的外部實現(xiàn) // Student :: ~Student() { // delete this; // }//管理員函數(shù)實現(xiàn) StudentManager :: StudentManager() {ifstream ifs;ifs.open(FILENAME, ios :: in);//如果文件不存在if (!ifs.is_open()) {cout << "該文件不存在!" << endl;this -> m_fileIsEmpty = true;this -> m_StdArray = NULL;ifs.close();return ;}//文件存在但是數(shù)據(jù)為空char ch; ifs >> ch; //先讀取一個字符if (ifs.eof()) {cout << "該文件為空!" <<endl;this -> m_fileIsEmpty = true;this -> m_StdArray = NULL;ifs.close();return ;}//文件存在,并記錄初始數(shù)據(jù)this -> m_StdArray = new vector<Student*>;this -> init_Std(); } void StudentManager :: Show_Menu() {cout << "-------------------------------------------" << endl;cout << "------------ 歡迎使用學(xué)生管理系統(tǒng)! ----------" << endl;cout << "------------- 0.退出管理程序 -------------" << endl;cout << "------------- 1.增加學(xué)生信息 -------------" << endl;cout << "------------- 2.顯示學(xué)生信息 -------------" << endl;cout << "------------- 3.刪除學(xué)生信息 -------------" << endl;cout << "------------- 4.修改學(xué)生信息 -------------" << endl;cout << "------------- 5.查找學(xué)生信息 -------------" << endl;cout << "------------- 6.清空所有數(shù)據(jù) -------------" << endl;cout << "-------------------------------------------" << endl;cout << endl; } void StudentManager :: Exit_System() {cout << "感謝您的使用!" << endl;// system("pause");exit(-1); //退出系統(tǒng) } void StudentManager :: Addinfo() {if (!this -> m_StdArray)this -> m_StdArray = new vector<Student*>;cout << "學(xué)生信息開始錄入" << endl;int i = 1; while(true) {char flag; string id;string name;string dep;cout << "請輸入第" << i << "個學(xué)生學(xué)號:" << endl;cin >> id;cout << "請輸入第" << i << "個學(xué)生姓名:" << endl;cin >> name;cout << "請輸入第" << i << "個學(xué)生職位:(班長or班干部or普通學(xué)生)" << endl;cin >> dep;Student *std = NULL;if (dep == "班長") {std = new ClassPresident(id, name, dep);}else if (dep == "班干部") {std = new Classleader(id, name, dep);}else if (dep == "普通學(xué)生") {std = new Nomalstuden(id, name, dep);}else {cout << "該學(xué)生職位不存在!信息錄入結(jié)束!" <<endl;break;}this -> m_StdArray -> push_back(std);i++;this -> m_fileIsEmpty = false; //更新文件非空標(biāo)記cout << "是否繼續(xù)錄入信息?(y繼續(xù)錄入,n結(jié)束錄入)" <<endl;cin >> flag;if (flag == 'y') continue;else break;}cout << "成功添加了" << i - 1 << "名學(xué)生信息!" <<endl;this -> save();system("pause");system("cls"); } void StudentManager :: save() {ofstream ofs;ofs.open(FILENAME, ios :: out);for (int i = 0; i < this -> m_StdArray -> size(); ++i) {ofs << this -> m_StdArray -> at(i) -> StId << " "<< this -> m_StdArray -> at(i) -> m_name << " "<< this -> m_StdArray -> at(i) -> m_Dept << endl;}ofs.close(); } void StudentManager :: init_Std() {ifstream ifs;ifs.open(FILENAME, ios :: in);string id;string name;string dep;while (ifs >> id && ifs >> name && ifs >> dep) {Student * std = NULL;if (dep == "班長") {std = new ClassPresident(id, name, dep);}else if (dep == "班干部") {std = new Classleader(id, name, dep);}else if (dep == "普通學(xué)生") {std = new Nomalstuden(id, name, dep);}this -> m_StdArray -> push_back(std);}this -> save(); } void StudentManager :: show_Std() {if (this -> m_fileIsEmpty) {cout << "文件不存在或者文件為空!" <<endl;}else {for (int i = 0; i < this -> m_StdArray -> size(); ++i) {this -> m_StdArray -> at(i) -> showInfo();}}system("pause");system("cls"); } void StudentManager :: del_Std() {if (this -> m_fileIsEmpty) {cout << "文件不存在或者文件為空!" << endl;}else {cout << "請輸入需要刪除的學(xué)生學(xué)號:" << endl;string id;cin >>id;int index = this -> IsExist(id);if (index != -1) {this -> m_StdArray -> erase(this -> m_StdArray -> begin() + index);this -> save();cout << "刪除成功!" <<endl;}else {cout << "刪除失敗,不存在該學(xué)號的學(xué)生!" <<endl;}}system("pause"); } int StudentManager :: IsExist(string id) {int len = this -> m_StdArray -> size();int index = -1;for (int i = 0; i < len; ++i) {if (this -> m_StdArray -> at(i) -> StId == id) {index = i;break;}}return index; } void StudentManager :: mod_Std() {if (this -> m_fileIsEmpty) {cout << "文件不存在或者文件為空" <<endl;}else {cout << "請輸入需要修改的學(xué)生學(xué)號:" << endl;string id;cin >> id;int index = this -> IsExist(id);if (index != -1) {//delete this -> m_StdArray -> at(index);string newid;string newname;string newdep;Student *std = NULL;cout<< "搜索到學(xué)號為" << id << "的學(xué)生,請輸入新學(xué)號:" << endl;cin >> newid; cout << "請輸入新姓名:" <<endl;cin >> newname;cout << "請輸入新職責(zé):" <<endl;cin >> newdep;if (newdep == "班長") {std = new ClassPresident(newid, newname, newdep);}else if (newdep == "班干部") {std = new Classleader(newid, newname, newdep);}else if (newdep == "普通學(xué)生") {std = new Nomalstuden(newid, newname, newdep);}this -> m_StdArray -> at(index) = std;cout <<"修改成功!" << endl;this -> save();}else {cout << "修改失敗,不存在該學(xué)號的學(xué)生!" << endl;}}system("pause"); } void StudentManager :: find_Std() {if (this -> m_fileIsEmpty) {cout << "文件不存在或者文件為空" <<endl;}else {cout << "請輸入需要查找的學(xué)生學(xué)號:" << endl;string id;cin >> id;int index = this -> IsExist(id);if (index != -1) {cout<< "查找成功!該學(xué)生信息如下:" << endl;this -> m_StdArray -> at(index) ->showInfo();}else {cout << "查找失敗!該學(xué)生不存在!" <<endl;}} } void StudentManager :: clean_File() {cout << "確定清空所有數(shù)據(jù)?" << endl;cout << "1,確定" <<endl;cout << "2,返回" <<endl;int selet = 0;cin >> selet;if (selet == 1) {ofstream ofs(FILENAME, ios :: trunc);ofs.close();if (this -> m_StdArray) {this -> m_StdArray -> clear();vector<Student*>(*this -> m_StdArray).swap(*this -> m_StdArray);this -> m_fileIsEmpty = true;this -> m_StdArray = NULL;}cout << "清空成功!" << endl;}system("pause");system("cls"); } StudentManager :: ~StudentManager() {if (this -> m_StdArray) {this -> m_StdArray -> clear();delete[] this -> m_StdArray;this -> m_StdArray = NULL;} }//普通學(xué)生函數(shù)實現(xiàn) Nomalstuden :: Nomalstuden(string Id, string name, string dep) {this -> StId = Id;this -> m_name = name;this -> m_Dept = dep; } void Nomalstuden :: showInfo() {cout << "學(xué)生學(xué)號:" << this -> StId<< "\t學(xué)生姓名:" << this -> m_name<< "\t學(xué)生職位:" << this -> m_Dept<< "\t學(xué)生任務(wù):遵守班級紀律" << endl; }//班長函數(shù)實現(xiàn) ClassPresident :: ClassPresident(string Id, string name, string dep) {this -> StId = Id;this -> m_name = name;this -> m_Dept = dep; } void ClassPresident :: showInfo () {cout << "學(xué)生學(xué)號:" << this -> StId<< "\t學(xué)生姓名:" << this -> m_name<< "\t學(xué)生職位:" << this -> m_Dept<< "\t學(xué)生任務(wù):管理班干部,與輔導(dǎo)員對接,帶領(lǐng)班級" << endl; }//班干部函數(shù)實現(xiàn) Classleader :: Classleader(string Id, string name, string dep) {this -> StId = Id;this -> m_name = name;this -> m_Dept = dep; } void Classleader :: showInfo () {cout << "學(xué)生學(xué)號:" << this -> StId<< "\t學(xué)生姓名:" << this -> m_name<< "\t學(xué)生職位:" << this -> m_Dept<< "\t學(xué)生任務(wù):履行自己的職責(zé),和各科老師對接,管理班級學(xué)生" << endl; }//主函數(shù) int main() {StudentManager stm; //實例化管理員int choice; //存儲用戶選項while(true) {stm.Show_Menu(); //調(diào)用打印界面成員函數(shù)cout << "請輸入您的選擇:" << endl;cin >> choice;switch (choice){case 0: //退出系統(tǒng)stm.Exit_System();break;case 1: //增加學(xué)生stm.Addinfo();break;case 2: //顯示學(xué)生stm.show_Std();break;case 3: //刪除學(xué)生stm.del_Std();break;case 4: //修改學(xué)生stm.mod_Std();break;case 5: //查找學(xué)生stm.find_Std();break;case 6: //清空文檔stm.clean_File();break; default:system("cls"); //清屏操作break;}}system("pause");return 0; }

總結(jié)

以上是生活随笔為你收集整理的学生管理系统(C++)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。