#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的处理不好,因为负值也会使数组从右往左取值。第二点是没有错误检查。