C++通讯录管理系统-创新互联
一、实现功能
分享题目:C++通讯录管理系统-创新互联
链接URL:http://myzitong.com/article/ddioce.html
- 添加联系人
- 显示联系人
- 删除联系人
- 查找联系人
- 修改联系人
- 清空联系人
- 退出通讯录
#include#includeusing namespace std;
#define MAX 1000
//设计联系人结构体
struct Person
{//姓名
string m_Name;
//性别
int m_Sex;
//年龄
int m_Age;
//电话
string m_Phone;
//住址
string m_Addr;
};
//设计通讯录结构体
struct Addressbooks
{//通讯录中保存的联系人数组
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<< "0、退出通讯录"<< endl;
}
//所有功能
//1.添加联系人
void addPerson(Addressbooks* abs)
{//判断通讯录是否已满,满了就不再添加
if (abs->m_Size == MAX)
{cout<< "通讯录已满,无法添加!"<< endl;
return;
}
else
{//添加具体联系人
string name;
cout<< "请输入姓名:";
cin >>name;
abs->personArray[abs->m_Size].m_Name = name;
int sex;
cout<< "请输入性别:"< cin >>sex;
if (sex == 1 || sex == 2)
{ abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
else
{ cout<< "请重新输入"<>age;
abs->personArray[abs->m_Size].m_Age = age;
string phone;
cout<< "请输入联系电话:";
cin >>phone;
abs->personArray[abs->m_Size].m_Phone = phone;
string address;
cout<< "请输入家庭住址:";
cin >>address;
abs->personArray[abs->m_Size].m_Addr = address;
abs->m_Size++;
cout<< "添加成功"<//判断通讯录中人数是否为0
if (abs->m_Size == 0)
{cout<< "当前记录为空"<< endl;
}
else
{for (int i = 0; i< abs->m_Size; i++)
{ cout<< "姓名:"<< abs->personArray[i].m_Name<< " ";
cout<< "性别:"<< (abs->personArray[i].m_Sex==1?"男":"女")<< " ";
cout<< "年龄:"<< abs->personArray[i].m_Age<< " ";
cout<< "电话:"<< abs->personArray[i].m_Phone<< " ";
cout<< "住址:"<< abs->personArray[i].m_Addr<< endl;
}
}
system("pause");//按任意键继续
system("cls");//清屏
}
//3.删除联系人
//3.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;
}
//3.2删除指定联系人
void deletePerson(Addressbooks *abs)
{string name;
cout<< "请输入您要删除的联系人"<< endl;
cin >>name;
int index = isExist(abs, name);//这里只传abs的原因是abs就是代表了地址了
if(index==-1)
{cout<<"未查到此人"<for (int i = index; i< abs->m_Size - 1; i++)
{ //数据前移
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_Size--;
cout<< "删除成功"<< endl;
}
system("pause");
system("cls");
}
//4.查找联系人
void findPerson(Addressbooks* abs)
{string name;
cout<< "请输入您要查找的联系人"<< endl;
cin >>name;
int index = isExist(abs, name);
if ( index== -1)
{cout<< "找不到这个联系人"<< endl;
}
else
{cout<< "姓名:"<< abs->personArray[index].m_Name<< " ";
cout<< "性别:"<< (abs->personArray[index].m_Sex == 1 ? "男" : "女")<< " ";
cout<< "年龄:"<< abs->personArray[index].m_Age<< " ";
cout<< "电话:"<< abs->personArray[index].m_Phone<< " ";
cout<< "住址:"<< abs->personArray[index].m_Addr<< endl;
}
system("pause");
system("cls");
}
//5.修改联系人
void modifyPerson(Addressbooks* abs)
{string name;
cout<< "请输入要修改的联系人姓名"<< endl;
cin >>name;
int index = isExist(abs, name);
if (index == -1)
{cout<< "查无此人"<< endl;
}
else
{//修改
string name;
cout<< "请输入姓名:";
cin >>name;
abs->personArray[index].m_Name = name;
int sex;
cout<< "请输入性别:"<< endl;
cout<< "1---男"<< endl;
cout<< "2---女"<< endl;
while (true)
{ cin >>sex;
if (sex == 1 || sex == 2)
{ abs->personArray[index].m_Sex = sex;
break;
}
else
{ cout<< "请重新输入"<< endl;
}
}
int age;
cout<< "请输入年龄:";
cin >>age;
abs->personArray[index].m_Age = age;
string phone;
cout<< "请输入联系电话:";
cin >>phone;
abs->personArray[index].m_Phone = phone;
string address;
cout<< "请输入家庭住址:";
cin >>address;
abs->personArray[index].m_Addr = address;
abs->m_Size= abs->m_Size;//只是修改,保存原来人数
cout<< "修改成功"<< endl;
}
system("pause");
system("cls");
}
//6.清空联系人
void cleanPerson(Addressbooks* abs)
{int judge = 0;
cout<< "确认是否清空 1--是,2--否"<< endl;
cin >>judge;
if (judge == 2)
{cout<< "取消清空,返回菜单"<< endl;
}
else if(judge==1)
{abs->m_Size = 0; //逻辑清空
cout<< "通讯录已清空"<< endl;
}
system("pause");
system("cls");
}
int main()
{//创建通讯里结构体变量
Addressbooks abs;
//初始化通讯录中当前人员个数
abs.m_Size = 0;
int select = 0;
while (true)
{//菜单调用
showMenu();
cin >>select;
switch (select)
{case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
{ //case中加括号的原因是string name在switch语句中重复定义了,必须括起来变成局部变量
deletePerson(&abs);
break;
}
case 4:
findPerson(&abs);
break;
case 5:
modifyPerson(&abs);
break;
case 6:
cleanPerson(&abs);
break;
case 0:
cout<< "欢迎下次使用"<< endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
三、参考黑马程序员视频P72~P83- 黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、虚拟主机、营销软件、网站建设、周宁网站维护、网站推广。分享题目:C++通讯录管理系统-创新互联
链接URL:http://myzitong.com/article/ddioce.html