iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++实现简易选课系统代码分享
  • 644
分享到

C++实现简易选课系统代码分享

2024-04-02 19:04:59 644人浏览 安东尼
摘要

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

下面是详细代码分享:

#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 student\n";}

    //删除已经设置的课程
    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 courses\n",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 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();//返回上一级菜单
    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  passWord\n");
    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 password\n",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 : %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);//查看已设置的课程
    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;
}

到此这篇关于C++实现简易选课系统代码分享的文章就介绍到这了,更多相关C++实现选课系统内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C++实现简易选课系统代码分享

本文链接: https://www.lsjlt.com/news/161456.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c语言怎么计算字符串长度
    在C语言中,可以使用内置的strlen()函数来计算字符串的长度。该函数位于string.h头文件中。以下是一个示例代码: #inc...
    99+
    2024-05-15
    c语言
  • c语言怎么判断三个数的大小
    可以通过比较三个数之间的大小关系来判断它们的大小。以下是一个示例代码: #include <stdio.h> int ...
    99+
    2024-05-15
    c语言
  • c++排序函数sort怎么使用
    在C++中,使用sort函数对容器中的元素进行排序,可以按照默认的升序排列,也可以自定义排序规则。 下面是一个简单的例子,对一个ve...
    99+
    2024-05-15
    c++
  • c++中string的compare函数怎么使用
    在C++中,可以使用string的compare函数来比较两个string对象的大小。该函数的原型如下: int compare(c...
    99+
    2024-05-15
    c++
  • c++中string的compare函数有什么用
    c++中string的compare函数用于比较两个字符串的大小关系。该函数返回一个整数值,表示两个字符串的比较结果。 具体来说,如...
    99+
    2024-05-15
    c++
  • c++中print函数的功能有哪些
    在C++中,通常使用std::cout来进行输出操作,而不是使用print函数。std::cout是C++标准库中的一个输出流对象,...
    99+
    2024-05-15
    c++
  • c语言数据结构怎么使用
    在C语言中,数据结构可以通过定义结构体来实现。以下是一些常见的数据结构的使用方法示例: 链表: #include <std...
    99+
    2024-05-15
    c语言
  • c++中print函数的用法是什么
    在C++中没有内置的print函数,但可以使用cout来输出内容到控制台。cout是C++标准库中的一个输出流对象,可以使用<...
    99+
    2024-05-15
    c++
  • c++中concept的用法是什么
    在C++20中,Concept是一种新的语言特性,用于定义类型要求和约束。Concept可以被用来约束函数模板、类模板和普通函数的参...
    99+
    2024-05-15
    c++
  • c++中concept的作用是什么
    在C++中,concept的作用是定义一种通用的约束,用于限制模板参数的类型范围。通过使用concept,可以在编译时对模板参数进行...
    99+
    2024-05-15
    c++
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作