c/c++语言开发共享C++实现简易选课系统代码分享

下面是详细代码分享:#include<bits/stdc++.h>using namespace std;声明函数部分://声明函数部分void buildmainmenu();//声明主

下面是详细代码分享:

#include<bits/stdc++.h>  using namespace std;

声明函数部分:

//声明函数部分  void buildmainmenu();//声明主菜单函数  void selectsytem(int aid,int who);//声明选课系统  void mycourse(int aid,int who);//声明我的课表  void printc(int who);//声明打印课表函数  void wrongw();//错误提示函数

前置数据处理:

  • 现有课程数据course.txt
  • 现有学生数据students.txt
  • 现有老师数据 teacher.txt
  • 按顺序合并为date.txt.

初始状态为:

  • 1.无任何课程->导入现有课程数据 (具体内容见course类)
  • 2.无任何老师数据->导入老师数据 (具体内容见teacher类)
  • 3.此时老师并没有设置任何课程
  •     ->需要老师登陆系统进行设置
  •     该步骤需要手动键入数据,参考数据见setcourse.txt
  • 4.无任何学生数据->导入学生数据 (具体内容见student类)
  • 5.暂且令助教来自于学生,则助教的数据和学生数据一致
  • 6.对于学生和助教,需通过学号和所设密码进入选课系统
//用于打印边框  void printegde(){puts("**********************************************");}    //课程类  class course  {  public:      int courseid;//编号      string cousrename;//名称      int credit;//学分      bool haveset=0;//判断该门课是否被老师设置,没有设置(0)则不能被学生选择        course(){}//默认构造函数,要记得写      //自定义的含参构造函数      void setcourse(int a,string b,int c){courseid=a;cousrename=b;credit=c;}  };  //alllist 存储所有课程的信息  course alllist[200];  int c_totalnum;//代表课程的总数    class person  {  public:      string name;//姓名      int age;//年龄      //构造      person(){}      void setperson(string a,int b){name=a;age=b;}  };    class teacher:public person  {  public:      int teacherid;//老师的编号      int len;//需要教授的课程数量      int teachcourse[10];//所授课程id编号      int now_have=0;//已设置课程数量      teacher(){}//默认构造函数        void set_teacher(string name,int age,int id,int n)//构造      {          name=name;          age=age;          teacherid=id;          len=n;      }        void teach(){cout<<"i am a teacher,i teach my studentn";}        //删除已经设置的课程      void deletec(int cid)      {          system("cls");//用于清空cmd窗口            //如果当前没有课程可以删除就直接返回          if(now_have==0)           {              puts("you have set no course!");              _sleep(500);//用于暂停0.5s              return;          }            //can用于判断是否能取消设置某一门课(即要取消的课是否已被设置)          int can=0;          for(int i=1;i<=now_have;i++)          {              if(teachcourse[i]==cid)              {                  //可以取消,那么把原本设置好的最后一门课放到要取消的课的位置上                  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!");//不能取消设置的情况          _sleep(800);      }        void printnowteach()//输出已设置课程      {          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];//取出课程编号方便下一行书写              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;//返回上一级系统          else if(flag>0 && flag<=c_totalnum)//如果输入的数字在限定范围之内          {              deletec(flag);//取消设置              printnowteach();//并重置该页面          }          else//如果输入的数字不在预设范围内,进行报错提示          {              wrongw();              printnowteach();          }      }      void setcourse(int courseid)      //在已有课程中选择属于自己teach的课程编号      {          system("cls");          //如果已经选满了          if(len-now_have<=0)           {              puts("you have already set all of your courses!");              _sleep(800);return;          }          if(alllist[courseid].haveset!=0)//如果已经被别人设置了              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;//学号      int coursecount=0;//已选课程数目      int mycourse[20];//已选课程的课程编号      int leastnum;//至少选择课程数目      string key;//密码        student(){}      //此处age表示入学年份,如2021,2020      void set_student(string name,int age,long long num,int n,string key)      {          name=name;          age=age;          number=num;//学号          leastnum=n;          key=key;          memset(mycourse,0,sizeof(mycourse));//初始化已选课程数组      }        void selectcourse(int courseid)      {          system("cls");          //用于判断自己是否已经选过这门课程          bool havechoose = 0;          for(int i=1;i<=coursecount;i++)              if(courseid==mycourse[i])                   havechoose=1;            //如果该门课已经被老师设置并且自己没有选过          if(alllist[courseid].haveset && havechoose==0)          {              puts("you successfully stlect the course!");              mycourse[++coursecount]=courseid;          }          //老师没有设置课程          else if(alllist[courseid].haveset==0) puts("there is no such course!");          //自己已经选过          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);      }        //判断是否满足学校要求      void judge()      {          if(coursecount>=leastnum) //比较已选课程和要求课程数量              puts("you can complete the credits of this semester");          else               printf("you need to choose %d more coursesn",leastnum-coursecount);      }  };  student a_s[2000];//所有学生的信息  int s_totalnum;//学生的总数    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()  {      //导入所有课程数据      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;          //输入编号,名称,学分          alllist[i].setcourse(a,c,b);      }  }    void pre_teacher()  {      //导入所有老师数据      int a,b,c;string d;      scanf("%d",&t_totalnum);      for(int i=1;i<=t_totalnum;i++)      {          cin>>d>>a>>b>>c;          //输入姓名,年龄,编号,应设置课程数量          a_t[i].set_teacher(d,a,b,c);      }  }    void pre_student()  {      //导入所有学生数据      int a;long long b;string d,e;      scanf("%d",&s_totalnum);      for(int i=1;i<=s_totalnum;i++)      {          //姓名 入学年份 学号 至少选课数统一为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()  //选课系统前置准备工作  {      pre_course();//导入课程数据      pre_teacher();//导入老师数据      pre_student();//导入学生数据  }    void wrongw()//报错提示  {      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)//学生      {          //没课的情况          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');      }      //是否进行退课操作      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//学生          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 代表学生 1 代表助教  {      system("cls");      //打印所有课程信息      printc(0);      //输出学生姓名 .c_str()的作用是将string类型的数据转成char类型,使得它可以用printf输出      printf("student: %s .",a_s[aid].name.c_str());      if(who==0)           a_s[aid].judge();//学生      printf("please enter the course id to selectnyou can input -1 to exitnyou can input -2 to check mycourse and delete coursennow enter a number:");      int flag=0;scanf("%d",&flag);      if(flag==-1) buildmainmenu();//返回上一级菜单      else if(flag==-2) mycourse(aid,who);//查看已选课表      else if(flag>0 && flag<=c_totalnum)//数据在预设范围内,则选课      {          if(who==0)//学生              a_s[aid].selectcourse(flag),selectsytem(aid,who);          else a_ta[aid].selectcourse(flag),selectsytem(aid,who);//助教      }      else {wrongw();selectsytem(aid,who);}//报错  }    void studentsystem(int who)  {      system("cls");      //切换到从控制台输入数据      freopen("con","r",stdin);       //接下来为登陆页面        puts("please enter your student number and  passwordn");      printegde();putchar('n');      printf("student number:");long long sn;//输入学号      scanf("%lld",&sn);      printf("password:      ");      char pas[20],acs[20];scanf("%s",pas);//输入密码      int aid;//在数据库中的序号      //在数据库中找到学号对应的正确密码      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;//输入密码的机会      while(strcmp(acs,pas)!=0 && times>0)      //看输入的密码与正确密码是否匹配,以及次数是否耗尽      {          puts("wrong password!!!");          printf("you have %d times to enter the correct passwordn",times);          times--;scanf("%s",pas);      }      //次数耗尽推出系统      if(times==0)      {          puts("i am sorry you can't enter our system!");          _sleep(800);exit(0);      }      if(who==0) selectsytem(aid,who);//学生      else selectsytem(aid,1);//助教  }    //老师设置课程  void setcourse(int tid)  {      system("cls");      printf("welcome : %sn",a_t[tid].name.c_str());      printf("you need to set %d coursesnn",a_t[tid].len-a_t[tid].now_have);      printc(1);      printf("please enter the course id to setnyou can input -1 to exitnyou can input -2 to check mycoursennow enter a number:");      int flag=0;scanf("%d",&flag);      if(flag==-1) buildmainmenu();      else if(flag==-2) a_t[tid].printnowteach(),setcourse(tid);//查看已设置的课程      else if(flag>0 && flag<=c_totalnum)//设置课程          a_t[tid].setcourse(flag),setcourse(tid);      else {wrongw();setcourse(tid);}  }    void teachersystem()  {      system("cls");      freopen("con","r",stdin); //切换到从控制台输入数据      puts("         welcome  to  teacher  system!        ");      printegde();putchar('n');      //输入教师编号以进入系统      printf("please enter your teacher id:");      int tid;scanf("%d",&tid);      if(tid>0 && tid<=t_totalnum)          setcourse(tid);      else{          wrongw();teachersystem();      }  }    void tasystem()  {      //实际上助教系统可以看错和学生是一个系统的      studentsystem(1);  }    //构建主菜单  void buildmainmenu()  {      system("cls");      freopen("con","r",stdin); //切换到从控制台输入数据      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);//进入子系统      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()//主函数  {      pre();//前置数据导入      buildmainmenu();//构建主菜单      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  */

到此这篇关于c++实现简易选课系统代码分享的文章就介绍到这了,更多相关c++实现选课系统内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享C++实现简易选课系统代码分享,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1038421.html

(0)
上一篇 2022年1月26日
下一篇 2022年1月26日

精彩推荐