仅仅在内存上实现简单的通讯录,不涉及数据库,程序终止数据不存
- 添加联系人
- 显示联系人
- 删除联系人
- 查找联系人
- 修改联系人
- 清空联系人
- 退出通讯录
void showMenu()//显示菜单
void addPerson(Addressbooks *p)//添加联系人
void showPerson(Addressbooks* p)//显示联系人
void deletePerson(Addressbooks* p)//删除联系人
void findPerson(Addressbooks* p)//查找联系人
void changePerson(Addressbooks* p)//修改联系人
void cleanPerson(Addressbooks* p)//清空联系人
#include <iostream>
#include <string>
using namespace std;
#define MAX 500//联系人最大数量
//联系人结构体
struct Person
{
string m_Name="";
int m_Sex=0;
int m_Age=0;
string m_Phone="";
string m_Addr="";
};
//通讯录结构体
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 << "|0、退出通讯录|" << endl;
}
void addPerson(Addressbooks *p) {
if (p->m_Size >= MAX) {
cout << "通讯录已满" << endl;
return;
}
else
{
//添加具体联系人
//姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
p->personArray[p->m_Size].m_Name=name;
//性别
int sex;
cout << "请输入性别:" << endl;
cout << "1--男,2--女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2) {
p->personArray[p->m_Size].m_Sex=sex;
break;
}
else
cout << "输入有误,清重新输入" << endl;
}
//年龄
int age;
cout << "请输入年龄:" << endl;
cin >> age;
p->personArray[p->m_Size].m_Age=age;
//电话
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
p->personArray[p->m_Size].m_Phone=phone;
//地址
string address;
cout << "请输入地址:" << endl;
cin >> address;
p->personArray[p->m_Size].m_Addr=address;
p->m_Size++;
system("cls");
cout << "添加成功" << endl;
}
}
void showPerson(Addressbooks* p) {
if (p->m_Size == 0) {
system("cls");
cout << "列表为空,请添加联系人" << endl;
}
else
{
for (int i = 0; i < p->m_Size; i++) {
cout << "姓名 " << p->personArray[i].m_Name << endl;
cout << "性别 " << (p->personArray[i].m_Sex==1?"男":"女") << endl;
cout << "年龄 " << p->personArray[i].m_Age << endl;
cout << "电话 " << p->personArray[i].m_Phone << endl;
cout << "地址 " << p->personArray[i].m_Addr << endl;
}
}
system("pause");
system("cls");
}
void deletePerson(Addressbooks* p)
{
string name;
cout << "请输入需要删除的联系人姓名" << endl;
cin >> name;
for (int i = 0; i <= p->m_Size; i++)
{
if (name == p->personArray[i].m_Name)
{
for (int j = i; j < p->m_Size; j++)
p->personArray[j] = p->personArray[j + 1];
cout << "删除成功" << endl;
p->m_Size--;
break;
}
else
{
cout << "查无此人,请确认后再执行删除操作" << endl;
}
}
}
void cleanPerson(Addressbooks* p) {
int ifOrElse;//存放是否确定的变量值
cout << "确认清空联系人?" << endl;
cout << "1--是,2--否" << endl;
cin >> ifOrElse;
if (ifOrElse == 1)
p->m_Size = 0;
system("pause");
system("cls");
}
void changePerson(Addressbooks* p) {
//具体联系人
if (p->m_Size == 0) {
system("pause");
system("cls");
cout << "列表为空,请添加联系人" << endl;
}
else {
int i = 0;
string name;
cout << "请输入需要修改的姓名:" << endl;
cin >> name;
for (; i < p->m_Size; i++)
{
if (name == p->personArray[i].m_Name)
{
cout << "查找成功" << endl;
//姓名
cout << "请输入姓名:" << endl;
cin >> name;
p->personArray[i].m_Name = name;
//性别
int sex;
cout << "请输入性别:" << endl;
cout << "1--男,2--女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2) {
p->personArray[i].m_Sex = sex;
break;
}
else
cout << "输入有误,清重新输入" << endl;
}
//年龄
int age;
cout << "请输入年龄:" << endl;
cin >> age;
p->personArray[i].m_Age = age;
//电话
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
p->personArray[i].m_Phone = phone;
//地址
string address;
cout << "请输入地址:" << endl;
cin >> address;
p->personArray[i].m_Addr = address;
system("pause");
system("cls");
cout << "修改成功" << endl;
break;
}
}
if (i >= p->m_Size)
{
system("pause");
system("cls");
cout << "查无此人,请确认后再执行修改操作" << endl;
}
}
}
void findPerson(Addressbooks* p) {
if (p->m_Size == 0) {
system("cls");
cout << "列表为空,请添加联系人" << endl;
}
else
{
cout << "请输入被查找人姓名" << endl;
string name;
cin >> name;
for (int i = 0; i < p->m_Size; i++) {
if (name == p->personArray[i].m_Name) {
cout << "姓名 " << p->personArray[i].m_Name << endl;
cout << "性别 " << (p->personArray[i].m_Sex == 1 ? "男" : "女") << endl;
cout << "年龄 " << p->personArray[i].m_Age << endl;
cout << "电话 " << p->personArray[i].m_Phone << endl;
cout << "地址 " << p->personArray[i].m_Addr << endl;
break;
}
if(i>= p->m_Size)
{
cout << "查无此人,请重新确认后查找" << endl;
}
}
}
system("pause");
system("cls");
}
int main()
{
Addressbooks abs;//创建结构体变量
abs.m_Size = 0;//设置通讯录初始数量
string name;//存放改删的人名
int select = 0;//创建用户选择变量
while (1) {
showMenu();
cin >> select;
switch (select)
{
case 1://增
addPerson(&abs);
break;
case 2://显示
showPerson(&abs);
break;
case 3://删
deletePerson(&abs);
break;
case 4://查
findPerson(&abs);
break;
case 5://改
changePerson(&abs);
break;
case 6://清空
cleanPerson(&abs);
break;
case 0://程序退出
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
default:
break;
}
}
}
原文:https://www.cnblogs.com/kemuling/p/14299815.html