您好,登錄后才能下訂單哦!
這篇文章主要介紹了C++如何實(shí)現(xiàn)簡單的學(xué)生成績管理系統(tǒng)的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇C++如何實(shí)現(xiàn)簡單的學(xué)生成績管理系統(tǒng)文章都會有所收獲,下面我們一起來看看吧。
manage.h
#include <iostream> #include <fstream> #include <string> using namespace std; //學(xué)生 template <class T> class Student { template <class T> friend class StudentManage; public: Student() { next = NULL; } private: int Sid;//學(xué)生編號 string Sname;//學(xué)生姓名 string Ssex;//學(xué)生性別 int Sage;//學(xué)生年齡 int Sscore;//學(xué)生成績 Student<T>* next;//指向下一結(jié)點(diǎn)的指針 }; template <class T> class StudentManage { public: StudentManage(); ~StudentManage(); StudentManage<T>& SInsert(int k, const T& sid, const string& sname, const string& ssex, const T& sage, const T& sscore);//插入 bool SIsEmpty() const;//判空 int SGetLength() const;//獲取長度 bool SGetData(int k, T& sid, string& sname, string& ssex, T& sage, T& sscore);//獲取數(shù)據(jù) bool SModifyData(int k, const T& sid, const string& sname, const string& ssex, const T& sage, const T& sscore);//修改 int SFind(const int& sid);//根據(jù)id查找 StudentManage<T>& SDelete(int k, T& sid, string& sname, string& ssex, T& sage, T& sscore);//刪除 void Sort(T a[]); void SOutPut(ostream& sout) const;//打印 private: Student<T>* shead; }; //班級 template <class T> class Class { template <class T> friend class ClassManage; public: Class() { next = NULL; } private: int Cid;//班級編號 string CName;//班級名稱 string CBzrName;//班主任名稱 string CXsName;//學(xué)生名稱 Class<T>* next; }; template <class T> class ClassManage { public: ClassManage(); ~ClassManage(); ClassManage<T>& CInsert(int k, const T& cid, const string& cname, const string& cbzrname, const string& cxsname);//插入 bool CIsEmpty() const;//判空 int CGetLength() const;//獲取長度 bool CGetData(int k, T& cid, string& cname, string& cbzrname, string& cxsname);//獲取數(shù)據(jù) bool CModifyData(int k, const T& cid, const string& cname, const string& cbzrname, const string& cxsname);//修改 int CFind(const int& cid);//根據(jù)id查找 ClassManage<T>& CDelete(int k, T& cid, string& cname, string& cbzrname, string& cxsname);//刪除 void COutPut(ostream& cout) const;//打印 private: Class<T>* chead; }; //成績 template <class T> class Grade { template <class T> friend class GradeManage; public: Grade() { next = NULL; } private: string Gname; int Gscore; Grade<T>* next; }; template <class T> class GradeManage { public: GradeManage(); ~GradeManage(); GradeManage<T>& GInsert(int k, const string& gname, const T& gscore);//插入 bool GIsEmpty() const;//判空 int GGetLength() const;//獲取長度 bool GGetData(int k, string& gname, T& gscore);//獲取數(shù)據(jù) bool GModifyData(int k, const string& gname, const T& gscore);//修改 int GFind(const string& gname);//根據(jù)課程名稱查找 GradeManage<T>& GDelete(int k, string& gname, T& gscore);//刪除 void GOutPut(ostream& gout) const;//打印 private: Grade<T>* ghead; };
manage.cpp
#include "manage.h" //學(xué)生 template<class T> inline StudentManage<T>::StudentManage() { shead = new Student<T>(); } template<class T> StudentManage<T>::~StudentManage() { delete shead; } template<class T> StudentManage<T>& StudentManage<T>::SInsert(int k, const T& sid, const string& sname, const string& ssex, const T& sage, const T& sscore) { Student<T>* p = shead; Student<T>* newNode = new Student<T>; newNode->Sid = sid; newNode->Sname = sname; newNode->Ssex = ssex; newNode->Sage = sage; newNode->Sscore = sscore; int len = SGetLength(); if (k<1 || k>len + 1) cout << "失敗"; else { for (int i = 1; i < k; i++) p = p->next; newNode->next = p->next; p->next = newNode; } return *this; } template<class T> bool StudentManage<T>::SIsEmpty() const { return shead->next == NULL; } template<class T> int StudentManage<T>::SGetLength() const { int length = 0; Student<T>* p = shead->next; while (p) { length++; p = p->next; } return length; } template<class T> bool StudentManage<T>::SGetData(int k, T& sid, string& sname, string& ssex, T& sage, T& sscore) { Student<T>* p = shead->next; int index = 1; if (k<1 || k>SGetLength()) return false; while (p != NULL && index < k) { index++; p = p->next; } if (p == NULL) return false; else { sid = p->Sid; sname = p->Sname; ssex = p->Ssex; sage = p->Sage; sscore = p->Sscore; return true; } } template<class T> bool StudentManage<T>::SModifyData(int k, const T& sid, const string& sname, const string& ssex, const T& sage, const T& sscore) { Student<T>* p = shead->next; int index = 1; if (k<1 || k>SGetLength()) return false; while (p != NULL && index < k) { index++; p = p->next; } if (p == NULL) return false; else { p->Sid = sid; p->Sname = sname; p->Ssex = ssex; p->Sage = sage; p->Sscore = sscore; return true; } } template<class T> int StudentManage<T>::SFind(const int& sid) { Student<T>* p = shead->next; int index = 1; while (p != NULL && p->Sid != sid) { p = p->next; index++; } if (p != NULL) return index; else return 0; } template<class T> StudentManage<T>& StudentManage<T>::SDelete(int k, T& sid, string& sname, string& ssex, T& sage, T& sscore) { if (SGetData(k, sid, sname, ssex, sage, sscore)) { Student<T>* p = shead; Student<T>* q = NULL; for (int i = 1; i < k; i++) p = p->next; q = p->next; p->next = q->next; delete q; } else cout << "失敗"; return *this; } template<class T> void StudentManage<T>::Sort(T a[]) { int n = SGetLength(); T i, j, temp; for (i = 1; i < n; i++) { if (a[i] < a[i - 1]) { temp = a[i]; for (j = i - 1; j >= 0 && a[j] > temp; --j) a[j + 1] = a[j]; a[j + 1] = temp; } } } template<class T> void StudentManage<T>::SOutPut(ostream& sout) const { Student<T>* p = shead->next; while (p != NULL) { sout << "學(xué)生編號:" << p->Sid << "學(xué)生姓名:" << p->Sname << "學(xué)生性別:" << p->Ssex << "學(xué)生年齡:" << p->Sage << "學(xué)生總分:" << p->Sscore <<" " << endl; p = p->next; } } template <class T> ostream& operator<<(ostream& sout, StudentManage<T>& x) { x.SOutPut(sout); return sout; } //班級 template<class T> inline ClassManage<T>::ClassManage() { chead = new Class<T>(); } template<class T> ClassManage<T>::~ClassManage() { delete chead; } template<class T> ClassManage<T>& ClassManage<T>::CInsert(int k, const T& cid, const string& cname, const string& cbzrname, const string& cxsname) { Class<T>* p = chead; Class<T>* newNode = new Class<T>; newNode->Cid = cid; newNode->CName = cname; newNode->CBzrName = cbzrname; newNode->CXsName = cxsname; int len = CGetLength(); if (k<1 || k>len + 1) cout << "失敗"; else { for (int i = 1; i < k; i++) p = p->next; newNode->next = p->next; p->next = newNode; } return *this; } template<class T> bool ClassManage<T>::CIsEmpty() const { return chead->next == NULL; } template<class T> int ClassManage<T>::CGetLength() const { int length = 0; Class<T>* p = chead->next; while (p) { length++; p = p->next; } return length; } template<class T> bool ClassManage<T>::CGetData(int k, T& cid, string& cname, string& cbzrname, string& cxsname) { Class<T>* p = chead->next; int index = 1; if (k<1 || k>CGetLength()) return false; while (p != NULL && index < k) { index++; p = p->next; } if (p == NULL) return false; else { cid = p->Cid; cname = p->CName; cbzrname = p->CBzrName; cxsname = p->CXsName; return true; } } template<class T> bool ClassManage<T>::CModifyData(int k, const T& cid, const string& cname, const string& cbzrname, const string& cxsname) { Class<T>* p = chead->next; int index = 1; if (k<1 || k>CGetLength()) return false; while (p != NULL && index < k) { index++; p = p->next; } if (p == NULL) return false; else { p->Cid = cid; p->CName = cname; p->CBzrName = cbzrname; p->CXsName = cxsname; return true; } } template<class T> int ClassManage<T>::CFind(const int& cid) { Class<T>* p = chead->next; int index = 1; while (p != NULL && p->Cid != cid) { p = p->next; index++; } if (p != NULL) return index; else return 0; } template<class T> ClassManage<T>& ClassManage<T>::CDelete(int k, T& cid, string& cname, string& cbzrname, string& cxsname) { if (CGetData(k, cid, cname, cbzrname, cxsname)) { Class<T>* p = chead; Class<T>* q = NULL; for (int i = 1; i < k; i++) p = p->next; q = p->next; p->next = q->next; delete q; } else cout << "失敗"; return *this; } template<class T> void ClassManage<T>::COutPut(ostream& cout) const { Class<T>* p = chead->next; while (p != NULL) { cout << "班級編號:" << p->Cid << "班級:" << p->CName << "班主任姓名:" << p->CBzrName << "學(xué)生姓名:" << p->CXsName <<" " << endl; p = p->next; } } template <class T> ostream& operator<<(ostream& cout, ClassManage<T>& x) { x.COutPut(cout); return cout; } //成績 template<class T> inline GradeManage<T>::GradeManage() { ghead = new Grade<T>(); } template<class T> GradeManage<T>::~GradeManage() { delete ghead; } template<class T> GradeManage<T>& GradeManage<T>::GInsert(int k, const string& gname, const T& gscore) { Grade<T>* p = ghead; Grade<T>* newNode = new Grade<T>; newNode->Gname = gname; newNode->Gscore = gscore; int len = GGetLength(); if (k<1 || k>len + 1) cout << "失敗"; else { for (int i = 1; i < k; i++) p = p->next; newNode->next = p->next; p->next = newNode; } return *this; } template<class T> bool GradeManage<T>::GIsEmpty() const { return ghead->next == NULL; } template<class T> int GradeManage<T>::GGetLength() const { int length = 0; Grade<T>* p = ghead->next; while (p) { length++; p = p->next; } return length; } template<class T> bool GradeManage<T>::GGetData(int k, string& gname, T& gscore) { Grade<T>* p = ghead->next; int index = 1; if (k<1 || k>GGetLength()) return false; while (p != NULL && index < k) { index++; p = p->next; } if (p == NULL) return false; else { gname = p->Gname; gscore = p->Gscore; return true; } } template<class T> bool GradeManage<T>::GModifyData(int k, const string& gname, const T& gscore) { Grade<T>* p = ghead->next; int index = 1; if (k<1 || k>GGetLength()) return false; while (p != NULL && index < k) { index++; p = p->next; } if (p == NULL) return false; else { p->Gname = gname; p->Gscore = gscore; return true; } } template<class T> int GradeManage<T>::GFind(const string& gname) { Grade<T>* p = ghead->next; int index = 1; while (p != NULL && p->Gname != gname) { p = p->next; index++; } if (p != NULL) return index; else return 0; } template<class T> GradeManage<T>& GradeManage<T>::GDelete(int k, string& gname, T& gscore) { if (GGetData(k, gname, gscore)) { Grade<T>* p = ghead; Grade<T>* q = NULL; for (int i = 1; i < k; i++) p = p->next; q = p->next; p->next = q->next; delete q; } else cout << "失敗"; return *this; } template<class T> void GradeManage<T>::GOutPut(ostream& gout) const { Grade<T>* p = ghead->next; while (p != NULL) { gout << "課程名稱:" << p->Gname << "課程成績:" << p->Gscore<<" " << endl; p = p->next; } } template <class T> ostream& operator<<(ostream& gout, GradeManage<T>& x) { x.GOutPut(gout); return gout; } /* int main() { //學(xué)生 StudentManage<int> s; s.SInsert(1, 1, "小明", "男",23 , 290); s.SInsert(2, 2, "張三", "男", 22, 280); s.SInsert(3, 3, "小紅", "女", 21, 270); s.SInsert(4, 4, "李四", "男", 24, 260); cout << s; int n = s.SGetLength(); cout << "共有" << n << "條數(shù)據(jù)" << endl; s.SModifyData(3, 3, "小麗", "女", 21, 270); cout << s; int sid, sage, sscore; string sname, ssex; int sid1, sage1, sscore1; string sname1, ssex1; s.SGetData(1, sid, sname,ssex, sage, sscore); cout << "學(xué)生編號:" << sid << " 學(xué)生姓名:" << sname << " 學(xué)生性別:" << ssex << " 學(xué)生年齡:" << sage << " 學(xué)生總分:" << sscore << endl; s.SDelete(2, sid1, sname1, ssex1, sage1, sscore1); cout << "刪除學(xué)生編號:" << sid1 << " 學(xué)生姓名:" << sname1 << " 學(xué)生性別:" << ssex1 << " 學(xué)生年齡:" << sage1 << " 學(xué)生總分:" << sscore1 << endl; cout << s; //班級 ClassManage<int> c; c.CInsert(1,1,"一班","李主任","王麻子"); c.CInsert(2, 2,"一班", "李主任", "十大"); c.CInsert(3, 3,"一班", "李主任", "的撒"); cout << c; int x = c.CGetLength(); cout << "共有" << x << "條數(shù)據(jù)" << endl; c.CModifyData(3, 3, "一班", "張主任", "的撒"); cout << c; int cid; string cname, cbzrname,cxsname; c.CGetData(1, cid, cname, cbzrname, cxsname); cout << "班級編號:" << cid << " 班級:" <<cname << " 班主任姓名:" << cbzrname << " 學(xué)生姓名:" << cxsname << endl; //成績 GradeManage<int> g; g.GInsert(1, "c++", 90); g.GInsert(1, "java", 90); cout << g; int t = g.GGetLength(); cout << "共有" << t << "條數(shù)據(jù)" << endl; g.GModifyData(2, "python", 92); cout << g; string gname;int gscore; g.GGetData(1, gname, gscore); cout << "課程名稱:" << gname << " 課程成績:" << gscore << endl; return 0; } */ int main() { ofstream outfile;//終端輸入--內(nèi)存--文本 //學(xué)生 StudentManage<int> stud; stud.SInsert(1, 1, "小明", "男", 23, 290); stud.SInsert(2, 2, "張三", "男", 22, 280); stud.SInsert(3, 3, "小紅", "女", 21, 270); stud.SInsert(4, 4, "李四", "男", 24, 260); int sid; string sname; string ssex; int sage; int sscore; int sid1; string sname1; string ssex1; int sage1; int sscore1; int sid2; string sname2; string ssex2; int sage2; int sscore2; //班級 ClassManage<int> cla; cla.CInsert(1, 1, "一班", "李主任", "王麻子"); cla.CInsert(2, 2, "一班", "李主任", "王五"); cla.CInsert(3, 3, "二班", "李主任", "達(dá)摩"); int cid; string cname, cbzrname, cxsname; int cid1; string cname1, cbzrname1, cxsname1; int cid2; string cname2, cbzrname2, cxsname2; //成績 GradeManage<int> gra; gra.GInsert(1, "c++", 90); gra.GInsert(1, "java", 90); string gname; int gscore; string gname1; int gscore1; string gname2; int gscore2; //主界面 cout << "歡迎來到學(xué)生成績管理系統(tǒng)!" << endl; cout << "選擇1,進(jìn)入學(xué)生信息管理"<<endl; cout << "選擇2,進(jìn)入班級信息管理" << endl; cout << "選擇3,進(jìn)入成績信息管理" << endl; cout <<"輸入999退出" << endl; int n; cout << "請輸入選項(xiàng):"; cin >> n; while (n != 999) { switch (n) { case 1:cout << "歡迎來到學(xué)生信息管理,輸入999退出" << endl; cout << "1、查看學(xué)生信息\n2、增加學(xué)生信息\n3、修改學(xué)生信息\n4、刪除學(xué)生信息\n5、保存學(xué)生信息\n6、讀取學(xué)生信息\n7、學(xué)生按總成績排序*" << endl; int s; cout << "請輸入選項(xiàng):"; cin >> s; while(s!=999){ switch (s) { case 1:cout << "查看學(xué)生信息" << endl; cout << stud << endl; break; case 2:cout << "增加學(xué)生信息" << endl; int k; cout << "請輸入學(xué)生位置" << endl; cin >> k; cout << "請輸入編號:"; cin >> sid; cout << "請輸入姓名:"; cin >> sname; cout << "請輸入性別:"; cin >> ssex; cout << "請輸入年齡:"; cin >> sage; cout << "請輸入總分:"; cin >> sscore; stud.SInsert(k, sid, sname, ssex, sage, sscore); break; case 3:cout << "修改學(xué)生信息" << endl; int k1; cout << "請輸入要修改的學(xué)生位置" << endl; cin >> k1; cout << "請輸入要修改的編號:"; cin >> sid1; cout << "請輸入要修改的姓名:"; cin >> sname1; cout << "請輸入要修改的性別:"; cin >> ssex1; cout << "請輸入要修改的年齡:"; cin >> sage1; cout << "請輸入要修改的總分:"; cin >> sscore1; stud.SModifyData(k1, sid1, sname1, ssex1, sage1, sscore1); break; case 4:cout << "刪除學(xué)生信息" << endl; int k2; cout << "請輸入要刪除的學(xué)生位置" << endl; cin >> k2; cout << "請輸入要刪除的編號:"; cin >> sid2; cout << "請輸入要刪除的姓名:"; cin >> sname2; cout << "請輸入要刪除的性別:"; cin >> ssex2; cout << "請輸入要刪除的年齡:"; cin >> sage2; cout << "請輸入要刪除的總分:"; cin >> sscore2; stud.SDelete(k2, sid2, sname2, ssex2, sage2, sscore2); break; case 5:cout << "保存學(xué)生信息" << endl; outfile.open("F:\student.txt"); outfile << stud << endl; outfile.close(); break; case 6:cout << "讀取學(xué)生信息" << endl; { ifstream in("F:\student.txt", ios::in); if (!in.fail()) { while (!in.eof()) { string str5; in >> str5; cout << str5 << endl; } } } break; case 7:cout << "學(xué)生按總成績排序" << endl; break; default: break; } cout << "請輸入選項(xiàng):"; cin >> s; } break; case 2:cout << "歡迎來到班級信息管理,輸入999退出" << endl; cout << "1、查看班級信息\n2、增加班級信息\n3、修改班級信息\n4、刪除班級信息\n5、保存班級信息\n6、讀取班級信息" << endl; int c; cout << "請輸入選項(xiàng):"; cin >> c; while (c != 999) { switch (c) { case 1:cout << "查看班級信息" << endl; cout << cla << endl; break; case 2:cout << "增加班級信息" << endl; int k; cout << "請輸入班級位置" << endl; cin >> k; cout << "請輸入班級編號:"; cin >> cid; cout << "請輸入班級名稱:"; cin >> cname; cout << "請輸入班主任名稱:"; cin >> cbzrname; cout << "請輸入學(xué)生名字:"; cin >> cxsname; cla.CInsert(k, cid, cname, cbzrname, cxsname); break; case 3:cout << "修改班級信息" << endl; int k1; cout << "請輸入班級位置" << endl; cin >> k1; cout << "請輸入班級編號:"; cin >> cid1; cout << "請輸入班級名稱:"; cin >> cname1; cout << "請輸入班主任名稱:"; cin >> cbzrname1; cout << "請輸入學(xué)生名字:"; cin >> cxsname1; cla.CModifyData(k1, cid1, cname1, cbzrname1, cxsname1); break; case 4:cout << "刪除班級信息" << endl; int k2; cout << "請輸入班級位置" << endl; cin >> k2; cout << "請輸入班級編號:"; cin >> cid2; cout << "請輸入班級名稱:"; cin >> cname2; cout << "請輸入班主任名稱:"; cin >> cbzrname2; cout << "請輸入學(xué)生名字:"; cin >> cxsname2; cla.CDelete(k2, cid2, cname2, cbzrname2, cxsname2); break; case 5:cout << "保存班級信息" << endl; outfile.open("F:\class.txt"); outfile << cla << endl; outfile.close(); break; case 6:cout << "讀取班級信息" << endl; { ifstream in("F:\class.txt", ios::in); if (!in.fail()) { while (!in.eof()) { string str5; in >> str5; cout << str5 << endl; } } } break; default: break; } cout << "請輸入選項(xiàng):"; cin >> c; } break; case 3:cout << "歡迎來到成績信息管理,輸入999退出" << endl; cout << "1、查看成績信息\n2、增加成績信息\n3、修改成績信息\n4、刪除成績信息\n5、保存成績信息\n6、讀取成績信息" << endl; int g; cout << "請輸入選項(xiàng):"; cin >> g; while (g != 999) { switch (g) { case 1:cout << "查看成績信息" << endl; cout << gra << endl; break; case 2:cout << "增加成績信息" << endl; int k; cout << "請輸入成績位置" << endl; cin >> k; cout << "請輸入課程名稱:"; cin >> gname; cout << "請輸入課程成績:"; cin >> gscore; gra.GInsert(k, gname, gscore); break; case 3:cout << "修改成績信息" << endl; int k1; cout << "請輸入成績位置" << endl; cin >> k1; cout << "請輸入課程名稱:"; cin >> gname1; cout << "請輸入課程成績:"; cin >> gscore1; gra.GModifyData(k1, gname1, gscore1); break; case 4:cout << "刪除成績信息" << endl; int k2; cout << "請輸入成績位置" << endl; cin >> k2; cout << "請輸入課程名稱:"; cin >> gname2; cout << "請輸入課程成績:"; cin >> gscore2; gra.GDelete(k2, gname2, gscore2); break; case 5:cout << "保存成績信息" << endl; outfile.open("F:\grade.txt"); outfile << gra << endl; outfile.close(); break; case 6:cout << "讀取成績信息" << endl; { ifstream in("F:\grade.txt", ios::in); if (!in.fail()) { while (!in.eof()) { string str5; in >> str5; cout << str5 << endl; } } } break; default: break; } cout << "請輸入選項(xiàng):"; cin >> g; } break; default: break; } cout << "請輸入選項(xiàng):"; cin >> n; } /* ofstream outfile;//終端輸入--內(nèi)存--文本 outfile.open("F:\student.txt", ios::binary | ios::app | ios::in | ios::out);//注意,里面有iOS::app,表示打開文件后,在寫入的文件不會覆蓋原文件中的內(nèi)容,也就是原來文件中的數(shù)據(jù)會得到保存。 //outfile << "學(xué)生編號:" << sid << " 學(xué)生姓名: " << sname << " 學(xué)生性別:" << ssex << " 學(xué)生年齡:" << sage << " 學(xué)生總分:" << sscore << endl; outfile << stud << endl; outfile.close(); */ return 0; }
關(guān)于“C++如何實(shí)現(xiàn)簡單的學(xué)生成績管理系統(tǒng)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“C++如何實(shí)現(xiàn)簡單的學(xué)生成績管理系統(tǒng)”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。