大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍C++实现学生选课系统的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联主要从事成都网站设计、做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务自贡,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
具体内容如下
#include#include #include #include #include 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 < 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 (p), sizeof(*p)); while (t) { oo.write(reinterpret_cast (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 (q), sizeof(*q)); while (t) { oo.write(reinterpret_cast (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++实现学生选课系统的方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!