您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)C++實(shí)現(xiàn)簡易選課系統(tǒng)代碼怎么寫,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
下面是詳細(xì)代碼分享:
#include<bits/stdc++.h> using namespace std;
聲明函數(shù)部分:
//聲明函數(shù)部分 void BuildMainMenu();//聲明主菜單函數(shù) void SelectSytem(int AID,int who);//聲明選課系統(tǒng) void MyCourse(int AID,int who);//聲明我的課表 void PrintC(int who);//聲明打印課表函數(shù) void WrongW();//錯(cuò)誤提示函數(shù)
前置數(shù)據(jù)處理:
現(xiàn)有課程數(shù)據(jù)course.txt
現(xiàn)有學(xué)生數(shù)據(jù)students.txt
現(xiàn)有老師數(shù)據(jù) teacher.txt
按順序合并為date.txt.
初始狀態(tài)為:
1.無任何課程->導(dǎo)入現(xiàn)有課程數(shù)據(jù) (具體內(nèi)容見Course
類)
2.無任何老師數(shù)據(jù)->導(dǎo)入老師數(shù)據(jù) (具體內(nèi)容見teacher
類)
3.此時(shí)老師并沒有設(shè)置任何課程
->需要老師登陸系統(tǒng)進(jìn)行設(shè)置
該步驟需要手動(dòng)鍵入數(shù)據(jù),參考數(shù)據(jù)見Setcourse.txt
4.無任何學(xué)生數(shù)據(jù)->導(dǎo)入學(xué)生數(shù)據(jù) (具體內(nèi)容見student
類)
5.暫且令助教來自于學(xué)生,則助教的數(shù)據(jù)和學(xué)生數(shù)據(jù)一致
6.對(duì)于學(xué)生和助教,需通過學(xué)號(hào)和所設(shè)密碼進(jìn)入選課系統(tǒng)
//用于打印邊框 void PrintEgde(){puts("**********************************************");} //課程類 class Course { public: int CourseId;//編號(hào) string cousreName;//名稱 int credit;//學(xué)分 bool HaveSet=0;//判斷該門課是否被老師設(shè)置,沒有設(shè)置(0)則不能被學(xué)生選擇 Course(){}//默認(rèn)構(gòu)造函數(shù),要記得寫 //自定義的含參構(gòu)造函數(shù) void SetCourse(int a,string b,int c){CourseId=a;cousreName=b;credit=c;} }; //AllList 存儲(chǔ)所有課程的信息 Course AllList[200]; int C_totalnum;//代表課程的總數(shù) class Person { public: string name;//姓名 int age;//年齡 //構(gòu)造 Person(){} void SetPerson(string a,int b){name=a;age=b;} }; class teacher:public Person { public: int teacherId;//老師的編號(hào) int Len;//需要教授的課程數(shù)量 int TeachCourse[10];//所授課程ID編號(hào) int now_have=0;//已設(shè)置課程數(shù)量 teacher(){}//默認(rèn)構(gòu)造函數(shù) void Set_Teacher(string Name,int Age,int ID,int n)//構(gòu)造 { name=Name; age=Age; teacherId=ID; Len=n; } void teach(){cout<<"I am a teacher,I teach my student\n";} //刪除已經(jīng)設(shè)置的課程 void DeleteC(int CID) { system("cls");//用于清空cmd窗口 //如果當(dāng)前沒有課程可以刪除就直接返回 if(now_have==0) { puts("You have set no course!"); _sleep(500);//用于暫停0.5s return; } //CAN用于判斷是否能取消設(shè)置某一門課(即要取消的課是否已被設(shè)置) int CAN=0; for(int i=1;i<=now_have;i++) { if(TeachCourse[i]==CID) { //可以取消,那么把原本設(shè)置好的最后一門課放到要取消的課的位置上 TeachCourse[i]=TeachCourse[now_have]; now_have--; CAN=1; break; } } if(CAN==1) puts("You successfully deleted the course!"); else puts("There is no such course!");//不能取消設(shè)置的情況 _sleep(800); } void PrintNowteach()//輸出已設(shè)置課程 { system("cls"); puts(" <M y C o u r s e> \n"); PrintEgde(); //如果沒有課 if(now_have==0) puts("You have no course now!"); //如果有課 for(int i=1;i<=now_have;i++) { int x=TeachCourse[i];//取出課程編號(hào)方便下一行書寫 printf("*%5d %-29s %4d *\n",AllList[x].CourseId,AllList[x].cousreName.c_str(),AllList[x].credit); } PrintEgde();putchar('\n'); printf({"You can input The CourseID to delete:(or -1 to return)"}); int flag=0;scanf("%d",&flag); if(flag==-1) return;//返回上一級(jí)系統(tǒng) else if(flag>0 && flag<=C_totalnum)//如果輸入的數(shù)字在限定范圍之內(nèi) { DeleteC(flag);//取消設(shè)置 PrintNowteach();//并重置該頁面 } else//如果輸入的數(shù)字不在預(yù)設(shè)范圍內(nèi),進(jìn)行報(bào)錯(cuò)提示 { WrongW(); PrintNowteach(); } } void setCourse(int CourseID) //在已有課程中選擇屬于自己Teach的課程編號(hào) { system("cls"); //如果已經(jīng)選滿了 if(Len-now_have<=0) { puts("You have already set all of your courses!"); _sleep(800);return; } if(AllList[CourseID].HaveSet!=0)//如果已經(jīng)被別人設(shè)置了 puts("This course has been set!"); else { puts("You successfully set the course!"); TeachCourse[++now_have]=CourseID; AllList[CourseID].HaveSet=1; } _sleep(800); } }; teacher A_T[200];//所有老師的信息 int T_totalnum; class student:public Person { public: long long number;//學(xué)號(hào) int courseCount=0;//已選課程數(shù)目 int mycourse[20];//已選課程的課程編號(hào) int LeastNum;//至少選擇課程數(shù)目 string key;//密碼 student(){} //此處age表示入學(xué)年份,如2021,2020 void Set_student(string Name,int Age,long long Num,int n,string Key) { name=Name; age=Age; number=Num;//學(xué)號(hào) LeastNum=n; key=Key; memset(mycourse,0,sizeof(mycourse));//初始化已選課程數(shù)組 } void selectCourse(int CourseID) { system("cls"); //用于判斷自己是否已經(jīng)選過這門課程 bool HaveChoose = 0; for(int i=1;i<=courseCount;i++) if(CourseID==mycourse[i]) HaveChoose=1; //如果該門課已經(jīng)被老師設(shè)置并且自己沒有選過 if(AllList[CourseID].HaveSet && HaveChoose==0) { puts("You successfully stlect the course!"); mycourse[++courseCount]=CourseID; } //老師沒有設(shè)置課程 else if(AllList[CourseID].HaveSet==0) puts("There is no such course!"); //自己已經(jīng)選過 else if(HaveChoose==1) puts("This course you have chosen!"); _sleep(800); } void Delete(int CID) { system("cls"); if(courseCount==0) return; int CAN; for(int i=1;i<=courseCount;i++) { if(mycourse[i]==CID) { mycourse[i]=mycourse[courseCount]; courseCount--; CAN=1; break; } } if(CAN==1) puts("You successfully deleted the course!"); else puts("There is no such course!"); _sleep(800); } //判斷是否滿足學(xué)校要求 void judge() { if(courseCount>=LeastNum) //比較已選課程和要求課程數(shù)量 puts("You can complete the credits of this semester"); else printf("You need to choose %d more courses\n",LeastNum-courseCount); } }; student A_S[2000];//所有學(xué)生的信息 int S_totalnum;//學(xué)生的總數(shù) class TeachingAssistant:public teacher,public student { public: void teach(){puts("I am a teaching assistant,I help my teacher teach his students");} TeachingAssistant(){} void selectCourse(int CourseID) { system("cls"); if(AllList[CourseID].HaveSet) { puts("You successfully stlect the course!"); mycourse[++courseCount]=CourseID; } else puts("There is no such course!"); _sleep(800); } void Delete(int CID) { system("cls"); if(courseCount==0) return; int CAN; for(int i=1;i<=courseCount;i++) { if(mycourse[i]==CID) { mycourse[i]=mycourse[courseCount]; courseCount--; CAN=1; break; } } if(CAN==1) puts("You successfully deleted the course!"); else puts("There is no such course!"); _sleep(800); } }; TeachingAssistant A_TA[2500]; void Pre_course() { //導(dǎo)入所有課程數(shù)據(jù) int a,b;string c; freopen("date.txt","r",stdin); scanf("%d",&C_totalnum); for(int i=1;i<=C_totalnum;i++) { cin>>a>>c>>b; //輸入編號(hào),名稱,學(xué)分 AllList[i].SetCourse(a,c,b); } } void Pre_teacher() { //導(dǎo)入所有老師數(shù)據(jù) int a,b,c;string d; scanf("%d",&T_totalnum); for(int i=1;i<=T_totalnum;i++) { cin>>d>>a>>b>>c; //輸入姓名,年齡,編號(hào),應(yīng)設(shè)置課程數(shù)量 A_T[i].Set_Teacher(d,a,b,c); } } void Pre_student() { //導(dǎo)入所有學(xué)生數(shù)據(jù) int a;long long b;string d,e; scanf("%d",&S_totalnum); for(int i=1;i<=S_totalnum;i++) { //姓名 入學(xué)年份 學(xué)號(hào) 至少選課數(shù)統(tǒng)一為2 cin>>d>>a>>b>>e; A_S[i].Set_student(d,a,b,2,e); A_TA[i].Set_student(d,a,b,2,e); } } void Pre() //選課系統(tǒng)前置準(zhǔn)備工作 { Pre_course();//導(dǎo)入課程數(shù)據(jù) Pre_teacher();//導(dǎo)入老師數(shù)據(jù) Pre_student();//導(dǎo)入學(xué)生數(shù)據(jù) } void WrongW()//報(bào)錯(cuò)提示 { system("cls"); puts("You entered the wrong one!"); _sleep(500); } void MyCourse(int AID,int who) { system("cls"); puts(" <M y C o u r s e> \n"); PrintEgde(); if(who==0)//學(xué)生 { //沒課的情況 if(A_S[AID].courseCount==0) puts("You have no course now!"); //有課的情況 for(int i=1;i<=A_S[AID].courseCount;i++) { int x=A_S[AID].mycourse[i]; printf("*%5d %-29s %4d *\n",AllList[x].CourseId,AllList[x].cousreName.c_str(),AllList[x].credit); } PrintEgde();putchar('\n'); A_S[AID].judge(); } else { if(A_TA[AID].courseCount==0) puts("You have no course now!"); for(int i=1;i<=A_TA[AID].courseCount;i++) { int x=A_TA[AID].mycourse[i]; printf("*%5d %-29s %4d *\n",AllList[x].CourseId,AllList[x].cousreName.c_str(),AllList[x].credit); } PrintEgde();putchar('\n'); } //是否進(jìn)行退課操作 printf({"You can input The CourseID to delete:(or -1 to return)"}); int flag=0;scanf("%d",&flag); if(flag==-1) SelectSytem(AID,who); else if(flag>0 && flag<=C_totalnum) { if(who==0) A_S[AID].Delete(flag); else A_TA[AID].Delete(flag); MyCourse(AID,who); } else {WrongW();MyCourse(AID,who);} } void PrintC(int who) //打印所有課程信息 { puts(" <Course Information> \n"); PrintEgde(); puts("*Course Id Name Credit *"); PrintEgde(); if(who==1)//老師和助教 for(int i=1;i<=C_totalnum;i++) printf("*%5d %-29s %4d *\n",AllList[i].CourseId,AllList[i].cousreName.c_str(),AllList[i].credit); else//學(xué)生 for(int i=1;i<=C_totalnum;i++) if(AllList[i].HaveSet) printf("*%5d %-29s %4d *\n",AllList[i].CourseId,AllList[i].cousreName.c_str(),AllList[i].credit); PrintEgde();putchar('\n'); } void SelectSytem(int AID,int who) //who 0 代表學(xué)生 1 代表助教 { system("cls"); //打印所有課程信息 PrintC(0); //輸出學(xué)生姓名 .c_str()的作用是將string類型的數(shù)據(jù)轉(zhuǎn)成char類型,使得它可以用printf輸出 printf("Student: %s .",A_S[AID].name.c_str()); if(who==0) A_S[AID].judge();//學(xué)生 printf("Please enter the Course Id to select\nYou can input -1 to exit\nYou can input -2 to check Mycourse and delete course\nNow enter a number:"); int flag=0;scanf("%d",&flag); if(flag==-1) BuildMainMenu();//返回上一級(jí)菜單 else if(flag==-2) MyCourse(AID,who);//查看已選課表 else if(flag>0 && flag<=C_totalnum)//數(shù)據(jù)在預(yù)設(shè)范圍內(nèi),則選課 { if(who==0)//學(xué)生 A_S[AID].selectCourse(flag),SelectSytem(AID,who); else A_TA[AID].selectCourse(flag),SelectSytem(AID,who);//助教 } else {WrongW();SelectSytem(AID,who);}//報(bào)錯(cuò) } void StudentSystem(int who) { system("cls"); //切換到從控制臺(tái)輸入數(shù)據(jù) freopen("CON","r",stdin); //接下來為登陸頁面 puts("Please enter your student number and password\n"); PrintEgde();putchar('\n'); printf("Student number:");long long SN;//輸入學(xué)號(hào) scanf("%lld",&SN); printf("Password: "); char Pas[20],Acs[20];scanf("%s",Pas);//輸入密碼 int AID;//在數(shù)據(jù)庫中的序號(hào) //在數(shù)據(jù)庫中找到學(xué)號(hào)對(duì)應(yīng)的正確密碼 for(int i=1;i<=S_totalnum;i++) if(A_S[i].number==SN){strcpy(Acs,A_S[i].key.c_str());AID=i;break;} int times=2;//輸入密碼的機(jī)會(huì) while(strcmp(Acs,Pas)!=0 && times>0) //看輸入的密碼與正確密碼是否匹配,以及次數(shù)是否耗盡 { puts("Wrong Password!!!"); printf("you have %d times to enter the correct password\n",times); times--;scanf("%s",Pas); } //次數(shù)耗盡推出系統(tǒng) if(times==0) { puts("I am sorry you can't enter our system!"); _sleep(800);exit(0); } if(who==0) SelectSytem(AID,who);//學(xué)生 else SelectSytem(AID,1);//助教 } //老師設(shè)置課程 void Setcourse(int TID) { system("cls"); printf("Welcome : %s\n",A_T[TID].name.c_str()); printf("You need to set %d courses\n\n",A_T[TID].Len-A_T[TID].now_have); PrintC(1); printf("Please enter the Course Id to set\nYou can input -1 to exit\nYou can input -2 to check Mycourse\nNow enter a number:"); int flag=0;scanf("%d",&flag); if(flag==-1) BuildMainMenu(); else if(flag==-2) A_T[TID].PrintNowteach(),Setcourse(TID);//查看已設(shè)置的課程 else if(flag>0 && flag<=C_totalnum)//設(shè)置課程 A_T[TID].setCourse(flag),Setcourse(TID); else {WrongW();Setcourse(TID);} } void TeacherSystem() { system("cls"); freopen("CON","r",stdin); //切換到從控制臺(tái)輸入數(shù)據(jù) puts(" Welcome to Teacher system! "); PrintEgde();putchar('\n'); //輸入教師編號(hào)以進(jìn)入系統(tǒng) printf("Please enter your Teacher Id:"); int TID;scanf("%d",&TID); if(TID>0 && TID<=T_totalnum) Setcourse(TID); else{ WrongW();TeacherSystem(); } } void TASystem() { //實(shí)際上助教系統(tǒng)可以看錯(cuò)和學(xué)生是一個(gè)系統(tǒng)的 StudentSystem(1); } //構(gòu)建主菜單 void BuildMainMenu() { system("cls"); freopen("CON","r",stdin); //切換到從控制臺(tái)輸入數(shù)據(jù) puts(" Welcome to course selection system! "); PrintEgde(); puts("* 1.Student entrance *"); puts("* 2.Teacher entrance *"); puts("* 3.TeachingAssistant *"); puts("* -1.Exit this system *"); PrintEgde();putchar('\n'); printf("Please input 1,2,3 or -1 to enter this system:"); int flag=-1;scanf("%d",&flag);//進(jìn)入子系統(tǒng) if(flag==1) StudentSystem(0); else if(flag==2) TeacherSystem(); else if(flag==3) TASystem(); else if(flag==-1) exit(0); else { WrongW(); BuildMainMenu(); } } int main()//主函數(shù) { Pre();//前置數(shù)據(jù)導(dǎo)入 BuildMainMenu();//構(gòu)建主菜單 return 0; }
/* date.txt 10 1 Media_English 2 2 Literature_English 2 3 Drama_English 2 4 Academic_English 2 5 Cross-cultural_Communication 2 6 Public_Speaking 2 7 Intermediate_English_Speaking 2 8 Intermediate_English_Writing 2 9 Basic_Russian 2 10 Basic_French 2 5 Harry_potter 35 1 2 Hermione 34 2 3 Henry_rowen 36 3 1 Snape 55 4 2 Dumbledore 46 5 2 2 Jack 2021 2021001 123456! Tom 2021 2021002 abc123 */
看完上述內(nèi)容,你們對(duì)C++實(shí)現(xiàn)簡易選課系統(tǒng)代碼怎么寫有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。