基于c++中vector实现的简易学生管理系统

第一次用c++写程序,感觉还是有一点像c的写法,只是用了c++中的函数,参数。

(更新了查找,查找和排序一样,可以按照年龄和分数执行)

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <map>
using namespace std;
class Info{
public:
    static int id;
    string name;
    int age;
    int score;
    Info(){}
    ~Info(){}
};
int Info::id=0;
void showmenu(){
    cout<<"1.add 2.del 3.sort 4.show 5.modify 6.find 7.quit"<<endl;
}
void AddInfo(vector<Info>& stu){
    Info stu_temp;
    cout<<"please input name:";
    cin>>stu_temp.name;
    cout<<"please input age:";
    cin>>stu_temp.age;
    cout<<"please input score:";
    cin>>stu_temp.score;
    stu.push_back(stu_temp);
    stu_temp.id++;
}
void ShowInfo(vector<Info>& stu){
    if(!int(stu.size())){
        cout<<"none info"<<endl;
        return;
    }
    cout<<endl;
    int i=0;
    for(Info stu_temp:stu){
        cout<<i<<" "<<stu_temp.name<<" "<<stu_temp.age<<" "<<stu_temp.score<<endl;
        i++;
    }
    cout<<endl;
}
void DelInfo(vector<Info>& stu){
    if(!int(stu.size())){
        cout<<"none info"<<endl;
        return;
    }
    ShowInfo(stu);
    cout<<"please input id:";
    int id;
    cin>>id;
    if(id<0||id>int(stu.size())){
        cout<<"input error"<<endl;
        return;
    }
    vector<Info>::iterator it=stu.begin();
    int i=0;
    while(i<id){
        i++;
        it++;
    }
    stu.erase(it);
    stu.data()->id--;
}
void sortInfo(vector<Info>& stu){
    cout<<"please input num(1 for age,2 for score):";
    int flag_1;
    cin>>flag_1;
    if(flag_1!=1&&flag_1!=2){
        cout<<"input error"<<endl;
        return;
    }
    int flag_2;
    cout<<"please input num(1 for up,2 for down):";
    cin>>flag_2;
    if(flag_2!=1&&flag_2!=2){
        cout<<"input error"<<endl;
        return;
    }
    if(flag_1==1){
        if(flag_2==1){
            sort(stu.begin(),stu.end(),[](Info stu1,Info stu2)->bool{return stu1.age<stu2.age;});
        }else{
            sort(stu.begin(),stu.end(),[](Info stu1,Info stu2)->bool{return stu1.age>stu2.age;});
        }
    }else{
        if(flag_2==1){
            sort(stu.begin(),stu.end(),[](Info stu1,Info stu2)->bool{return stu1.score<stu2.score;});
        }else{
            sort(stu.begin(),stu.end(),[](Info stu1,Info stu2)->bool{return stu1.score>stu2.score;});
        }
    }
    ShowInfo(stu);
}
void ModInfo(vector<Info>& stu){
    cout<<"please input id(you want modify):";
    int id;
    cin>>id;
    if(id<0||id>int(stu.size())){
        cout<<"input error"<<endl;
        return;
    }
    int i=0;
    vector<Info>::iterator it=stu.begin();
    while(i<id){
        i++;
        it++;
    }
    Info stu_temp;
    cout<<"please input name:";
    cin>>stu_temp.name;
    cout<<"please input age:";
    cin>>stu_temp.age;
    cout<<"please input score:";
    cin>>stu_temp.score;
    it->name=stu_temp.name;
    it->age=stu_temp.age;
    it->score=stu_temp.score;
}
void FindInfo(vector<Info>& stu){
    if(!int(stu.size())){
        cout<<"none info"<<endl;
        return;
    }
    cout<<"please input num(1 for age,2 for name):";
    int num;
    cin>>num;
    if(num!=1&&num!=2){
        cout<<"input error"<<endl;
        return;
    }
    if(num==1){
        int age;
        cout<<"please input age:";
        cin>>age;
        using namespace placeholders;
        map<int,Info> m;
        for(Info stu_temp:stu){
            m[stu_temp.age]=stu_temp;
        }
        auto it=m.find(age);
        if(it!=m.end()){
            cout<<"have"<<endl;
        }else{
            cout<<"none peo"<<endl;
        }
    }else{
        string name;
        cout<<"please input name:";
        cin>>name;
        using namespace placeholders;
        map<string,Info> m1;
        for(Info stu_temp1:stu){
            m1[stu_temp1.name]=stu_temp1;
        }
        auto it=m1.find(name);
        if(it!=m1.end()){
            cout<<"have"<<endl;
        }else{
            cout<<"none peo"<<endl;
        }
    }
}
int getinput(vector<Info>& stu){
    cout<<"please input number:";
    int num;
    cin>>num;
    switch (num) {
    case 1: AddInfo(stu);
        break;
    case 2: DelInfo(stu);
        break;
    case 3: sortInfo(stu);
        break;
    case 4: ShowInfo(stu);
        break;
    case 5: ModInfo(stu);
        break;
    case 6: FindInfo(stu);
        break;
    case 7:
        throw out_of_range("quit");
    default:
        cout<<"input error"<<endl;
        break;
    }
    return 0;
}
int main()
{
    vector<Info> stu;
    while(1){
        showmenu();
        try {
            getinput(stu);
        } catch (out_of_range&(buf)) {
            return 0;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值