| 网站首页 | 业界新闻 | 群组 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
雷速体育发帖软件开发QQ118000023C语言培训|一对一辅导|零基础学编程LightningChart 快速先进的.Net图表控件
共有 404 人关注过本帖
标题:一个简单的选课系统
只看楼主 收藏
yjrhgvbn
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-9-12
结帖率:0
  已结贴   问题点数:20  回复次数:1   
一个简单的选课系统
自己写的一个简单的控制选课系统,因为没考虑太多,使用没有成绩查看还有密码输入之类的系统,主要的内容是用vector创建了几个结构体数组,有兴趣的可以看一看,有错误的地方还请指出
程序代码:

#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<iomanip>
using namespace std;
struct Course//课程信息
{
    long ID;
    string name;
};
vector<Course>course;//课程数组
struct Student      
{
    string name;
    long ID;
    vector<Course>Syllabus;
    bool isCouseSelect(long    ID);
    void Acourse();
    void Dcourse();
    void Show();
};
vector<Student>student;//学生数组

template<typename T>
void EnterNum(T &i)   //个人习惯,用来输入数字还可以验错,就当cin>>看
{
    cin >> i;
    if (!cin)         //输入失败,比如输入了字母
    {
        cout << "错误输入,请输入数字:";
        cin.clear();            
        while (cin.get() != '\n');  
        cin >> i;
    }
}
Course GetCourse(long ID)   //通过ID获得一个课程,循环一遍就可以
{
    for (size_t i = 0; i < course.size(); i++)
        if (ID == course[i].ID)
            return course[i];
    throw 0;                 //没有找到就使用异常拋回,不懂异常处理的话可以直接先使用一个函数判断课程不存在,比如下面的isCourseHave函数
}
void CourseShow()     //课程全部展示,循环
{
    for (size_t i = 0; i < course.size(); i++)
        cout << "#" << i << "课程编号:" << course[i].ID << " 名称:" << course[i].name << endl;
}
bool isCourseHave(long ID)     //判断课程的存在,一样是循环
{
    for (size_t i = 0; i < course.size(); i++)
        if (course[i].ID == ID)
            return true;
    return false;
}
bool Student::isCouseSelect(long ID)   //判断学生要选的课程是已经选上,循环查看一下保存学生选课信息的数组就可以
{
    for (size_t i = 0; i < Syllabus.size(); i++)
        if (Syllabus[i].ID == ID)
            return true;
    return false;
}
void Student::Acourse()               //学生选课
{
    long ID;
    cout << "请输入课程ID:";
    EnterNum(ID);
    while (!isCourseHave(ID))         //判断课程是否存在了
    {
        cout << "没有该课程,请重新输入(0退出)\n";
        EnterNum(ID);
        if (!ID)         
            return;
    }
    if (!isCouseSelect(ID))
    {
        this->Syllabus.push_back(GetCourse(ID));
        cout << "成功选课\n";
    }
    else
    {
        cout << "你已经选上本课程了\n";
    }
}
void Student::Dcourse()  //删除课程
{
    long ID;
    cout << "请输入课程ID:";
    EnterNum(ID);
    while (!isCourseHave(ID))
    {
        cout << "没有该课程,请重新输入(0退出)\n";
        EnterNum(ID);
        if (!ID)
            break;
    }
    if (isCouseSelect(ID))
    {
        for (auto p = Syllabus.begin(); p != Syllabus.end();)
        {
            if (p->ID == ID)
            {
                p = Syllabus.erase(p);
                break;
            }
            else
                ++p;
        }
        cout << "成功删除该课程\n";
    }
    else
    {
        cout << "你还没添加该课程\n";
    }
}
void Student::Show() //展示一个学生的全部已选课信息
{
    if (Syllabus.size() == 0)
    {
        cout << "" << endl;
        return;
    }
    for (size_t i = 0; i < Syllabus.size(); i++)
        cout << "   #" << i << "课程编号:" << Syllabus[i].ID << " 名称:" << Syllabus[i].name << endl;
}
Student& StudentSearch(long ID)  //ID查找学生,没有拋回,同上面,不太懂的可以先用函数判断此学生是否已经添加到数组
{
    for (size_t i = 0; i < student.size(); i++)
        if (student[i].ID == ID)
            return student[i];
    throw 0;
}
void StudentShow()  //展示所有学生的选课信息
{
    for (size_t i = 0; i < student.size(); i++)
    {
        cout << "#" << i + 1 << ": ID:" << setw(3) << setfill('0') << student[i].ID << " 名字:" << student[i].name << endl
            << "此学生的选课信息:\n";
        student[i].Show();
    }
}
void AddCourse()    //老师添加课程
{
    while (1)
    {
        Course Tcourse;
        cout << "请输入课程ID(0退出):";
        EnterNum(Tcourse.ID);
        if (!Tcourse.ID)
            return;
        if (isCourseHave(Tcourse.ID))
        {
            cout << "该课程已添加,请重新输入或者输入0退出\n";
            continue;
        }
        cout << "请输入你要输入添加课程的名称:";
        cin.get();                                       //cin之后使用getline,记得先使用cin.get(),不然getline会读取输入流保留回车,相当于你直接在getline输入回车
        getline(cin, Tcourse.name);
        cout << "确认定添加课程" << Tcourse.name << " ID:" << Tcourse.ID << "?(y or n)";
        char verify = 'n';
        while (1)
        {
            cin >> verify;
            if (verify == 'y' || verify == 'Y')
            {
                course.push_back(Tcourse);
                cout << "成功添加该课程\n";
                break;
            }
            else if (verify == 'n' || verify == 'N')
                break;
            cout << "请输入y或n\n";
        }
        cout << "输入1继续添加\n";
        int c;
        EnterNum(c);
        if (c != 1)
            return;
    }
}
void DeletetCurse()    //老师删除课程
{
    cout << "请输入课程ID:";
    long ID;
    EnterNum(ID);
    if (isCourseHave(ID))
    {
        for (auto p = course.begin(); p != course.end();)
        {
            if (p->ID == ID)
            {
                p = course.erase(p);
                break;
            }
            else
                ++p;
        }
        cout << "成功删除该课程\n";
    }
    else
    {
        cout << "你还没添加该课程\n";
    }
}
void AddStudnt()//老师添加学生
{
    while (1)
    {
        Student Tstudent;
        cout << "请输入学生ID(0退出)\n";
        EnterNum(Tstudent.ID);
        if (!Tstudent.ID)
            return;
        try {
            StudentSearch(Tstudent.ID);
            cout << "该学生已添加,请重新输入或者输入0退出\n";
            continue;
        }
        catch (...) {}
        cout << "请输入学生姓名\n";
        cin.get();
        getline(cin, Tstudent.name);
        cout << "确认定添加学生" << Tstudent.name << " ID:" << Tstudent.ID << "?(y or n)";
        char verify = 'n';
        while (1)
        {
            cin >> verify;
            if (verify == 'y' || verify == 'Y')
            {
                student.push_back(Tstudent);
                cout << "成功添加该学生\n";
                break;
            }
            else if (verify == 'n' || verify == 'N')
                break;
            cout << "请输入y或n\n";
        }
        cout << "输入1继续添加\n";
        int c;
        EnterNum(c);
        if (c != 1)
            return;
    }
}
void Open()    //打开文件
{
    ifstream Sin("student.txt");   //没有使用ios,因为涉及删除,所以我觉得使用最好是重新写入一遍学生的信息,就直接使用了最简单的方式
    ifstream Cin("course.txt");
    Student Tstudent;
    Course Tcourse;
    if ((!Sin) && (!Cin))
    {
        cerr << "所有数据读求失败,请重新尝试\n";
    }
    else if (!Sin)
    {
        cerr << "学生数据读求失败,请重新尝试\n";
    }
    else if (!Cin)
    {
        cerr << "课程数据读求失败,请重新尝试\n";
    }
    else
        cout << "读取成功\n";
    while (Cin>> Tcourse.name >> Tcourse.ID )
    {
        course.push_back(Tcourse);
    }
    while (Sin >> Tstudent.name>> Tstudent.ID)//我把学生信息和选课信息放一起了,格式是学生名字 学生ID 课程ID 课程ID。。。。。
    {                                          //所以读取起来有点麻烦,先读取学生姓名和ID(名字不能是数字)
        long CourseID;
        while (Sin >> CourseID)   //循环输入选课的ID,读到姓名哪里会退出循环,
        {            
            try {
                Tstudent.Syllabus.push_back(GetCourse(CourseID));
            }
            catch (...) {}
        }
        Sin.clear();        //清空输入流,之后才能正常读取姓名
        student.push_back(Tstudent);
        Tstudent.Syllabus.clear();   //清空临时数组,有兴趣可以试一试不清空会怎么样
    }
    Sin.close();
    Cin.close();
}
void Save()     //保存到文件
{
    ofstream Sout("student.txt");    //ofstream不用ios在打开文件时会清空文件的内容
    ofstream Cout("course.txt");
    for (size_t i = 0; i < student.size(); i++)
    {
        Sout << student[i].name << " " << student[i].ID;
        for (size_t k = 0; k < student[i].Syllabus.size(); k++)
            Sout << " " << student[i].Syllabus[k].ID;
        Sout << endl;
    }
    for (size_t i = 0; i < course.size(); i++)
        Cout << course[i].name << " " << course[i].ID << endl;
    cout << "保存成功\n";
}
void TeacherAct()   //老师使用的系统
{
    cout << "1 查看学生信息 2更该学生信息 3添加课程 4删除课程 5添加学生 6查看全部课程(0退出)\n";
    int c;
    EnterNum(c);
    while (c)
    {
        if (c == 1)
        {
            cout << "1 查找学生 2 全部浏览 (0退出)\n";
            int c2;
            EnterNum(c2);
            if (c2 == 1)
            {
                cout << "请输入学生ID(0退出)\n";
                while (1)
                {
                    long ID;
                    EnterNum(ID);
                    if (ID == 0)
                        break;
                    try {
                        cout << "学生ID:" << setw(3) << setfill('0') << ID << " 姓名:" << StudentSearch(ID).name << endl
                            << "此学生的选课信息:\n";
                        StudentSearch(ID).Show();
                    }
                    catch (...) { cout << "没有该学生\n"; }
                    cout << "\n重新输入学生ID或者输入0退出\n";
                }
            }
            if (c2 == 2)
                StudentShow();
        }
        if (c == 2)
        {
            cout << "请输入学生ID(0退出)\n";
            while (1)
            {
                long ID;
                EnterNum(ID);
                if (ID == 0)
                    break;
                try {
                    StudentSearch(ID);                   //这相当于判断此ID的学生是否存在
                    cout << "1增添该学生课程 2删除该学生课程(0退出)\n";
                    int c2;
                    EnterNum(c2);
                    if (c2 == 1)
                        StudentSearch(ID).Acourse();
                    if (c2 == 2)
                        StudentSearch(ID).Dcourse();
                    if (!c2)
                        break;
                }
                catch (...) { cout << "没有该学生\n"; }
                cout << "\n重新输入学生ID或者输入0退出\n";
            }
        }
        if (c == 3)
            AddCourse();
        if (c == 4)
            DeletetCurse();
        if (c == 5)
            AddStudnt();
        if (c == 6)
            CourseShow();
        cout << "1 查看学生信息 2更该学生信息 3添加课程 4删除课程 5添加学生 6查看全部课程(0退出)\n";
        EnterNum(c);
    }
}
void StudentAct()   //学生的操作
{
    cout << "请输入ID:";
    long ID;
    EnterNum(ID);
    char verify = 'n';
    while (1)
    {
        try {
            StudentSearch(ID).name;
            cout << "请确认信息: 姓名:" << StudentSearch(ID).name << " ID:" << StudentSearch(ID).ID << "(y or n)\n";
            cin >> verify;
            if (verify == 'y' || verify == 'Y')
                break;
            else if (verify == 'n' || verify == 'N')
                return;
        }
        catch (...) { cout << "没有该学生\n"; };
        cout << "重新输入ID或者输入n退出\n";
    }
    Student &stu = StudentSearch(ID);
    cout << "1添加课程 2删除课程 3查看已选课程 4查看全部课程(0退出)\n";
    while (1)
    {
        int c;
        EnterNum(c);
        if (c == 1)
            stu.Acourse();
        if (c == 2)
            stu.Dcourse();
        if (c == 3)
            stu.Show();
        if (c == 4)
            CourseShow();
        if (c == 0)
            break;
        cout << "1添加课程 2删除课程 3查看已选课程 4查看全部课程(0退出)\n";
    }
}
int main()
{
    Open();
    cout << "1 教师; 2 学生\n";
    int c;
    EnterNum(c);
    if (c == 1)
        TeacherAct();
    if (c == 2)
        StudentAct();
    Save();
    system("pause");
}
2018-03-29 12:26
Jonny0201
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:13
帖 子:283
专家分:1247
注 册:2016-11-7
  得分:20 
程序代码:
template<typename T>
void EnterNum(T &i)   //个人习惯,用来输入数字还可以验错,就当cin>>看
{
    cin >> i;
    if (!cin)         //输入失败,比如输入了字母
    {
        cout << "错误输入,请输入数字:";
        cin.clear();            
        while (cin.get() != '\n');  
        cin >> i;
    }
}

全篇都是long 或者 int, 并没有出现其他的, 想问下这里用模板的意义

[此贴子已经被作者于2018-3-29 19:15编辑过]

2018-03-29 19:13







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053390 second(s), 8 queries.
Copyright©2004-2018, BCCN.NET, All Rights Reserved