c++学生信息管理完整代码

详细的c++学生信息管理代码解析
点击这里

#include
#include
#include
using namespace std;
struct StuInfo;
vector<StuInfo> s;
void AddInfo();
void LocateInfo();
void DisplayInfo();
void ModifyInfo();
void DeleteInfo();
void ExitSystem();
void home()
{
    system("cls");
    cout << "a) “1.学生信息增加”" << endl << "b) “2.学生信息显示” " << endl << "c) “3.学生信息查找” " << endl << "d) “4.学生信息修改”" << endl << "e) “5.学生信息删除”" << endl << "f) “0.退出”" << endl;
    char a;
    cin >> a;
    switch(a)
    {
    case 'a':
        AddInfo();
        break;
    case 'b':
        DisplayInfo();
        break;
    case 'c':
        LocateInfo();
        break;
    case 'd':
        ModifyInfo();
        break;
    case 'e':
        DeleteInfo();
        break;
    case 'f':
        ExitSystem();
        break;
    default:
        system("cls");
        cout << "ERROR\n";
        system("pause");
        home();
    }
loop:
    ;
}
struct StuInfo
{
    string name;
    long No;
    int score;
};
void AddInfo()
{
    system("cls");
    string title="学生息增加";
    cout << "*******************" << title << "*******************\n\n\n" << endl;
    int n;
    cout << "请输入录入信息人数:";
    cin >> n;
    cout << endl;
    StuInfo stu;
    for(int i=0; i<n; i++)
    {
        cout << "第" << i+1 << "名学生:\n";
        cout << "请输入姓名:";
        cin >> stu.name;
        cout << "\n请输入学号:";
        cin >> stu.No;
        cout << "\n请输入成绩:";
        cin >> stu.score;
        cout << endl;
        s.push_back(stu);
    }
    cout << "学生信息录入完成,按任意键退出。" << endl;
    system("pause");
    home();
}
void LocateInfo()
{
    system("cls");
    long num;
    string title="学生信息查找";
    cout << "*******************" << title << "*******************" << endl;
    cout << "\n\n\n请输入学生学号:" ;
    cin >> num;
    cout << endl;
    int flag=0;
    for(int i=0; i<s.size(); i++)
    {
        if(s[i].No==num)
        {
            flag=i+1;
            break;
        }
    }
    flag?cout << "姓名:" << s[flag-1].name << endl << "学号:" << s[flag-1].No << endl << "成绩:" << s[flag-1].score << endl << "查找完成,按任意键继续。":cout << "未曾找到,无法操作!" << endl;
    system("pause");
    home();
}
void DisplayInfo()
{
    system("cls");
    string title="学生信息显示";
    cout << "*******************" << title << "*******************" << endl;
    for(int i=0; i<s.size(); i++)
    {
        cout << "姓名:" << s[i].name << " 学号:" << s[i].No << " 成绩:"  << s[i].score << endl;
    }
    system("pause");
    home();
}
void ModifyInfo()
{
    system("cls");
    int flag=0;
    int num;
    string title="学生信息修改";
    cout << "*******************" << title << "*******************" << endl;
    cout << "\n\n\n请输入被修改学生的学号:";
    cin >> num;
    cout << endl;
    for(int i=0; i<s.size(); i++)
    {
        if(s[i].No==num)
        {
            flag=i+1;
            cout << "当前学生信息:\n学生姓名:" << s[i].name << "\n学号:" << s[i].No << "\n成绩:" << s[i].score << endl;
            break;
        }
    }
    if(!flag)
    {
        cout << "未曾找到,无法操作!" << endl;
    }
    else
    {
        StuInfo stu;
        cout << "请输入姓名:";
        cin >> stu.name;
        cout << "\n请输入学号:";
        cin >> stu.No;
        cout << "\n请输入成绩:";
        cin >> stu.score;
        cout << endl;
        s[flag-1]=stu;
        cout << "信息修改完成,学生信息:\n姓名:" << stu.name << " 学号:" << stu.No << " 成绩:" << stu.score << endl;
    }
    system("pause");
    home();
}
void DeleteInfo()
{
    system("cls");
    string title="学生信息删除";
    long num;
    cout << "*******************" << title << "*******************" << endl;
    cout << "\n\n\n请输入被删除学生学号:";
    cin >> num;
    cout << endl;
    for(vector<StuInfo>::iterator i=s.begin(); i<s.end(); i++)
    {
        if(i->No==num)
        {
            char c;
            cout << "姓名:" << i->name << " 学号:" << i->No << " 成绩:" << i->score << endl << "您确定删除信息吗?(Y/N):\n";
            cin >> c;
            if(c=='Y' || c=='y')
            {
                s.erase(i);
                cout << "信息成功删除,按任意键继续。\n";
                goto BR;
            }
            else if(c=='N' || c=='n')
            {
                goto BR;
            }
            else
            {
                cout << "输入错误,程序将退回主菜单。\n";
                goto BR;
            }
        }
    }
    cout << "未曾找到,无法操作!\n";
BR:
    system("pause");
    home();
}
void ExitSystem()
{
    exit(1);
}
int main()
{
    vector<StuInfo> s;
    home();
    return 0;
}

你可能感兴趣的:(c++学生信息管理完整代码)