结构体1,知识点(结构体),存放人员详情,名字,性别,年龄等
struct person
{
string m_name;
int m_sex;
int m_age;
string m_phone;
string m_addr;
};
结构体2,知识点 (结构体数组),存放人员数量和人员数组
struct addressbooks
{
struct person personarray[MAX];
int m_size;
};
上面两个结构体存放我们的数据,我们基于这两个结构体操作
菜单界面,用于输入提示
void showMenu()
{
cout << "*****1.添加联系人*****" << endl;
cout << "*****2.显示联系人*****" << endl;
cout << "*****3.删除联系人*****" << endl;
cout << "*****4.查找联系人*****" << endl;
cout << "*****5.修改联系人*****" << endl;
cout << "*****6.清空联系人*****" << endl;
cout << "*****7.退出通讯录*****" << endl;
}
主函数循环测试输入并跳转函数
知识点,cin输入,switch选择
增删改查之添加
判断人数是否满,满了直接退出
人数没有满通过结构体2抽象出人员表进行添加操作
知识点->结构体指针访问 .结构体访问
a->b[人员数量].具体参数
完了之后人员数要+1防止数据覆盖
void addperson(addressbooks *abs)
{
if(abs->m_size==MAX)
{
cout <<"通讯录已满,不能再添加!" <<endl;
return;
}
else
{
string name;
cout << "请输入姓名"<< endl;
cin >>name;
abs->personarray[abs->m_size].m_name=name;
int sex=0;
cout << "请输入性别"<< endl;
cout << "输入1为男"<< endl;
cout << "输入2为女"<< endl;
while (true)
{
cin >> sex;
if(sex==1||sex==2)
{
abs->personarray[abs->m_size].m_sex=sex;
break;
}
else
{
cout <<"输入错误,请重新输入" << endl;
break;
}
}
int age=0;
cout << "请输入年龄"<< endl;
cin >>age;
if( age>=0 && age<=150 )
abs->personarray[abs->m_size].m_age=age;
else
cout << "你不是地球人"<< endl;
string phone;
cout << "请输你的电话号码"<< endl;
cin >> phone;
abs->personarray[abs->m_size].m_phone=phone;
string address;
cout << "请输你的住址"<< endl;
cin >> address;
abs->personarray[abs->m_size].m_addr=address;
abs->m_size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
}
增删改查之删除
输入字符串通过isExist查找返回结构体数组下标
然后把结构体数组地址全部前移
人员总数减去1
isExist函数本体在下面
void deletePerson(addressbooks *abs)
{
cout << "请输入您要删除的联系人" << endl;
string name;
cin >> name;
int ret =isExist(abs,name);
if(ret!=-1)
{
for(int i=ret;i<abs->m_size;i++)
{
abs->personarray[i]=abs->personarray[i+1];
}
abs->m_size--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
isExist函数本体
遍历数组,对照结构体1的名字成员,返回下标
int isExist(addressbooks * abs,string name)
{
for(int i=0; i <abs->m_size; i++)
{
if(abs->personarray[i].m_name==name)
{
return i;
}
}
return -1;
}
增删改查之修改(看源码理解,没有新知识点)
void revampPerson(addressbooks* abs)
{
cout << "请输入您要修改的联系人" << endl;
string name;
cin >> name;
int ret =isExist(abs,name);
if(ret!=-1)
{
cout <<"新名字修改为"<< endl;
string new_name;
cin >> new_name;
abs->personarray[ret].m_name=new_name;
cout <<"性别修改为"<< endl;
int new_sex;
cin >> new_sex;
abs->personarray[ret].m_sex=new_sex;
cout <<"年龄修改为"<< endl;
int new_age;
cin >> new_age;
abs->personarray[ret].m_age=new_age;
cout <<"家庭住址修改为"<< endl;
string new_addr;
cin >> new_addr;
abs->personarray[ret].m_addr=new_addr;
cout <<"电话修改为"<< endl;
string new_phone;
cin >> new_phone;
abs->personarray[ret].m_phone=new_phone;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
增删改查之总体查询(看源码理解,没有新知识点)
void showPerson(addressbooks * abs)
{
if(abs->m_size==0)
{
cout << "记录为空" << endl;
}
else
for(int i=0;i<abs->m_size;i++)
{
cout <<"姓名"<<abs->personarray[i].m_name<<"\t";
cout <<"性别"<<(abs->personarray[i].m_sex==1 ? "男":"女")<<"\t";
cout <<"年龄"<<abs->personarray[i].m_age<<"\t";
cout <<"电话"<<abs->personarray[i].m_phone<<"\t";
cout <<"住址"<<abs->personarray[i].m_addr<<endl;
cout<<endl;
cout<<endl;
}
system("pause");
system("cls");
}
增删改查之单独查询(看源码理解,没有新知识点)
void findPerson(addressbooks* abs)
{
string name;
cout <<"请输入你要查找的联系人名字"<<endl;
cin >> name;
int ret =isExist(abs,name);
if(ret!=-1)
{
cout<<"名字 : "<<abs->personarray[ret].m_name<<"\t";
cout<<"年龄 : "<<abs->personarray[ret].m_age<<"\t";
cout<<"住址 : "<<abs->personarray[ret].m_addr<<"\t";
cout<<"电话 : "<<abs->personarray[ret].m_phone<<"\t";
cout<<"性别 : "<<(abs->personarray[ret].m_sex==1?"男":"女")<<endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
学习记录,源码评论区获取