C++学生选课系统

1.系统分析

本系统的用户分为普通用户和管理员两类,其中普通用户包括教师和学生。普通用户只能对自己的信息进行查询与修改,管理员则拥有所有功能权限。

1.1模块划分

    学生选课系统,在登录模块之后,根据系统用户的身份不同划分为三个模块,即:学生模块、教师模块和管理员模块。

1.2功能模块描述

   (1)登录模块

登录模块用于用户身份选择与用户登录,完成基本的验证。根据所填信息进行判断,提示“登录成功”、“用户名不存在”或者“密码错误”的信息。

   (2)学生模块

学生登录之后,可以查看课程、选课以及修改密码等功能。

(3)教师模块

    教师登录之后,可以查看自己所教课程的信息,以及选择该课程的学生信息,可以修改个人密码。

   (4)管理员模块

管理员登录之后,可以对该系统进行管理,原则上拥有所有用户的全部权限。主要功能有添加、修改及删除课程信息,设置选课、退课时间,查看课程信息和修改密码等。

2.系统设计

【系统流程图】

【类图】

3.测试数据

(1)课程信息测试数据,见文件“Subject.txt”;

(2)学生信息测试数据,见文件“Student.txt”;

(3)学生账号密码测试数据,见文件“studentIP.txt”;

(4)教师账号密码测试数据,见文件“TeacherIP.txt”;

(5)管理员账号密码测试数据,见文件“ManagerIP.txt”;

(6)学生选课情况测试数据,保存于文件“StSubject.txt”;

(7)选课时间测试数据,见文件“TimeSelect.txt”;

(8)退课时间测试数据,见文件“TimeQuit.txt”。

4.文件说明

5.系统效果图【部分】

【登录】

【学生】

【教师】

【管理员】

6.源代码

【运行前需要自行新建所需文件,必要时自己加上测试数据】

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <conio.h>
#include <ctime>
#include <iomanip>

using namespace std;

class Student;

//账号密码
class IdPw
{
public:
    IdPw(){}
    ~IdPw(){}
    string id; //账号
    string password; //密码
    IdPw *next;
};

//课程
class Subject
{
public:
    Subject(){}
    ~Subject(){}

    string subID; //课程号
    string subName; //课程名
    string subTearcherID; //任课教师工号
    string subTeacher; //任课老师
    string subCate; //课程类型
    float mark; //学分
    int maxNum; //课程最大容量
    int okNum; //选此课的人数
    Subject *next;
    Student *head_SubSt; //选此课的学生信息
};

//基类
class Base
{
public:
    Base(){}
    ~Base(){}
    void readShowSub(); //显示可选课程

    IdPw ip;
};

//学生
class Student : virtual public Base
{
public:
    Student(){}
    ~Student(){}
    void querySub(); //查询已选课程
    void selecSub(); //选课
    void quitSub(); //退课
    void modifyPw(); //修改密码

    int n; //已选课程数
    string stNo; //学号
    string name; //姓名
    string college; //学院
    string major; //专业
    string clas; //班级
    Student *next;
    Subject *head_stSub; //所选课程
};

//教师
class Teacher : virtual public Base
{
public:
    Teacher(){}
    ~Teacher(){}
    void querySubInfo(); //查询所教课程信息
    void modifyPw(); //修改密码
};

//管理员
class Manager : public Student, public Teacher
{
public:
    Manager(){}
    ~Manager(){}
    void addSub(); //添加课程
    void deleteSub(); //删除课程
    void modifySub(); //修改课程信息
    void setSelSubTime(); //设置选课时间
    void setQuitSubTime(); //设置退课时间
    void modifyPwM(); //修改密码
    void ShowSub();
    void writeSub(Subject *head_sub);
    void writeAddSub(Subject *p);
};

class Time
{
public:
    int year;
    int month;
    int day;
};

/**声明对象**/
Student st;
Teacher tea;
Manager ma;
Time start, endt;

/** Base **/
//读取课程文件
Subject* readSub()
{
    Subject *head_sub = new Subject;
    head_sub ->next = NULL;
    Subject *p, *q;
    q = head_sub;
    ifstream in("Subject.txt");
    if (!in)
    {
        cout << "\n\n\t\t\t\tCan't open \"Subject.txt\"!";
        exit(0);
    }

    p = new Subject;
    while (in >> p ->subID >> p ->subName >> p ->mark >> p ->okNum >> p ->maxNum >> p ->subTeacher >> p ->subTearcherID >> p ->subCate)
    {
        p ->next = NULL;
        q ->next = p;
        q = p;
        p = new Subject;
    }
    in.close();
    return head_sub;
}//Base::readSub

//显示可选课程
void Base::readShowSub()
{
    Subject *head_sub = new Subject;
    head_sub ->next = NULL;
    head_sub = readSub();
    Subject *p = head_sub ->next;
    cout << setiosflags(ios_base::left) << setw(4) << "序号" << setw(13) << "  课程编号" << setw(16) << "  课程名" << setw(6) << "学分" << setw(7) << "剩余" << setw(10) << "最大容量" << setw(10) <<"任课老师" << "课程属性\n";
    cout << "===========================================";
    cout << "====================================" << endl;
    int i = 1;
    while (p)
    {
        if (p ->maxNum - p ->okNum > 0)
            cout << setiosflags(ios_base::left)  << " " << setw(4) << i++ << setw(14) << p ->subID << setw(14) << p ->subName << setw(6) << p ->mark  << setw(8) << p ->maxNum - p ->okNum << setw(10) << p ->maxNum << setw(10) << p ->subTeacher << setw(10) <<p ->subCate << endl << endl;
        p = p ->next;
    }
}//Base::readShowSub

//读取账号密码文件
IdPw * read(string fname)
{
    IdPw *head_IP = new IdPw;
    IdPw *q = head_IP;

    ifstream in(fname);
    if (!in)
    {
        cout << "Can't open the file!" << endl;
        exit(0);
    }
    IdPw *p = new IdPw;
    while (in >> p ->id >> p ->password)
    {
        p ->next = NULL;
        q ->next = p;
        q = p;
        p = new IdPw;
    }
    in.close();
    return head_IP;
}//read()

//写入账号密码文件
void write(IdPw *head_ip, string fname)
{
    IdPw *p = head_ip ->next;
    head_ip ->next = NULL;
    ofstream ou(fname);
    if (!ou)
    {
        cout << "Can't open the file!" << endl;
        exit(0);
    }
    while(p)
    {
        ou << p ->id << " " << p ->password << endl;
        p = p ->next;
    }
    ou.close();
}//write

/** Student **/
//修改密码
void Student::modifyPw()
{
    IdPw *head_ip = new IdPw;
    head_ip ->next = NULL;
    string pw1, pw2, pw3;
    int flag = 1;

    head_ip = read("studentIP.txt");
    if (head_ip == NULL)
    {
        cout << "\n\n\t\t\t\t文件打开错误,密码修改失败!";
        return ;
    }
    IdPw *p = head_ip ->next;

    cout << "\n\t\t\t\t请输入原密码:";
    cin >> pw1;
    cout << "\n\n\t\t\t\t请输入新密码:";
    cin >> pw2;
    cout << "\n\n\t\t\t\t再次输入新密码:";
    cin >> pw3;

    p = head_ip ->next;
    while (p)
    {
        if (p ->id == st.ip.id)
        {
            if (p ->password == pw1)
            {
                if (pw2 == pw3)
                {
                    p ->password = pw2;
                    cout << "\n\n\t\t\t\t密码修改成功!";
                    cout << " *按Enter键返回!*" << endl;
                    write(head_ip, "studentIP.txt");
                }
                else
                {
                    cout << "\n\n\t\t\t\t两次新密码不一致!";
                }
                flag = 0;
            }
        }
        if (flag == 0)
            break;
        p = p ->next;
    }
    if (flag == 1)
        cout << "\n\n\t\t\t\t原密码输入错误!";
}//Student::modifyPw()

//读取选课时间
void readTime1()
{
    ifstream in("TimeSelect.txt");
    if (!in)
    {
        cout << "Can't open \"TimeSelect.txt\"!";
        exit(0);
    }
    in >> start.year >> start.month >> start.day;
    in >> endt.year >> endt.month >> endt.day;
}//readTime1()

//读取退课时间
void readTime2()
{
    ifstream in("TimeQuit.txt");
    if (!in)
    {
        cout << "Can't open \"TimeQuit.txt\"!";
        exit(0);
    }
    in >> start.year >> start.month >> start.day;
    in >> endt.year >> endt.month >> endt.day;
}//readTime2()

/** 计算两个日期的“时间差” **/
int diffDay(Time t1, Time t2)
{
    if (t1.month == t2.month)
        return t2.day-t1.day;
    return 31-t1.day+t2.day;
}//diffDay(Time t1, Time t2)

/** 读取学生-课程信息文件 **/
Student* readStSub()
{
    Student *head_st = new Student;
    head_st ->next = NULL;
    Student *p, *q;
    p = new Student;
    q = head_st;
    q ->next = NULL;

    p ->head_stSub = new Subject;
    p ->head_stSub ->next = NULL;
    Subject *s, *t;
    t = p ->head_stSub;
    t ->next = NULL;
    // 读取学生选课文件
    ifstream in("StSubject.txt");
    if (!in)
    {
        cout << "Can't open \"StSubject.txt\"!";
        exit(0);
    }
    while (in >> p ->stNo >> p ->n)
    {
        for (int i = 0; i < p ->n; i++)
        {
            s = new Subject;
            in >> s ->subID;
            s ->next = NULL;
            t ->next = s;
            t = s;
        }
        p ->next = NULL;
        q ->next = p;
        q = p;
        p = new Student;
        p ->head_stSub = new Subject;
        t = p ->head_stSub;
        t ->next = NULL;
    }//while
    in.close();
    return head_st;
}//readStSub()

/** 更新学生-课程信息文件 **/
void writeStSub(Student *head_st)
{
    Student *p;
    Subject *s;
    ofstream ou2("StSubject.txt");
    if (!ou2)
    {
        cout << "Can't open \"StSubject.txt\"!";
        exit(0);
    }
    p = head_st ->next;
    while (p)
    {
        //该学生所选的课程数加一
        if (p ->stNo == st.ip.id)
           p ->n++;

        ou2 << p ->stNo << " " << p ->n;
        s = p ->head_stSub ->next;
        while (s)
        {
            ou2 << " " << s ->subID;
            s = s ->next;
        }
        ou2 << endl;
        p = p ->next;
    }
    ou2.close();
}//writeStSub()

/** 学生选课 **/
void Student::selecSub()
{
    //判断是否是选课时间
    Time nowTime;
    time_t t = time(NULL);
    tm *tt = localtime(&t);
    nowTime.year = tt ->tm_year+1900;
    nowTime.month = tt ->tm_mon+1;
    nowTime.day = tt ->tm_mday;
    readTime1();
    int x = diffDay(start, endt);
    int y = diffDay(start, nowTime);

    if (x >= y)
    {
        Subject *head_sub = new Subject;
        head_sub ->next = NULL;
        head_sub = readSub();
        Subject *a = head_sub ->next;

        //显示可选课程
        readShowSub();
        int c;
        cout << "\n  请输入要选择的课程(序号):";
        cin >> c;

        //根据输入的序号定位课程
        int i = 1;
        int flag = 0;
        while (a)
        {
            if (c == i)
            {
                flag = 1;
                break;
            }
            if (a ->maxNum - a ->okNum > 0)
                i++;
            a = a ->next;
        }
        if (flag == 1)
        {
            /** 判断是否已选此课,并更新信息 **/
            Student *head_st = new Student;
            head_st ->next = NULL;
            head_st = readStSub();
            Student *p;
            p = new Student;

            p ->head_stSub = new Subject;
            p ->next = NULL;
            Subject *s, *t;

            //判断该学生本学期是否已选本课程
            int flag1 = 0, flag2 = 1;
            p = head_st ->next;
            while (p)
            {
                if (p ->stNo == st.ip.id)
                {
                    flag1 = 1;
                    s = p ->head_stSub ->next;
                    t = p ->head_stSub;
                    while (s)
                    {
                        if (s ->subID == a ->subID)
                        {
                            flag2 = 0;
                            break;
                        }
                        t = s;
                        s = s ->next;
                    }
                    break;
                }//if
                p = p ->next;
            }//while

            //更新学生-课程文件
            if (flag1 == 1 && flag2 == 1)
            {
                //将新选课程加入链表
                s = new Subject;
                s ->subID = a ->subID;
                s ->next = NULL;
                t ->next = s;

                writeStSub(head_st);

                //修改课程信息
                a ->okNum++;
                ma.writeSub(head_sub);

                //更新课程_学生信息
                Student *head_st2 = new Student;
                Student *e = new Student, *r;
                r = head_st2;
                r ->next = NULL;

                //读取学生基本信息
                ifstream in2("Student.txt");
                if (!in2)
                {
                    cout << "Can't open \"Student.txt\"!";
                    exit(0);
                }
                while (in2 >> e ->stNo >> e ->name >> e ->college >> e ->major >> e ->clas)
                {
                    e ->next = NULL;
                    r ->next = e;
                    r = e;
                    e = new Student;
                }
                in2.close();

                //将学生信息加入到课程-学生文件
                r = head_st2 ->next;
                while (r)
                {
                    if (r ->stNo == st.ip.id)
                        break;
                    r = r ->next;
                }

                string ssss = a ->subID + ".txt";
                ofstream ou(ssss, ios::app);
                if (!ou)
                {
                    cout << "Can't open \"" << ssss << "\"!";
                    exit(0);
                }
                ou << r ->stNo << " " << r ->name << " " << r ->college << " " << r ->major << " " << r ->clas << endl;
                ou.close();
                cout << "选课成功!";
                cout << " *按Enter键返回!*" << endl;
            }//if
            else
            {
                cout << " 你本学期已选本课程!" << endl;
            }
        }//if
        else
        {
            cout << " 输入错误!" << endl;
        }
    }//if (x >= y)
    else
        cout << "现在不是选课时间!";
}//Student::selecSub()

/** 查看已选课程 **/
void Student::querySub()
{
    Subject *head_Sub = new Subject;
    head_Sub ->next = NULL;
    Subject *p, *s;
    Student *head_st  =new Student;
    head_st ->next = NULL;
    Student *sp;

    head_Sub = readSub();
    head_st = readStSub();
    sp = head_st ->next;
    int i = 1;

    cout << "序号     课程号    " <<  "    课程名      " << "  学分 " << " 任课教师 " << " 课程属性" << endl;
    cout << "===========================================";
    cout << "===================================" << endl;
    while (sp)
    {
        if (sp ->stNo == st.ip.id)
        {
            s = sp ->head_stSub ->next;
            while (s)
            {
                p = head_Sub;
                while (p)
                {
                    if (p ->subID == s ->subID)
                    {
                        cout << " " << setiosflags(ios_base::left) << setw(6) << i++ << setw(13) << p ->subID << setw(18) << p ->subName << setw(6) << p ->mark << setw(10) << p ->subTeacher << p ->subCate << endl << endl;
                        break;
                    }
                    p = p ->next;
                }//while
                s = s ->next;
            }//while
            break;
        }//if
        sp = sp ->next;
    }//while
}//Student::querySub()

/** 读取课程-学生文件 **/
Student * readSubSt(string s)
{
    Student *head = new Student;
    Student *r = new Student, *q;
    q = head;
    q ->next = NULL;

    ifstream in(s);
    if (!in)
    {
        cout << "Can't open \"" << s << "\"!";
        exit(0);
    }
    while (in >> r ->stNo >> r ->name >> r ->college >> r ->major >> r ->clas)
    {
        r ->next = NULL;
        q ->next = r;
        q = r;
        r = new Student;
    }
    in.close();
    return head;
}//readSubSt

/** 退课 **/
void Student::quitSub()
{
    //判断是否是选课时间
    Time nowTime;
    time_t t = time(NULL);
    tm *tt = localtime(&t);
    nowTime.year = tt ->tm_year+1900;
    nowTime.month = tt ->tm_mon+1;
    nowTime.day = tt ->tm_mday;
    readTime2();
    int x = diffDay(start, endt);
    int y = diffDay(start, nowTime);
    if (x >= y)
    {
        //显示已选课程
        querySub();
        int c;
        cout << "请选择退选课的序号:";
        cin >> c;

        //定位课程
        Student *head_st = new Student;
        head_st ->next = NULL;
        Student *p;
        head_st = readStSub();
        int i = 1;
        Subject *s;
        string subId;

        p = head_st ->next;
        while (p)
        {
            if (p ->stNo == st.ip.id)
            {
                s = p ->head_stSub ->next;
                while (s)
                {
                    i++;
                    s = s ->next;
                }
                break;
            }
        }//while
        if (c <= i && c > 0)
        {
            i = 1;
            //更新学生-课程文件
            ofstream ou("StSubject.txt");
            if (!ou)
            {
                cout << "Can't open \"StSubject.txt\"!";
                exit(0);
            }
            p = head_st ->next;
            while (p)
            {
                if (p ->stNo == st.ip.id)
                {
                    p ->n--;
                    ou << p ->stNo << " " << p ->n;
                    s = p ->head_stSub ->next;
                    while (s)
                    {
                        if (i != c)
                            ou << " " << s ->subID;
                        else
                            subId = s ->subID;
                        i++;
                        s = s ->next;
                    }
                }//if
                else
                {
                    ou << p ->stNo << " " << p ->n;
                    s = p ->head_stSub ->next;
                    while (s)
                    {
                        ou << " " << s ->subID;
                        s = s ->next;
                    }
                }
                ou << endl;
                p = p ->next;
            }//while
            ou.close();

            //更新课程文件
            Subject *head_sub = new Subject;
            head_sub ->next = NULL;
            head_sub = readSub();
            s = head_sub ->next;
            while (s)
            {
                if (s ->subID == subId)
                {
                    s ->okNum--;
                    break;
                }
                s = s ->next;
            }
            ofstream ou2("Subject.txt");
            if (!ou2)
            {
                cout << "Can't open the file!";
                exit(0);
            }
            s = head_sub ->next;
            while (s)
            {
                ou2 << s ->subID << " " << s ->subName << " " << s ->mark << " " << s ->okNum << " " << s ->maxNum << " " << s ->subTeacher << " " << s ->subTearcherID << " " << s ->subCate << endl;
                s = s ->next;
            }
            ou2.close();

            //更新课程-学生文件
            Student *head_st2 = new Student;
            head_st2 ->next = NULL;
            string sss = subId + ".txt";
            head_st2 = readSubSt(sss);

            ofstream ou3(sss);
            if (!ou3)
            {
                cout << "Can't open the file!";
                exit(0);
            }
            p = head_st2 ->next;
            while (p)
            {
                if (p ->stNo != st.ip.id)
                    ou << p ->stNo << " " << p ->name << " " << p ->college << " " << p ->major << " " << p ->clas << endl;
                p = p ->next;
            }
            ou3.close();
            cout << "退课成功!";
            cout << " *按Enter键返回!*" << endl;
        }//if
        else
        {
            cout << "输入错误!";
        }
    }//if
    else
    {
        cout << "现在不是退课时间!";
    }
}//Student::quitSub()

/** Teacher **/
//修改密码
void Teacher::modifyPw()
{
    IdPw *head_ip = new IdPw;
    head_ip ->next = NULL;
    string pw1, pw2, pw3;
    int flag = 1;

    head_ip = read("TeacherIP.txt");
    if (head_ip == NULL)
    {
        cout << "\n\n\t\t\t\t文件打开错误,密码修改失败!";
        return ;
    }
    IdPw *p = head_ip ->next;

    cout << "\n\t\t\t\t请输入原密码:";
    cin >> pw1;
    cout << "\n\n\t\t\t\t请输入新密码:";
    cin >> pw2;
    cout << "\n\n\t\t\t\t再次输入新密码:";
    cin >> pw3;

    p = head_ip ->next;
    while (p)
    {
        if (p ->id == tea.ip.id)
        {
            if (p ->password == pw1)
            {
                if (pw2 == pw3)
                {
                    p ->password = pw2;
                    cout << "\n\n\t\t\t\t密码修改成功!";
                    write(head_ip, "TeacherIP.txt");
                }
                else
                {
                    cout << "\n\n\t\t\t\t两次新密码不一致!";
                }
                flag = 0;
            }//if
        }
        if (flag == 0)
            break;
        p = p ->next;
    }
    if (flag == 1)
        cout << "\n\n\t\t\t\t原密码输入错误!";
}//Teacher::modifyPw

/** 查询所教课程信息  **/
void Teacher::querySubInfo()
{
    Subject *head_sub = new Subject;
    head_sub ->next = NULL;
    Student *head_SubSt = new Student;
    head_SubSt ->next = NULL;
    head_sub = readSub();

    Subject *p = head_sub ->next;
    cout << " 序号" << "   课程编号\t" << "课程名\t" << "  学分  " << "选课人数" << "  最大容量  " << "任课老师" << "  课程属性\n";
    cout << "===========================================";
    cout << "===================================" << endl;
    int i = 1;
    while (p)
    {
        if (p ->subTearcherID == tea.ip.id)
            cout << setw(3) << i++  << "."<< setw(13) << p ->subID << setw(15) << p ->subName << setw(4) << p ->mark << setw(8) << p ->okNum << setw(12) << p ->maxNum << setw(12) << p ->subTeacher << p ->subCate << endl << endl;
        p = p ->next;
    }

    string c;
    cout << "\n  *1-查看详细信息   0-返回*" << endl << endl;
    cout << "   请选择:";
    cin >> c;
    cout << endl;
    if (c == "1")
    {
        cout << " *请选择科目序号: ";
        int c2, t = 1;
        cin >> c2;
        int flag = 0;
        p = head_sub ->next;
        while (p)
        {
            if (tea.ip.id == p ->subTearcherID)
            {
                if (t == c2)
                {
                    flag = 1;
                    break;
                }
                t++;
            }
            p = p ->next;
        }
        if (flag == 1)
        {
            string subId = p ->subID + ".txt";
            head_SubSt = readSubSt(subId);

            Student *head_new = new Student, *p1, *t, *r, *s;
            p1 = head_new ->next = head_SubSt ->next;
            p1 = p1 ->next;
            head_new ->next ->next = NULL;
            while (p1)
            {
                t = p1 ->next;
                r = head_new ->next;
                s = head_new;
                while (r!=NULL && p1 ->stNo > r ->stNo)
                {
                    s = r;
                    r = r ->next;
                }
                s ->next = p1;
                p1 ->next = r;
                p1 = t;
            }



            cout << "序号 " << "     学号  " << "    姓名    " <<  "   学院  " << "    专业班级" << endl;
            cout << "===========================================";
            cout << "===================================" << endl;
            int t2 = 1;
            Student *sp = head_new ->next;
            while (sp)
            {
                cout << " " << setiosflags(ios_base::left) << setw(6) << t2++ << setw(12) << sp ->stNo << setw(10) << sp ->name << setw(12) << sp ->college << sp ->major << sp ->clas << endl << endl;
                sp = sp ->next;
            }
        }//if
        else
        {
            cout << "*输入错误! ";
        }
    }//if
    else if (c != "0")
    {
        cout << "*输入错误! ";
    }
    cout << " 按Enter返回!";
}//Teacher::querySubInfo()

/** Manager **/
/** 写入课程文件 **/
void Manager::writeSub(Subject *head_sub)
{
    Subject *p;

    ofstream ou("Subject.txt");
    if (!ou)
    {
        cout << "\n\n\t\t\t\tCan't open the file!";
        exit(0);
    }
    p = head_sub ->next;
    while (p)
    {
        ou << p ->subID << " " << p ->subName << " " << p ->mark << " " << p ->okNum << " " << p ->maxNum << " " << p ->subTeacher << " " << p ->subTearcherID << " " << p ->subCate << endl;
        p = p ->next;
    }
    ou.close();
}//Manager::writeSub

/** 写入添加课程文件 **/
void Manager::writeAddSub(Subject *p)
{
    ofstream ou("Subject.txt", ios::app);
    if (!ou)
    {
        cout << "\n\n\t\t\t\tCan't open the file!";
        exit(0);
    }
    ou << p ->subID << " " << p ->subName << " " << p ->mark << " " << p ->okNum << " " << p ->maxNum << " " << p ->subTeacher << " " << p ->subTearcherID << " " << p ->subCate << endl;
    ou.close();
}//Manager::writeAddSub

/** 添加课程 **/
void Manager::addSub()
{
    ifstream in("Subject.txt");
    if (!in)
    {
        cout << "Can't open the file!\n";
        exit(0);
    }
    Subject *head_sub = new Subject;
    head_sub ->next = NULL;
    head_sub = readSub();
    Subject *q;

    Subject *p = new Subject;
    int y;
    do
    {
        y = 0;
        cout << "请输入课程号:";
        cin >> p ->subID;
        q = head_sub ->next;
        while (q)
        {
            if (q ->subID == p ->subID)
            {
                y = 1;
                cout << "该课程号已存在,请重新输入!" << endl;
                break;
            }
            q = q ->next;
        }
    }while (y == 1);
    cout << "请输入课程名:";
    cin >> p ->subName;
    cout << "请输入课程学分:";
    cin >> p ->mark;
    cout << "请输入最大学生容量:";
    cin >> p ->maxNum;
    cout << "请输入课程类型归属:";
    cin >> p ->subCate;
    cout << "请输入任课教师工号:";
    cin >> p ->subTearcherID;
    cout << "请输入任课教师姓名:";
    cin >> p ->subTeacher;
    p ->okNum = 0;
    cout << "确定要添加此课程?\n";
    cout << " 1-确定  0-取消\n";
    cout << "请选择:";
    int c;
    cin >> c;
    if (c == 1)
    {
        writeAddSub(p);
        cout << "添加课程成功!";
    }
    string s = p ->subID + ".txt";
    ofstream ou(s);
    ou.close();
    cout << " *按Enter键返回!*" << endl;
}//Manager::addSub()

/** 修改课程信息 **/
void Manager::modifySub()
{
    ifstream in("Subject.txt");
    if (!in)
    {
        cout << "Can't open the file!\n";
        exit(0);
    }
    Subject *head_sub = new Subject;
    head_sub ->next = NULL;
    string s;
    head_sub = readSub();
    Subject *p = head_sub ->next;

    int y = 1;
    int choice;
    while (y)
    {
        system("cls");
        cout << "                           ------------------\n";
        cout << "                              *选 课 系 统*\n";
        cout << "                                *修改课程*\n";
        cout << "                           ------------------\n\n";
        cout << "               *******************************************\n\n";
        cout << "               *******************************************\n\n";
        cout << "               || 1-修改课程名        2-修改课程学分    ||\n\n";
        cout << "               || 3-修改课程容量      4-修改任课教师    ||\n\n";
        cout << "               || 5-修改课程归属      6-全部修改        ||\n\n";
        cout << "               || 0-退出修改                            ||\n\n";
        cout << "               *******************************************\n\n";
        cout << "               *******************************************\n\n";
        cout << "                请输入所选择的序号:";
        cin >> choice;
        if(choice >= 0 && choice <= 6)
            y = 0;
        else
        {
            cout << "               输入错误,请重新输入!\n";
            getchar();
            getchar();
            system("cls");
        }//switch(choice)
    }//while(y)
    if (choice != 0)
    {
        cout << "请输入要修改课程的课程号:";
        cin >> s;
    }
    p = head_sub ->next;
    int flag = 1;

    while (p)
    {
        if (p ->subID == s)
        {
            flag = 0;
            break;
        }
        p = p ->next;
    }
    if (flag == 0)
    {
        int flag = 1;
        switch(choice)
        {
            case 0: break;
            case 1:
                cout << "请输入修改后的课程名:";
                cin >> p ->subName;
                break;
            case 2:
                cout << "请输入修改后的课程学分:";
                cin >> p ->mark;
                break;
            case 3:
                cout << "请输入修改后的课程容量:";
                cin >> p ->maxNum;
                break;
            case 4:
                cout << "请输入修改后的任课教师工号:";
                cin >> p ->subTearcherID;
                cout << "请输入修改后的任课教师:";
                cin >> p ->subTeacher;
                break;
            case 5:
                cout << "请输入修改后的课程归属:";
                cin >> p ->subCate;
                break;
            case 6:
                cout << "请输入修改后的课程名:";
                cin >> p ->subName;
                cout << "请输入修改后的课程学分:";
                cin >> p ->mark;
                cout << "请输入修改后的课程容量:";
                cin >> p ->maxNum;
                cout << "请输入修改后的任课教师工号:";
                cin >> p ->subTearcherID;
                cout << "请输入修改后的任课教师:";
                cin >> p ->subTeacher;
                break;
            default :
                flag = 0;
                break;
        }//switch
        if (flag == 1)
        {
            cout << "\n确定要修改课程信息?";
            cout << "\n  1-确定  0-取消\n";
            cout << "请选择:";
            int c;
            cin >> c;
            if (c == 1)
            {
                writeSub(head_sub);
                cout << "修改课程成功!";
            }
        }
        else
            cout << "输入错误,修改失败!";
    }//if
    else if (choice == 0)
    {
        ;
    }
    else
    {
        cout << "未找到此课程号对应的信息!";
        getchar();
    }
    cout << " *按Enter键返回!*" << endl;
}//Manager::modifySub

/** 删除课程信息 **/
void Manager::deleteSub()
{
    Subject *head_sub = new Subject;
    head_sub ->next = NULL;
    Subject *p, *q;
    head_sub = readSub();

    string s;
    cout << "请输入要删除课程的课程号:";
    cin >> s;
    p = head_sub ->next;
    q = head_sub;
    int flag = 1;
    while (p)
    {
        if (p ->subID == s)
        {
            flag = 0;
            break;
        }
        q = p;
        p = p ->next;
    }
    if (flag == 0)
    {
        cout << "要删除的课程信息为:\n";
        cout << "课程号:" << p ->subID << endl;
        cout << "课程名:" << p ->subName << endl;
        cout << "任课教师:" << p ->subTeacher << endl << endl;
        cout << "\n确定要修改课程信息?";
        cout << "\n  1-确定  0-取消\n";
        cout << "请选择:";
        int c;
        cin >> c;
        if (c == 1)
        {
            q ->next = p ->next;
            free(p);
            writeSub(head_sub);
            char sn[20];
            int len = s.size();
            for (int i = 0; i <= len; i++)
            {
                sn[i] = s[i];
            }
            strcat(sn, ".txt");
            remove(sn);
            cout << "\n删除成功!\n";

        }//if
    }//if
    else
    {
        cout << "未找到该课程!";
        getchar();
    }
    cout << " *按Enter键返回!*" << endl;
}//Manager::deleteSub

/**设置选课时间**/
void Manager::setSelSubTime()
{
    cout << "选课开始时间\n";
    cout << "年:"; cin >> start.year;
    cout << "月:"; cin >> start.month;
    cout << "日:"; cin >> start.day;
    cout << "选课结束时间\n";
    cout << "年:"; cin >> endt.year;
    cout << "月:"; cin >> endt.month;
    cout << "日:"; cin >> endt.day;
    ofstream ou("TimeSelect.txt");
    if (!ou)
    {
        cout << "Can't open \"TimeSelect.txt\"!";
        exit(0);
    }
    ou << start.year << " " << start.month << " " << start.day << endl;
    ou << endt.year << " " << endt.month << " " << endt.day << endl;
    ou.close();
    cout << "设置成功! *按Enter键返回!*" << endl;
}//Manager::setSelSubTime

/**设置选课时间**/
void Manager::setQuitSubTime()
{
    cout << "退课开始时间\n";
    cout << "年:"; cin >> start.year;
    cout << "月:"; cin >> start.month;
    cout << "日:"; cin >> start.day;
    cout << "退课结束时间\n";
    cout << "年:"; cin >> endt.year;
    cout << "月:"; cin >> endt.month;
    cout << "日:"; cin >> endt.day;
    ofstream ou("TimeQuit.txt");
    if (!ou)
    {
        cout << "Can't open \"TimeQuit.txt\"!";
        exit(0);
    }
    ou << start.year << " " << start.month << " " << start.day << endl;
    ou << endt.year << " " << endt.month << " " << endt.day << endl;
    ou.close();
    cout << "设置成功! *按Enter键返回!*" << endl;
}//Manager::setQuitSubTime

//修改密码
void Manager::modifyPwM()
{
    IdPw *head_ip = new IdPw;
    string pw1, pw2, pw3;
    int flag = 1;

    head_ip = read("ManagerIP.txt");
    if (head_ip == NULL)
    {
        cout << "\n\n\t\t\t\t文件打开错误,密码修改失败!";
        return ;
    }
    IdPw *p = head_ip ->next;

    cout << "\n\t\t\t\t请输入原密码:";
    cin >> pw1;
    cout << "\n\n\t\t\t\t请输入新密码:";
    cin >> pw2;
    cout << "\n\n\t\t\t\t再次输入新密码:";
    cin >> pw3;

    p = head_ip ->next;
    while (p)
    {
        if (p ->id == ma.ip.id)
        {
            if (p ->password == pw1)
            {
                if (pw2 == pw3)
                {
                    p ->password = pw2;
                    cout << "\n\n\t\t\t\t密码修改成功!";
                    cout << " *按Enter键返回!*" << endl;
                    write(head_ip, "ManagerIP.txt");
                }
                else
                {
                    cout << "\n\n\t\t\t\t两次新密码不一致!";
                }
                flag = 0;
            }
        }
        if (flag == 0)
            break;
        p = p ->next;
    }
    if (flag == 1)
        cout << "\n\n\t\t\t\t原密码输入错误!";
}//Manager::modifyPwM

void Manager::ShowSub()
{
    Subject *head_sub = new Subject;
    head_sub ->next = NULL;
    head_sub = readSub();
    Subject *p = head_sub ->next;
    cout << setiosflags(ios_base::left) << setw(4) << "序号" << setw(13) << "  课程编号" << setw(16) << "  课程名" << setw(6) << "学分" << setw(7) << "剩余" << setw(10) << "最大容量" << setw(10) <<"任课老师" << "课程属性\n";
    cout << "===========================================";
    cout << "====================================" << endl;
    int i = 1;
    while (p)
    {
            cout << setiosflags(ios_base::left)  << " " << setw(4) << i++ << setw(14) << p ->subID << setw(14) << p ->subName << setw(6) << p ->mark  << setw(8) << p ->maxNum - p ->okNum << setw(10) << p ->maxNum << setw(10) << p ->subTeacher << setw(10) <<p ->subCate << endl << endl;
        p = p ->next;
    }
}//Manager::ShowSub()

/** load() **/
int key;

//登录界面
void loadMenu(int t)
{
    char pw[20];
    system("cls");
    cout << "\n\n\n\n\n\n\n";
    cout << "             ╔*☆***◎***◇***☆*** 欢迎光临 ***☆***◇***◎***☆*╗\n";
    cout << "         ************************* 教 务 系 统 ************************\n\n";
    cout << endl << endl;
    cout << "\t\t\t\t请输入用户名:";
    switch(t)
    {
        case 1: cin >> st.ip.id; break;
        case 2: cin >> tea.ip.id; break;
        case 3: cin >> ma.ip.id; break;
    }
    cout << "\n\t\t\t\t请输入密码: ";
    int i = 0;
    char c;
    do
    {
        c = getch();
        if (c == '\b')
        {
            cout << "\b";
            cout << '\0';
            i--;
            cout << "\b";
        }
        else if (c != '\r')
        {
            cout << "*";
            pw[i] = c;
            i++;
        }
    }while (c != '\r');
    pw[i] = '\0';
    string pw1 = (string) pw;
    switch(t)
    {
        case 1: st.ip.password = pw1; break;
        case 2: tea.ip.password = pw1; break;
        case 3: ma.ip.password = pw1; break;
    }
}//loadMenu()

//学生登录
int load_student()
{
    int flag = 0;
    loadMenu(1);
    ifstream in("studentIP.txt");
    if (!in)
    {
        cout << "Can't open \"studentIP.txt\"!" << endl;
        exit(0);
    }
    string ids, pws;
    while (in >> ids >> pws)
    {
        if (ids == st.ip.id)
        {
            flag = 1;
            break;
        }
    }
    in.close();

    if (flag == 0)
    {
        key = 0;
        cout << "\n\n\t\t\t\t账号不存在!";
        getchar();
        getchar();
        return 0;
    }

    if (pws == st.ip.password)
    {
        return 1;
    }
    return 0;
}//load_student()

//教师登录
int load_teacher()
{
    int flag = 0;
    loadMenu(2);
    ifstream in("TeacherIP.txt");
    if (!in)
    {
        cout << "Can't open \"TeacherIP.txt\"!" << endl;
        exit(0);
    }
    string ids, pws;
    while (in >> ids >> pws)
    {
        if (ids == tea.ip.id)
        {
            flag = 1;
            break;
        }
    }
    in.close();

    if (flag == 0)
    {
        key = 0;
        cout << "\n\n\t\t\t\t账号不存在!";
        getchar();
        getchar();
        return 0;
    }

    if (pws == tea.ip.password)
    {
        return 1;
    }
    return 0;
}//load_teacher()

//管理员登录
int load_manager()
{
    int flag = 0;
    loadMenu(3);
    ifstream in("ManagerIP.txt");
    if (!in)
    {
        cout << "Can't open \"ManagerIP.txt\"!" << endl;
        exit(0);
    }
    string ids, pws;
    while (in >> ids >> pws)
    {
        if (ids == ma.ip.id)
        {
            flag = 1;
            break;
        }
    }
    in.close();

    if (flag == 0)
    {
        key = 0;
        cout << "\n\n\t\t\t\t账号不存在!";
        getchar();
        getchar();
        return 0;
    }

    if (pws == ma.ip.password)
    {
        return 1;
    }
    return 0;
}//load_manager()

void Menu_Student()
{
    int y = 1;
    int choice;
    while (y)
    {
        system("cls");
        cout << "                           ------------------\n";
        cout << "                              *选 课 系 统*\n";
        cout << "                           ------------------\n\n";
        cout << "               *******************************************\n\n";
        cout << "               *******************************************\n\n";
        cout << "               || 1-选课              2-查看可选课程    ||\n\n";
        cout << "               || 3-退课              4-查看已选课程    ||\n\n";
        cout << "               || 5-修改密码          0-退出系统        ||\n\n";
        cout << "               *******************************************\n\n";
        cout << "               *******************************************\n\n";
        cout << "                请输入所选择的序号:";
        cin >> choice;
        system("cls");
        switch (choice)
        {
            case 0: y = 0; break;
            case 1: st.selecSub(); getchar(); break;
            case 2: st.readShowSub(); cout << "按Enter键返回!"; getchar(); break;
            case 3: st.quitSub(); getchar(); break;
            case 4: st.querySub(); cout << "按Enter键返回!"; getchar(); break;
            case 5: st.modifyPw(); getchar(); break;
            default:
                cout << "               输入错误,请重新输入!\n";
                getchar();
                break;
        }//switch(choice)
        getchar();
    }//while(y)
}//Menu_Student()

void Menu_Teacher()
{
    int y = 1;
    int choice;
    while (y)
    {
        system("cls");
        cout << "                           ------------------\n";
        cout << "                              *选 课 系 统*\n";
        cout << "                           ------------------\n\n";
        cout << "               *******************************************\n\n";
        cout << "               *******************************************\n\n";
        cout << "               || 1-查看所教课程信息      2-修改密码    ||\n\n";
        cout << "               || 0-退出系统                            ||\n\n";
        cout << "               *******************************************\n\n";
        cout << "               *******************************************\n\n";
        cout << "                请输入所选择的序号:";
        cin >> choice;
        system("cls");
        switch (choice)
        {
            case 0: y = 0; break;
            case 1: tea.querySubInfo(); getchar(); break;
            case 2: tea.modifyPw(); getchar(); break;
            default:
                cout << "               输入错误,请重新输入!\n";
                getchar();
                break;
        }//switch(choice)
        getchar();
    }//while(y)
}//Menu_Teacher()

void Menu_Manager()
{
    int y = 1;
    int choice;
    while (y)
    {
        system("cls");
        cout << "                           ------------------\n";
        cout << "                              *选 课 系 统*\n";
        cout << "                           ------------------\n\n";
        cout << "               *******************************************\n\n";
        cout << "               *******************************************\n\n";
        cout << "               || 1-添加课程          2-修改课程信息    ||\n\n";
        cout << "               || 3-删除课程          4-修改密码        ||\n\n";
        cout << "               || 5-设置选课时间      6-设置退课时间    ||\n\n";
        cout << "               || 7-查看可选课程      0-退出系统        ||\n\n";
        cout << "               *******************************************\n\n";
        cout << "               *******************************************\n\n";
        cout << "                请输入所选择的序号:";
        cin >> choice;
        system("cls");
        switch (choice)
        {
            case 0: y = 0; break;
            case 1: ma.addSub(); getchar(); break;
            case 2: ma.modifySub(); getchar(); break;
            case 3: ma.deleteSub(); getchar(); break;
            case 4: ma.modifyPwM(); getchar(); break;
            case 5: ma.setSelSubTime(); getchar(); break;
            case 6: ma.setQuitSubTime(); getchar(); break;
            case 7: ma.ShowSub(); cout << "*按Enter键返回!"; getchar(); break;
            default:
                cout << "               输入错误,请重新输入!\n";
                getchar();
                break;
        }//switch(choice)
        getchar();
    }//while(y)
}//Menu_Manager()

//登录
void load()
{
    int n;
    int y = 1;
    while (y)
    {
        key = 1;
        system("cls");
        cout << "\n\n\n\n\n\n\n";
        cout << "             ╔*☆***◎***◇***☆*** 欢迎光临 ***☆***◇***◎***☆*╗\n";
        cout << "         ************************* 教 务 系 统 ************************\n\n";
        cout << endl << endl;
        cout << "                      ============1-学生登录===========\n";
        cout << "                      ============2-教师登录===========\n";
        cout << "                      ============3-管理员登录=========\n";
        cout << "                      ============0-退出系统===========\n";
        cout << "       请输入您的选择:";
        cin >> n;
        cout << endl;
        switch(n)
        {
            case 0: y = 0; break;
            case 1: if (load_student() == 0 && key == 1)
                    {
                        cout << "\n\n\n\t\t\t\t密码输入错误!";
                        getchar();
                        getchar();
                    }
                    else if (key == 1)
                    {
                        cout << "\n\n\n\t\t\t\t登录成功,按Enter进入!";
                        getchar();
                        getchar();
                        Menu_Student();
                    }
                    break;
            case 2: if(load_teacher() == 0 && key == 1)
                    {
                        cout << "\n\n\n\t\t\t\t密码输入错误!";
                        getchar();
                        getchar();
                    }
                    else if (key == 1)
                    {
                        cout << "\n\n\n\t\t\t\t登录成功,按Enter进入!";
                        getchar();
                        getchar();
                        Menu_Teacher();
                    }
                    break;
            case 3: if(load_manager() == 0 && key == 1)
                    {
                        cout << "\n\n\n\t\t\t\t密码输入错误!";
                        getchar();
                        getchar();
                    }
                    else if (key == 1)
                    {
                        cout << "\n\n\n\t\t\t\t登录成功,按Enter进入!";
                        getchar();
                        getchar();
                        Menu_Manager();
                    }
                    break;
            default : y = 1; cout << "\n\n\t\t\t\t输入错误!";
        }//switch
    }//while(y)
}//load()

int main()
{
    load();
    return 0;
}
发布了132 篇原创文章 · 获赞 136 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Aibiabcheng/article/details/104620999