学生管理系统(容器)

#include<iostream>
#include<vector>
#include<windows.h>
using namespace std;
int a=999;
int b=0;
bool c=false;
struct student{
    string name;
    int age;
    int card;
    int grade;
}s;
void ka(){
    cout<<"学生管理系统:"<<endl;
    cout<<"1.新建学生信息"<<endl;
    cout<<"2.查找学生信息"<<endl;
    cout<<"3.修改学生信息"<<endl;
    cout<<"4.删除学生信息"<<endl;
    cout<<"5.退出系统"<<endl;
}
vector<student> insert(vector<student> v){
    cout<<"请输入学生姓名:";
    cin>>s.name;
    cout<<"请输入学生年龄:";
    cin>>s.age;
    cout<<"请输入学生年级:";
    cin>>s.grade;
    cout<<"你的学号是:";
    a++;
    s.card=a;
    cout<<s.card;
    v.push_back(s);
    Sleep(500);
    return v;
}
vector<student> find(vector<student> v){
    system("cls");
    while(1){
        cout<<"你要用什么形式查找:"<<endl;
        cout<<"1.学生卡号"<<endl;
        cout<<"2.学生年龄"<<endl;
        cout<<"3.学生姓名"<<endl;
        cout<<"4.退出"<<endl;
        c=false;
        cin>>b;
        system("cls");
        if(b==4){
            system("cls");
            break;
        }
        switch(b){
            case 1:{
                cout<<"学生卡号:"<<endl;
                cin>>b;
                for(int i=0;i<v.size();i++){
                    if(v[i].card==b){
                            cout<<"学生姓名:";
                            cout<<v[i].name<<endl;
                            cout<<"学生年龄:";
                            cout<<v[i].age<<endl;
                            cout<<"学生年级:";
                            cout<<v[i].grade<<endl;
                            cout<<"----------"<<endl;
                        c=true;
                    }
                }
                if(c==false){
                    cout<<"抱歉,没找到"<<endl;
                    cout<<"----------"<<endl;
                }
                break;
            }
            case 2:{
                cout<<"学生年龄:"<<endl;
                cin>>b;
                for(int i=0;i<v.size();i++){
                    if(v[i].age==b){
                        cout<<"学生姓名:";
                        cout<<v[i].name<<endl;
                        cout<<"学生年龄:";
                        cout<<v[i].age<<endl;
                        cout<<"学生年级:";
                        cout<<v[i].grade<<endl;
                        cout<<"----------"<<endl;
                        c=true;
                    }
                }
                if(c==false){
                    cout<<"抱歉,没找到"<<endl;
                    cout<<"----------"<<endl;
                }
                break;
            }
            case 3:{
                cout<<"学生姓名:"<<endl;
                string d;
                cin>>d;
                for(int i=0;i<v.size();i++){
                    if(v[i].name.find(d)!=string::npos){
                        cout<<"学生姓名:";
                        cout<<v[i].name<<endl;
                        cout<<"学生年龄:";
                        cout<<v[i].age<<endl;
                        cout<<"学生年级:";
                        cout<<v[i].grade<<endl;
                        cout<<"----------"<<endl;
                        c=true;
                    }
                }
                if(c==false){
                    cout<<"抱歉,没找到"<<endl;
                    cout<<"----------"<<endl;
                }
                break;
            }
            default :{
                break;
            }
            }
    }
    return v;
}
vector<student> change(vector<student> v){
system("cls");
while(1){
    cout<<"1.输入学生卡号"<<endl;
    cout<<"2.退出"<<endl;
    cin>>b;
    system("cls");
    if(b==2){
        break;
    }
    c=false;
    int e=0;
    cout<<"学生卡号:"<<endl;
    cin>>b;
    for(int i=0;i<v.size();i++){
        if(v[i].card==b){
            c=true;
            e=i;
        }
    }
    while(1){
        if(c==false){
            cout<<"抱歉,没找到"<<endl;
            cout<<"----------"<<endl;
            break;
        }
        cout<<"你要修改什么:"<<endl;
        cout<<"1.学生姓名"<<endl;
        cout<<"2.学生年龄"<<endl;
        cout<<"3.学生年级"<<endl;
        cout<<"4.退出"<<endl;
        cin>>b;
        system("cls");
        if(b==4){
            system("cls");
            break;
        }
        switch(b){
            case 1:{
                cout<<"原学生姓名:"<<v[e].name<<endl<<"要修改的学生姓名:";
                cin>>v[e].name;
                break;
            }
            case 2:{
                cout<<"原学生年龄:"<<v[e].age<<endl<<"要修改的学生年龄:";
                cin>>v[e].age;
                break;
            }
            case 3:{
                cout<<"原学生年级:"<<v[e].grade<<endl<<"要修改的学生年级:";
                cin>>v[e].grade;
                break;
            }
            default:{
                break;
            }
        }
        cout<<"修改成功"<<endl;
        cout<<"----------"<<endl;
    }
}
    return v;
}
vector<student> kill(vector<student> v){
    system("cls");
    while(1){
        cout<<"删除学生信息:"<<endl;
        cout<<"1.输入学号"<<endl;
        cout<<"2.退出"<<endl;
        c=false;
        cin>>b;
        system("cls");
        if(b==2){
            system("cls");
            break;
        }
        cout<<"学号:"<<endl;
        cin>>b;
        for(int i=0;i<v.size();i++){
            if(v[i].card==b){
                c=true;
                v[i].card=0;
                v.erase(v.begin(),v.begin()+v.size());
                cout<<"删除成功"<<endl;
                cout<<"----------"<<endl;
            }
        }
        if(c==false){
            cout<<"抱歉,没找到"<<endl;
            cout<<"----------"<<endl;
        }
    } 
    return v;
}
int main(){
    vector<student> v;
    while(1){
        ka();
        int n;
        cin>>n;
        if(n==5){
        	cout<<"感谢使用!"<<endl;
            break;
        }
        switch(n){
            case 1:{
                v=insert(v);
                break;
            }
            case 2:{
                find(v);
                break;
            }
            case 3:{
                v=change(v);
                break;
            }
            case 4:{
                v=kill(v);
                break;
            }
            default:{
            break;
            }
        }
        system("cls");
    }
    return 0;
}

  

posted @ 2023-12-16 09:53  陈若麟  阅读(14)  评论(0编辑  收藏  举报