联系人管理系统C++实现

#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct Person{
    string name;
    int sex;//1男0女
    int age;
    string tel;
    string adder;
};

struct Books{
    vector<Person> pv;
    int size = 0;
};

void add_person(Books &books){
    while(true){
        struct Person p;
        cout<<"name"<<endl;
        cin>>p.name;
        cout<<"age"<<endl;
        cin>>p.age;
        cout<<"sex"<<endl;
        cin>>p.sex;
        cout<<"tel"<<endl;
        cin>>p.tel;
        cout<<"adder"<<endl;
        cin>>p.adder;
        books.pv.push_back(p);
        books.size += 1;
        cout<<"还需要输入么?  Y/N"<<endl;
        char states;
        cin>>states;
        if(states == 'N'){
            break;
        }
    }

}
void show_all_person(const Books &books){
    cout<<"一共"<<books.size<<"个人"<<endl;
    for(auto &it : books.pv){
        cout<<"name: "<<it.name<<endl;
        cout<<"sex: "<<it.sex<<endl;
        cout<<"age: "<<it.age<<endl;
        cout<<"tel: "<<it.tel<<endl;
        cout<<"adder: "<<it.adder<<endl;
    }
}
int have_person(const string &name,const Books &books){
    int index = -1;
    for(auto &it : books.pv){
        ++ index;
        if(it.name == name){
            break;
        }
    }
    return index;
}
void del_person(Books &books){
    string del_name;
    cout<<"请输入要删除的人的姓名:"<<endl;
    cin>>del_name;
    int index = have_person(del_name,books);
    if(index != -1){
        books.pv.erase(books.pv.begin() + index);
        books.size -= 1;
    } else{
        cout<<"查无此人"<<endl;
    }
}
void find_person(const Books &books){
    string find_name;
    cout<<"请输入要查找的联系人姓名";
    cin>>find_name;
    int index = have_person(find_name,books);
    if(index != -1 ){
        cout<<"name: "<<books.pv[index].name<<endl;
        cout<<"sex: "<<books.pv[index].sex<<endl;
        cout<<"age: "<<books.pv[index].age<<endl;
        cout<<"tel: "<<books.pv[index].tel<<endl;
        cout<<"adder: "<<books.pv[index].adder<<endl;
    }
}

void update_person(Books &books){
    string m_name;
    cout<<"请输入要修改的联系人姓名";
    cin>>m_name;
    int index = have_person(m_name,books);
    if(index != -1 ){
        cout<<"1、name: "<<books.pv[index].name<<endl;
        cout<<"2、sex: "<<books.pv[index].sex<<endl;
        cout<<"3、age: "<<books.pv[index].age<<endl;
        cout<<"4、tel: "<<books.pv[index].tel<<endl;
        cout<<"5、adder: "<<books.pv[index].adder<<endl;
        cout<<"请输入要修改的项目序号: "<<endl;
        int ii;
        cin>>ii;
        switch (ii) {
            case 1:
                cout<<"old name: "<<books.pv[index].name<<"input new name:"<<endl;
                cin>>books.pv[index].name;
                cout<<"已修改 new name is : "<<books.pv[index].name<<endl;
                break;
            case 2:
                cout<<"old sex: "<<books.pv[index].sex<<"input new sex:"<<endl;
                cin>>books.pv[index].sex;
                cout<<"已修改 new sex is : "<<books.pv[index].sex<<endl;
                break;
            case 3:
                cout<<"old age "<<books.pv[index].age<<"input new age:"<<endl;
                cin>>books.pv[index].age;
                cout<<"已修改 new age is : "<<books.pv[index].age<<endl;
                break;
            case 4:
                cout<<"old tel: "<<books.pv[index].tel<<"input new tel:"<<endl;
                cin>>books.pv[index].tel;
                cout<<"已修改 new name is : "<<books.pv[index].tel<<endl;
                break;
            case 5:
                cout<<"old adder: "<<books.pv[index].adder<<"input new adder:"<<endl;
                cin>>books.pv[index].adder;
                cout<<"已修改 new adder is : "<<books.pv[index].adder<<endl;
                break;
        }
    }else{
        cout<< "找不到"<<endl;
    }
}

void cle_person(Books &books){
    books.size = 0;
    books.pv.clear();
}

void show_menu(){
    cout<<"************************"<<endl;
    cout<<"*****1、添加***2、显示****"<<endl;
    cout<<"*****3、删除***4、查找****"<<endl;
    cout<<"*****5、修改***6、清空****"<<endl;
    cout<<"***********7、退出*******"<<endl;
    cout<<"请输入选择………………"<<endl;
}

int main(int argc, char **argv) {
    int select = 0;
    struct Books books;
    while(true){
        show_menu();
        cin >> select;
        switch (select){
            case 1:
                add_person(books);
                break;
            case 2:
                show_all_person(books);
                break;
            case 3:
                del_person(books);
                break;
            case 4:
                find_person(books);
                break;
            case 5:
                update_person(books);
                break;
            case 6:
                cle_person(books);
                break;
            case 7:
                cout<<"再见!!"<<endl;
                return 0;
        }

    }

}

 以上代码存在BUG  index的处理不好,因为负值也会使数组从右往左取值。第二点是没有错误检查。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值