c++通讯录问题
#include<iostream>#include <string>
using namespace std;
#define MAX 1000
//联系人结构体
struct Person {
string m_Name;//姓名
string m_Sex;//性别 1m 2fm
string m_Phone;//电话
string m_Add;//住址
string m_Pcoad;//邮编
string m_QQ;//qq号
string m_Uow;//工作单位
};
//通讯录结构体
struct Addressbooks {
//已保存的联系人数组
struct Person personArry;
//通讯录中联系人个数
int m_Size;
};
//1,添加联系人
void addPerson(Addressbooks* abs) {
if (abs->m_Size == MAX) {
cout << "通讯录已满,无法添加!" << endl;
return;
}
else {
//姓名
string name;
cout << "请输入姓名;" << endl;
cin >> name;
abs->personArry.m_Name = name;
//性别
cout << "请输入性别:(男/女)" << endl;
string sex;
cin >> sex;
abs->personArry.m_Sex = sex;
//电话
cout << "请输入联系电话:" << endl;
string phone;
cin >> phone;
abs->personArry.m_Phone = phone;
//住址
cout << "请输入地址:" << endl;
string add;
cin >> add;
abs->personArry.m_Add = add;
//邮编
cout << "请输入邮编:" << endl;
string pcoad;
cin >> pcoad;
abs->personArry.m_Pcoad = pcoad;
cout << "请输入QQ:" << endl;
string QQ;
cin >> QQ;
abs->personArry.m_QQ = QQ;
//工作单位
cout << "请输入工作单位:" << endl;
string uow;
cin >> uow;
abs->personArry.m_Uow = uow;
//更新人数
abs->m_Size++;
cout << "添加成功" << endl;
system("pause");
system("cls");//清屏
}
}
//3显示所有联系人
void showPerson(Addressbooks* abs) {
//判断是否为零
if (abs->m_Size == 0) {
cout << "当前记录为空" << endl;
}
else {
for (int i = 0; i < abs->m_Size; i++) {
cout << "姓名;" << abs->personArry.m_Name << "\t";
cout << "性别;" << abs->personArry.m_Sex << "\t";
cout << "联系电话:" << abs->personArry.m_Phone << "\t";
cout << "地址:" << abs->personArry.m_Add << "\t";
cout << "邮编:" << abs->personArry.m_Pcoad << "\t";
cout << "QQ:" << abs->personArry.m_QQ << "\t";
cout << "工作单位:" << abs->personArry.m_Uow << endl;
}
}
system("pause");
system("cls");//清屏
}
//2 姓名查找
//检查联系人是否存在,存在返回位置,不存在返回-1
//参数1 通讯录参数2 对比姓名
int E1(Addressbooks* abs, string name) {
for (int i = 0; i < abs->m_Size; i++) {
//如果找到
if (abs->personArry.m_Name == name) {
return i;
}
}
return -1;
}
void findPerson1(Addressbooks* abs) {
cout << "请输入您要查找的姓名" << endl;
string name;
cin >> name;
//判断
int r = E1(abs, name);
if (r != -1)
{
cout << "姓名;" << abs->personArry.m_Name << "\t";
cout << "性别;" << abs->personArry.m_Sex << "\t";
cout << "联系电话:" << abs->personArry.m_Phone << "\t";
cout << "地址:" << abs->personArry.m_Add << "\t";
cout << "邮编:" << abs->personArry.m_Pcoad << "\t";
cout << "QQ:" << abs->personArry.m_QQ << "\t";
cout << "工作单位:" << abs->personArry.m_Uow << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");//清屏
}
// 单位查找
int E2(Addressbooks* abs, string uow) {
for (int i = 0; i < abs->m_Size; i++) {
//如果找到
if (abs->personArry.m_Uow == uow) {
return i;
}
}
return -1;
}
void findPerson2(Addressbooks* abs) {
cout << "请输入您要查找的单位" << endl;
string uow;
cin >> uow;
//判断
int r = E2(abs, uow);
if (r != -1)
{
cout << "姓名;" << abs->personArry.m_Name << "\t";
cout << "性别;" << abs->personArry.m_Sex << "\t";
cout << "联系电话:" << abs->personArry.m_Phone << "\t";
cout << "地址:" << abs->personArry.m_Add << "\t";
cout << "邮编:" << abs->personArry.m_Pcoad << "\t";
cout << "QQ:" << abs->personArry.m_QQ << "\t";
cout << "工作单位:" << abs->personArry.m_Uow << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");//清屏
}
// QQ查找
int E3(Addressbooks* abs, string QQ) {
for (int i = 0; i < abs->m_Size; i++) {
//如果找到
if (abs->personArry.m_QQ == QQ) {
return i;
}
}
return -1;
}
void findPerson3(Addressbooks* abs) {
cout << "请输入您要查找的QQ" << endl;
string QQ;
cin >> QQ;
//判断
int r = E3(abs, QQ);
if (r != -1)
{
cout << "姓名;" << abs->personArry.m_Name << "\t";
cout << "性别;" << abs->personArry.m_Sex << "\t";
cout << "联系电话:" << abs->personArry.m_Phone << "\t";
cout << "地址:" << abs->personArry.m_Add << "\t";
cout << "邮编:" << abs->personArry.m_Pcoad << "\t";
cout << "QQ:" << abs->personArry.m_QQ << "\t";
cout << "工作单位:" << abs->personArry.m_Uow << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");//清屏
}
//4删除 将删除对象之后的数据向前移动,并让通讯录人员减一
void deletePerson(Addressbooks* abs) {
cout << "请输入你要删除的人" << endl;
string name;
cin >> name;
int r = E1(abs, name);//接收返回值
if (r != -1) {
for (int i = r; i < abs->m_Size; i++) {
//数据迁移
abs->personArry = abs->personArry;
}
abs->m_Size--;//人员减一
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");//清屏
}
void showMenu() {//菜单
cout << " —————————————— " << endl;
cout << "————1,添加联系人————" << endl;
cout << "——****—2.查找联系人—****——" << endl;
cout << "——****—3,显示联系人—****——" << endl;
cout << "—————4,删除联系人—————" << endl;
cout << "——****—5,修改联系人—****——" << endl;
cout << "——****—6,清空联系人—****——" << endl;
cout << "————0,退出通讯录————" << endl;
cout << " —————————————— " << endl;
}
int main() {
Addressbooks abs;
abs.m_Size = 0;
int x = 0;
while (true) {
showMenu();//菜单调用
cin >> x;
switch (x) {
case 1://添加
addPerson(&abs);//址传递修饰实参
break;
case 2://查找
{ int s;//查找所用变量
cout << "请输入你的查找方式" << endl;
cout << "1----姓名查找" << endl;
cout << "2----单位查找" << endl;
cout << "3----QQ查找" << endl;
cin >> s;
switch (s) {
case 1://xingming
findPerson1(&abs);
break;
case 2://danwei
findPerson2(&abs);
break;
case 3://QQ
findPerson3(&abs);
break;
default:
break;
}
}
break;
case 3://显示
showPerson(&abs);
break;
case 4://删除
deletePerson(&abs);
break;
case 5://修改
break;
case 6://清空
break;
case 0://退出
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
如果添加的信息中有多人同名,查找这个姓名时只会出现第一个录入人的信息,其他信息不显示该怎么修改? int E1(Addressbooks* abs, string name,int begin=0) {///////////////////
for (int i = begin; i < abs->m_Size; i++) {
//如果找到
if (abs->personArry.m_Name == name) {
return i;
}
}
return -1;
}
void findPerson1(Addressbooks* abs) {
cout << "请输入您要查找的姓名" << endl;
string name;
cin >> name;
//判断
int r=-1,flag=1;//////////
while(1)////////////
{
r = E1(abs, name,r+1);////////////////
if (r != -1)
{
cout << "姓名;" << abs->personArry.m_Name << "\t";
cout << "性别;" << abs->personArry.m_Sex << "\t";
cout << "联系电话:" << abs->personArry.m_Phone << "\t";
cout << "地址:" << abs->personArry.m_Add << "\t";
cout << "邮编:" << abs->personArry.m_Pcoad << "\t";
cout << "QQ:" << abs->personArry.m_QQ << "\t";
cout << "工作单位:" << abs->personArry.m_Uow << endl;
flag=0;/////////////
}
else
break;///////////
}
if(flag)/////////
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");//清屏
}
页:
[1]