课程设计 学生成绩管理系统

Author Avatar
2U Jun 15, 2019
  • Read this article on other devices

功能导图

思维导图


数据文件

  1. password.txt

  2. student.txt

  3. subject.txt


源文件

main.cpp

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <windows.h>
#include "data.h"
#include "student.h"
#include "subject.h"
#include "system.h"

int main()
{
    welcome();
    login();
    load();
    do showMenu();
    while (chooseFunc());
    save();
    system("pause");
    return 0;
}

头文件

data.h

#pragma once

using namespace std;

struct Sub
{
    string name;
    double weight;
};
map<int, Sub> sub; //存储所有科目信息

struct Stu
{
    string name, id;
    int claz;
    map<int, int> m;
    double gpa()
    {
        double suma = 0, sumb = 0;
        for (auto it = m.begin(); it != m.end(); it++)
        {
            suma += it->second * sub[it->first].weight;
            sumb += sub[it->first].weight;
        }
        return suma / sumb;
    }
    int sum()
    {
        int res = 0;
        for (auto it = m.begin(); it != m.end(); it++)
            res += it->second;
        return res;
    }
};
vector<Stu> stu; //存储所有学生信息
vector<Stu> temp; //存储某次查询得到的学生信息

student.h

#pragma once

using namespace std;

void stuInsert()
{
    string name, id; char c;
    int claz, score;
    map<int, int> m;
    cout << "请输入学生信息,按回车键开始" << endl;
    getchar();
    while ((c = getchar()) != '0')
    {
        system("cls");
        m.clear();
        cout << "姓名:"; cin >> name;
        cout << "学号:"; cin >> id;
        cout << "班级:"; cin >> claz;
        for (auto it = sub.begin(); it != sub.end(); it++)
        {
            cout << it->second.name << "成绩:"; cin >> score;
            m[it->first] = score;
        }
        stu.push_back(Stu{ name, id, claz, m });
        cout << endl << "按回车键继续输入,输入0返回根目录" << endl;
        getchar();
    }
}

void stuDelete()
{
menu:
    system("cls");
    cout << "请输入您需要进行的操作标号" << endl;
    cout << "------------------------------" << endl;
    cout << endl << "0\t返回根目录" << endl;
    cout << endl << "1\t按指定学生编号逐个删除" << endl;
    cout << endl << "2\t按指定班级编号批量删除" << endl << endl;
    cout << "------------------------------" << endl;
    string id; int claz, ope; cin >> ope;
    switch (ope)
    {
    case 0: break;
    case 1:
        system("cls");
        cout << "请输入需要删除信息的学生编号:";
        while (cin >> id && id != "0")
        {
            bool flag = false; //标记是否查询到对象
            for (int i = 0; i < (int)stu.size(); i++) //遍历vector,逐个检验学生是否与指定编号匹配
            {
                if (stu[i].id == id)
                {
                    system("cls");
                    flag = true; //查询到对象
                    cout << "请确认是否删除该学生的信息[Y/N]" << endl << endl;
                    cout << std::right << setw(15) << "姓名" << std::right << setw(15) << "学号" << std::right << setw(15) << "班级";
                    for (auto it = sub.begin(); it != sub.end(); it++)
                        cout << std::right << setw(15) << it->second.name;
                    cout << std::right << setw(15) << "总成绩" << std::right << setw(15) << "平均学分绩点";
                    cout << endl << endl;
                    cout << std::right << setw(15) << stu[i].name << std::right << setw(15) << stu[i].id << std::right << setw(15) << stu[i].claz;
                    for (auto it = sub.begin(); it != sub.end(); it++)
                    {
                        if (stu[i].m.count(it->first)) cout << std::right << setw(15) << stu[i].m[it->first];
                        else cout << std::right << setw(15) << "尚未录入";
                    }
                    cout << std::right << setw(15) << stu[i].sum() << std::right << setw(15) << stu[i].gpa();
                    cout << endl;
                    string ope; cin >> ope;
                    if (ope == "Y" || ope == "y")
                    {
                        stu.erase(stu.begin() + i);
                        cout << endl << "成功删除" << endl;
                    }
                    else cout << endl << "取消删除" << endl;
                    break;
                }
            }
            if (!flag) //未查询到对象
                cout << endl << "不存在该学生编号!" << endl;
            system("pause");
            system("cls");
            cout << "(输入0返回上级目录)" << endl << endl;
            cout << "请继续输入需要删除信息的学生编号:";
        }
        goto menu;
    case 2:
        system("cls");
        cout << "请输入需要删除信息的班级编号:";
        while (cin >> claz && claz)
        {
            bool flag = false; //标记是否查询到对象
            for (auto it = stu.begin(); it != stu.end();) //遍历vector,逐个检验学生是否属于指定班级
            {
                if (it->claz == claz)
                {
                    system("pause");
                    system("cls");
                    flag = true; //查询到对象
                    cout << "请确认是否删除该学生的信息[Y/N]" << endl << endl;
                    cout << std::right << setw(15) << "姓名" << std::right << setw(15) << "学号" << std::right << setw(15) << "班级";
                    for (auto iter = sub.begin(); iter != sub.end(); iter++)
                        cout << std::right << setw(15) << iter->second.name;
                    cout << std::right << setw(15) << "总成绩" << std::right << setw(15) << "平均学分绩点";
                    cout << endl << endl;
                    cout << std::right << setw(15) << it->name << std::right << setw(15) << it->id << std::right << setw(15) << it->claz;
                    for (auto iter = sub.begin(); iter != sub.end(); iter++)
                    {
                        if (it->m.count(iter->first)) cout << std::right << setw(15) << it->m[iter->first];
                        else cout << std::right << setw(15) << "尚未录入";
                    }
                    cout << std::right << setw(15) << it->sum() << std::right << setw(15) << it->gpa();
                    cout << endl;
                    string ope; cin >> ope;
                    if (ope == "Y" || ope == "y")
                    {
                        it = stu.erase(it); //迭代器stu.erase(it)指向被删除元素的下一个,利用其返回值避免当前迭代器失效
                        cout << endl << "成功删除" << endl;
                    }
                    else
                    {
                        cout << endl << "取消删除" << endl;
                        it++;
                    }
                }
                else it++;
            }
            if (!flag) //未查询到对象
                cout << endl << "不存在该班级编号!" << endl;
            system("pause");
            system("cls");
            cout << "(输入0返回上级目录)" << endl << endl;
            cout << "请继续输入需要删除信息的班级编号:";
        }
        goto menu;
    default:
        cout << "指令无效,请重新输入!" << endl;
        system("pause");
        goto menu;
    }
}

int suborder; //存储某次按指定科目排序的科目次序

//以下为各种条件下的排序规则函数
bool cmp_nameAsc(const Stu &a, const Stu &b) { return a.name != b.name ? a.name < b.name : a.id < b.id; }
bool cmp_nameDes(const Stu &a, const Stu &b) { return a.name != b.name ? a.name > b.name : a.id < b.id; }
bool cmp_idAsc(const Stu &a, const Stu &b) { return a.id < b.id; }
bool cmp_idDes(const Stu &a, const Stu &b) { return a.id > b.id; }
bool cmp_sumAsc(Stu &a, Stu &b) { return a.sum() != b.sum() ? a.sum() < b.sum() : a.id < b.id; }
bool cmp_sumDes(Stu &a, Stu &b) { return a.sum() != b.sum() ? a.sum() > b.sum() : a.id < b.id; }
bool cmp_gpaAsc(Stu &a, Stu &b) { return a.gpa() != b.gpa() ? a.gpa() < b.gpa() : a.gpa() < b.gpa(); }
bool cmp_gpaDes(Stu &a, Stu &b) { return a.gpa() != b.gpa() ? a.gpa() > b.gpa() : a.gpa() < b.gpa(); }
bool cmp_subAsc(Stu &a, Stu &b)
{
    int i = 0, sid;
    for (auto it = sub.begin(); it != sub.end() && i < suborder; it++, i++)
        sid = it->first;
    if (a.m.count(sid) && b.m.count(sid)) return a.m[sid] != b.m[sid] ? a.m[sid] < b.m[sid] : a.id < b.id;
    return a.m.count(sid) ? false : b.m.count(sid) ? true : a.id < b.id;
}
bool cmp_subDes(Stu &a, Stu &b)
{
    int i = 0, sid;
    for (auto it = sub.begin(); it != sub.end() && i < suborder; it++, i++)
        sid = it->first;
    if (a.m.count(sid) && b.m.count(sid)) return a.m[sid] != b.m[sid] ? a.m[sid] > b.m[sid] : a.id < b.id;
    return a.m.count(sid) ? true : b.m.count(sid) ? false : a.id < b.id;
}

void print() //打印某次查询得到的学生信息
{
    cout << std::right << setw(15) << "姓名" << std::right << setw(15) << "学号" << std::right << setw(15) << "班级";
    for (auto it = sub.begin(); it != sub.end(); it++)
        cout << std::right << setw(15) << it->second.name;
    cout << std::right << setw(15) << "总成绩" << std::right << setw(15) << "平均学分绩点";
    cout << endl << endl;
    for (int i = 0; i < (int)temp.size(); i++)
    {
        cout << std::right << setw(15) << temp[i].name << std::right << setw(15) << temp[i].id << std::right << setw(15) << temp[i].claz;
        for (auto it = sub.begin(); it != sub.end(); it++)
        {
            if (temp[i].m.count(it->first)) cout << std::right << setw(15) << temp[i].m[it->first];
            else cout << std::right << setw(15) << "尚未录入";
        }
        cout << std::right << setw(15) << temp[i].sum() << std::right << setw(15) << temp[i].gpa() << endl << endl;
    }
}

void stuFind()
{
menu1:
    system("cls");
    cout << "请输入您需要进行的操作标号" << endl;
    cout << "------------------------------" << endl;
    cout << endl << "0\t返回根目录" << endl << endl;
    cout << "1\t按指定学生姓名查询" << endl << endl;
    cout << "2\t按指定学生编号查询" << endl << endl;
    cout << "3\t按指定班级编号查询" << endl << endl;
    cout << "------------------------------" << endl;
    string name, id; int claz, ope; cin >> ope;
    switch (ope)
    {
    case 0: break;
    case 1:
        system("cls");
        cout << "请输入学生姓名:";
        while (cin >> name && name != "0")
        {
            temp.clear();
            bool flag = false; //标记是否查询到对象
            for (int i = 0; i < (int)stu.size(); i++)
            {
                if (stu[i].name == name)
                {
                    flag = true; //查询到对象
                    temp.push_back(stu[i]);
                }
            }
            if (!flag) //未查询到对象
                cout << endl << "不存在该学生姓名!" << endl;
            else
            {
                system("cls");
                print();
            }
            system("pause");
            system("cls");
            cout << "(输入0返回上级目录)" << endl << endl;
            cout << "请继续输入需要查询的学生姓名:";
        }
        goto menu1;
    case 2:
        system("cls");
        cout << "请输入学生编号:";
        while (cin >> id && id != "0")
        {
            bool flag = false; //标记是否查询到对象
            for (int i = 0; i < (int)stu.size(); i++)
            {
                if (stu[i].id == id)
                {
                    system("cls");
                    flag = true; //查询到对象
                    cout << std::right << setw(15) << "姓名" << std::right << setw(15) << "学号" << std::right << setw(15) << "班级";
                    for (auto it = sub.begin(); it != sub.end(); it++)
                        cout << std::right << setw(15) << it->second.name;
                    cout << std::right << setw(15) << "总成绩" << std::right << setw(15) << "平均学分绩点";
                    cout << endl << endl;
                    cout << std::right << setw(15) << stu[i].name << std::right << setw(15) << stu[i].id << std::right << setw(15) << stu[i].claz;
                    for (auto it = sub.begin(); it != sub.end(); it++)
                    {
                        if (stu[i].m.count(it->first)) cout << std::right << setw(15) << stu[i].m[it->first];
                        else cout << std::right << setw(15) << "尚未录入";
                    }
                    cout << std::right << setw(15) << stu[i].sum() << std::right << setw(15) << stu[i].gpa();
                    cout << endl;
                }
            }
            if (!flag) //未查询到对象
                cout << endl << "不存在该学生编号!" << endl;
            system("pause");
            system("cls");
            cout << "(输入0返回上级目录)" << endl << endl;
            cout << "请继续输入需要查询的学生编号:";
        }
        goto menu1;
    case 3:
        system("cls");
        cout << "请输入班级编号:";
        while (cin >> claz && claz)
        {
            temp.clear();
            bool flag = false; //标记是否查询到对象
            for (int i = 0; i < (int)stu.size(); i++)
            {
                if (stu[i].claz == claz)
                {
                    flag = true; //查询到对象
                    temp.push_back(stu[i]);
                }
            }
            if (!flag) //未查询到对象
            {
                cout << endl << "不存在该班级编号!" << endl;
                system("pause");
                system("cls");
                cout << "(输入0返回上级目录)" << endl << endl;
                cout << "请继续输入需要查询的班级编号:";
                continue;
            }
        menu2:
            system("cls");
            cout << "请输入您需要进行的操作标号" << endl;
            cout << "--------------------------------------------------" << endl;
            cout << endl << setw(5) << 0 << " 重新输入班级编号" << endl << endl;
            cout << setw(5) << 1 << " 按学生姓名字典升序" << endl << endl;
            cout << setw(5) << 2 << " 按学生姓名字典降序" << endl << endl;
            cout << setw(5) << 3 << " 按学生编号升序" << endl << endl;
            cout << setw(5) << 4 << " 按学生编号降序" << endl << endl;
            cout << setw(5) << 5 << " 按总成绩升序" << endl << endl;
            cout << setw(5) << 6 << " 按总成绩降序" << endl << endl;
            cout << setw(5) << 7 << " 按平均学分绩点升序" << endl << endl;
            cout << setw(5) << 8 << " 按平均学分绩点降序" << endl << endl;
            int order = 9;
            for (auto it = sub.begin(); it != sub.end(); it++, order += 2)
            {
                cout << setw(5) << order << " 按" << it->second.name << "成绩升序" << endl << endl;
                cout << setw(5) << order + 1 << " 按" << it->second.name << "成绩降序" << endl << endl;
            }
            cout << "--------------------------------------------------" << endl;
            int ope; cin >> ope;
            switch (ope)
            {
            case 0: break;
            case 1: sort(temp.begin(), temp.end(), cmp_nameAsc); system("cls"); print(); system("pause"); goto menu2;
            case 2: sort(temp.begin(), temp.end(), cmp_nameDes); system("cls"); print(); system("pause"); goto menu2;
            case 3: sort(temp.begin(), temp.end(), cmp_idAsc); system("cls"); print(); system("pause"); goto menu2;
            case 4: sort(temp.begin(), temp.end(), cmp_idDes); system("cls"); print(); system("pause"); goto menu2;
            case 5: sort(temp.begin(), temp.end(), cmp_sumAsc); system("cls"); print(); system("pause"); goto menu2;
            case 6: sort(temp.begin(), temp.end(), cmp_sumDes); system("cls"); print(); system("pause"); goto menu2;
            case 7: sort(temp.begin(), temp.end(), cmp_gpaAsc); system("cls"); print(); system("pause"); goto menu2;
            case 8: sort(temp.begin(), temp.end(), cmp_gpaDes); system("cls"); print(); system("pause"); goto menu2;
            default:
                if (ope >= 9 && ope <= (int)sub.size() * 2 + 7)
                {
                    if (ope & 1) { suborder = (ope - 9) / 2 + 1; sort(temp.begin(), temp.end(), cmp_subAsc); }
                    else { suborder = (ope - 10) / 2 + 1; sort(temp.begin(), temp.end(), cmp_subDes); }
                    system("cls"); print();
                }
                else cout << "指令无效,请重新输入" << endl; system("pause"); goto menu2;
            }
            system("cls");
            cout << "(输入0返回上级目录)" << endl << endl;
            cout << "请继续输入需要查询的班级编号:";
        }
        goto menu1;
    default:
        cout << "指令无效,请重新输入" << endl;
        system("pause");
        goto menu1;
    }
}

void stuModify()
{
menu:
    system("cls");
    cout << "请输入您需要进行的操作标号" << endl;
    cout << "-------------------------" << endl;
    cout << endl << "0\t返回根目录" << endl << endl;
    cout << "1\t修改指定学生编号" << endl << endl;
    cout << "2\t修改指定班级编号" << endl << endl;
    cout << "-------------------------" << endl;
    string id; int ope, score; cin >> ope;
    switch (ope)
    {
    case 0: break;
    case 1:
        system("cls");
        cout << "请输入需要修改的学生编号:";
        while (cin >> id && id != "0")
        {
            bool flag = false; //标记是否查询到对象
            for (int i = 0; i < (int)stu.size(); i++)
            {
                if (stu[i].id == id)
                {
                    system("cls");
                    flag = true; //查询到对象
                    cout << "该学生的当前信息如下:" << endl << endl;
                    cout << std::right << setw(15) << "姓名" << std::right << setw(15) << "学号" << std::right << setw(15) << "班级";
                    for (auto it = sub.begin(); it != sub.end(); it++)
                        cout << std::right << setw(15) << it->second.name;
                    cout << std::right << setw(15) << "总成绩" << std::right << setw(15) << "平均学分绩点";
                    cout << endl << endl;
                    cout << std::right << setw(15) << stu[i].name << std::right << setw(15) << stu[i].id << std::right << setw(15) << stu[i].claz;
                    for (auto it = sub.begin(); it != sub.end(); it++)
                    {
                        if (stu[i].m.count(it->first)) cout << std::right << setw(15) << stu[i].m[it->first];
                        else cout << std::right << setw(15) << "尚未录入";
                    }
                    cout << std::right << setw(15) << stu[i].sum() << std::right << setw(15) << stu[i].gpa();
                    cout << endl << endl << "请输入修改后的学生信息:" << endl << endl;
                    string name; int claz;
                    cout << "姓名:"; cin >> name; stu[i].name = name;
                    cout << "学号:"; cin >> id; stu[i].id = id;
                    cout << "班级:"; cin >> claz; stu[i].claz = claz;
                    for (auto it = sub.begin(); it != sub.end(); it++)
                    {
                        cout << it->second.name << "成绩:"; cin >> score;
                        stu[i].m[it->first] = score;
                    }
                    cout << endl << "成功修改" << endl;
                    break;
                }
            }
            if (!flag) //未查询到对象
                cout << endl << "不存在该学生编号!" << endl;
            system("pause");
            system("cls");
            cout << "(输入0返回上级目录)" << endl << endl;
            cout << "请继续输入需要修改的学生编号:";
        }
        goto menu;
    case 2:
        int claz, newnum;
        system("cls");
        cout << "请输入需要修改的班级编号:";
        while (cin >> claz && claz)
        {
            bool flag = false; //标记是否查询到对象
            for (int i = 0; i < (int)stu.size(); i++)
            {
                if (stu[i].claz == claz)
                {
                    flag = true; //查询到对象
                    cout << "请输入修改后的班级编号:";
                    cin >> newnum;
                    stu[i].claz = newnum;
                    cout << endl << "成功修改" << endl;
                }
            }
            if (!flag) //未查询到对象
                cout << endl << "不存在该班级编号!" << endl;
            system("pause");
            system("cls");
            cout << "(输入0返回上级目录)" << endl << endl;
            cout << "请继续输入需要修改的班级编号:";
        }
        goto menu;
    default:
        cout << "指令无效,请重新输入" << endl;
        system("pause");
        goto menu;
    }
}

subject.h

#pragma once

void subInsert()
{
    string name;
    int sid;
    double weight;
    system("cls");
    cout << "请输入新科目名称:";
    cin >> name;
    bool flag = false; //标记是否输入未重复编号
    while (!flag)
    {
        flag = true;
        cout << "请输入新科目编号:";
        cin >> sid;
        for (auto it = sub.begin(); it != sub.end(); it++)
        {
            if (it->first == sid)
            {
                flag = false;
                cout << "该编号已存在!" << endl << endl;
                break;
            }
        }
    }
    cout << "请输入新科目学分:";
    cin >> weight;
    sub[sid].name = name;
    sub[sid].weight = weight;
    cout << endl << "成功增添" << endl;
    system("pause");
}

void subDelete()
{
menu:
    system("cls");
    cout << "请输入您需要进行的操作标号" << endl;
    cout << "------------------------------" << endl;
    cout << endl << "0\t返回根目录" << endl << endl;
    cout << "1\t按指定编号删除学科" << endl << endl;
    cout << "2\t按指定名称删除学科" << endl << endl;
    cout << "------------------------------" << endl;
    string name; int ope, sid; cin >> ope;
    bool flag = false;
    switch (ope)
    {
    case 0: break;
    case 1:
        system("cls");
        cout << "请输入需要删除信息的学科编号:";
        cin >> sid;
        flag = false; //标记是否查询到对象
        for (auto it = sub.begin(); it != sub.end(); it++) //遍历map,逐个检验学科是否与指定编号匹配
        {
            if (it->first == sid)
            {
                flag = true; //查询到对象
                sub.erase(it);
                for (int i = 0; i < (int)stu.size(); i++)
                {
                    if (stu[i].m.count(sid))
                        stu[i].m.erase(sid);
                }
                cout << endl << "成功删除" << endl;
                break;
            }
        }
        if (!flag) //未查询到对象
            cout << endl << "不存在该学科编号!" << endl;
        system("pause");
        system("cls");
        goto menu;
    case 2:
        system("cls");
        cout << "请输入需要删除信息的学科名称:";
        cin >> name;
        flag = false; //标记是否查询到对象
        for (auto it = sub.begin(); it != sub.end(); it++) //遍历map,逐个检验学科是否与指定名称匹配
        {
            if (it->second.name == name)
            {
                flag = true; //查询到对象
                for (int i = 0; i < (int)stu.size(); i++)
                {
                    if (stu[i].m.count(it->first))
                        stu[i].m.erase(it->first);
                }
                sub.erase(it);
                cout << endl << "成功删除" << endl;
                break;
            }
        }
        if (!flag) //未查询到对象
            cout << endl << "不存在该学科名称!" << endl;
        system("pause");
        system("cls");
        goto menu;
    default:
        cout << "指令无效,请重新输入!" << endl;
        system("pause");
        goto menu;
    }
}

void subModify()
{
menu:
    system("cls");
    cout << "请输入您需要进行的操作标号" << endl;
    cout << "------------------------------" << endl;
    cout << endl << "0\t返回根目录" << endl << endl;
    cout << "1\t按指定编号修改学科" << endl << endl;
    cout << "2\t按指定名称修改学科" << endl << endl;
    cout << "------------------------------" << endl;
    string name; int ope, sid; cin >> ope;
    double weight;
    bool flag = false;
    switch (ope)
    {
    case 0: break;
    case 1:
        system("cls");
        cout << "请输入需要修改信息的学科编号:";
        cin >> sid;
        flag = false; //标记是否查询到对象
        for (auto it = sub.begin(); it != sub.end(); it++) //遍历map,逐个检验学科是否与指定编号匹配
        {
            if (it->first == sid)
            {
                flag = true; //查询到对象
                bool flagg = false; //标记是否输入未重复编号
                while (!flagg)
                {
                    flagg = true;
                    cout << "请输入修改后的科目编号:";
                    cin >> sid;
                    for (auto it = sub.begin(); it != sub.end(); it++)
                    {
                        if (it->first == sid)
                        {
                            flagg = false;
                            cout << "该编号已存在!" << endl << endl;
                            break;
                        }
                    }
                }
                for (int i = 0; i < (int)stu.size(); i++)
                {
                    if (stu[i].m.count(it->first))
                    {
                        stu[i].m[sid] = stu[i].m[it->first];
                        stu[i].m.erase(it->first);
                    }
                }
                cout << "请输入修改后的科目名称:";
                cin >> name;
                cout << "请输入修改后的科目学分:";
                cin >> weight;
                sub[sid] = Sub{ name, weight };
                sub.erase(it);
                cout << endl << "成功修改" << endl;
                break;
            }
        }
        if (!flag) //未查询到对象
            cout << endl << "不存在该学科编号!" << endl;
        system("pause");
        system("cls");
        goto menu;
    case 2:
        system("cls");
        cout << "请输入需要修改信息的学科名称:";
        cin >> name;
        flag = false; //标记是否查询到对象
        for (auto it = sub.begin(); it != sub.end(); it++) //遍历map,逐个检验学科是否与指定名称匹配
        {
            if (it->second.name == name)
            {
                flag = true; //查询到对象
                bool flagg = false; //标记是否输入未重复编号
                while (!flagg)
                {
                    flagg = true;
                    cout << "请输入修改后的科目编号:";
                    cin >> sid;
                    for (auto it = sub.begin(); it != sub.end(); it++)
                    {
                        if (it->first == sid)
                        {
                            flagg = false;
                            cout << "该编号已存在!" << endl << endl;
                            break;
                        }
                    }
                }
                for (int i = 0; i < (int)stu.size(); i++)
                {
                    if (stu[i].m.count(it->first))
                    {
                        stu[i].m[sid] = stu[i].m[it->first];
                        stu[i].m.erase(it->first);
                    }
                }
                cout << "请输入修改后的科目名称:";
                cin >> name;
                cout << "请输入修改后的科目学分:";
                cin >> weight;
                sub[sid] = Sub{ name, weight };
                sub.erase(it);
                cout << endl << "成功修改" << endl;
                break;
            }
        }
        if (!flag) //未查询到对象
            cout << endl << "不存在该学科名称!" << endl;
        system("pause");
        system("cls");
        goto menu;
    default:
        cout << "指令无效,请重新输入!" << endl;
        system("pause");
        goto menu;
    }
}

void subDisplay() //显示各项学科信息
{
    system("cls");
    cout << std::right << setw(15) << "科目编号" << std::right << setw(15) << "科目名称" << std::right << setw(15) << "科目学分" << endl << endl;
    for (auto it = sub.begin(); it != sub.end(); it++)
        cout << std::right << setw(15) << it->first << std::right << setw(15) << it->second.name << std::right << setw(15) << it->second.weight << endl << endl;
    system("pause");
}

system.h

#pragma once

using namespace std;

void welcome()
{
    system("color 3f");
    cout << "--------------------------" << endl;
    cout << "欢迎使用学生信息管理系统!" << endl;
    cout << "--------------------------" << endl;
    cout << "正在加载,请稍后" << endl << endl;
    for (int i = 0; i < 20; i++)
    {
        Sleep(100);
        cout << "■";
    }
    cout << endl;
}

void login() //实现登录功能
{
    system("pause");
    system("cls");
    ifstream file;
    file.open("./src/password.txt");
    if (!file)
    {
        cout << "数据异常,无法访问!" << endl;
        system("pause");
        exit(0);
    }
    string pass, input;
    file >> pass;
    file.close();
    cout << "请输入密码:";
    while (cin >> input && pass != input) //检验输入串与密码是否匹配
    {
        if (input == "0") //输入指令0,取消登录操作
        {
            system("pause");
            exit(0);
        }
        system("cls");
        cout << "(输入0退出系统)" << endl << endl;
        cout << "密码错误,请重试:";
    }
    cout << endl << "登录成功!" << endl;
    system("pause");
}

void load() //从文件中读取全部学生信息和科目信息
{
    ifstream file;
    file.open("./src/subject.txt");
    if (!file)
    {
        cout << "数据异常,无法访问!" << endl;
        system("pause");
        exit(0);
    }
    int sid;
    while (file >> sid)
        file >> sub[sid].name >> sub[sid].weight;
    file.close();
    file.open("./src/student.txt");
    if (!file)
    {
        cout << "数据异常,无法访问!" << endl;
        system("pause");
        exit(0);
    }
    string name, id;
    int claz, score, nsub;
    map<int, int> m;
    while (file >> name >> id >> claz >> nsub)
    {
        m.clear();
        for (int i = 0; i < nsub; i++)
        {
            file >> sid >> score;
            m[sid] = score;
        }
        stu.push_back(Stu{ name, id, claz, m });
    }
}

void save() //程序运行结束前,将学生信息和科目信息写入文件
{
    ofstream student("./src/student.txt");
    for (int i = 0; i < (int)stu.size(); i++)
    {
        student << stu[i].name << " " << stu[i].id << " " << stu[i].claz << " " << (int)stu[i].m.size();
        for (auto it = stu[i].m.begin(); it != stu[i].m.end(); it++)
            student << " " << it->first << " " << it->second;
        student << endl;
    }
    student.close();
    ofstream subject("./src/subject.txt");
    for (auto it = sub.begin(); it != sub.end(); it++)
        subject << it->first << " " << it->second.name << " " << it->second.weight << endl;
}

void showMenu()
{
    system("cls");
    cout << "请输入您需要进行的操作标号" << endl;
    cout << "---------------------" << endl;
    cout << endl << "0\t退出系统" << endl << endl;
    cout << "1\t修改密码" << endl << endl;
    cout << "2\t增添学生信息" << endl << endl;
    cout << "3\t删除学生信息" << endl << endl;
    cout << "4\t修改学生信息" << endl << endl;
    cout << "5\t查询学生信息" << endl << endl;
    cout << "6\t增添科目信息" << endl << endl;
    cout << "7\t删除科目信息" << endl << endl;
    cout << "8\t修改科目信息" << endl << endl;
    cout << "9\t显示科目信息" << endl << endl;
    cout << "---------------------" << endl;
}

void changePass()
{
    system("cls");
    cout << "请输入3-10位新密码:" << endl;
    string pass;
    while (cin >> pass && (pass.length() < 3 || pass.length() > 10)) //检验新密码长度是否符合要求
    {
        system("cls");
        cout << "密码不符合要求,请重新输入:" << endl;
    }
    ofstream file("./src/password.txt");
    file << pass;
    file.close();
    system("cls");
    cout << "密码修改成功,新密码为:" << pass << endl;
    system("pause");
}

bool chooseFunc()
{
    int ope; cin >> ope;
    switch (ope)
    {
    case 0: return false;
    case 1: changePass(); break;
    case 2: stuInsert(); break;
    case 3: stuDelete(); break;
    case 4: stuModify(); break;
    case 5: stuFind(); break;
    case 6: subInsert(); break;
    case 7: subDelete(); break;
    case 8: subModify(); break;
    case 9: subDisplay(); break;
    default: cout << "指令无效,请重新输入!" << endl; system("pause");
    }
    return true;
}

This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
Link to this article: https://www.singularity2u.top/2019/06/15/课程设计-学生成绩管理系统/