// 通讯录管理系统【c++基础练习】.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <string>
#include<cstdlib>
using namespace std;
#define MAX 1000
void showMenu() {
cout << "1.添加联系人" << endl;
cout << "2.显示联系人" << endl;
cout << "3.删除联系人" << endl;
cout << "4.查找联系人" << endl;
cout << "5.修改联系人" << endl;
cout << "6.清空联系人" << endl;
cout << "0.退出通讯录" << endl;
};
// 联系人结构体
struct Person
{
string m_Name;
int m_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
struct Addressbooks
{
struct Person personArray[MAX];
int m_Size=0;
};
void showPerson(Addressbooks* abs)
{
if (abs->m_Size == 0)
{
cout << "通讯录里没有联系人" << endl;
}
else
{
cout << "-----通讯录列表-----" << endl;
for (int i = 0; i < abs->m_Size; i++) {
cout
<< "姓名:" << abs->personArray[i].m_Name
<< " 性别:" << abs->personArray[i].m_Sex
<< " 年龄:" << (abs->personArray[i].m_Age == 1 ? "男" : "女")
<< " 手机号:" << abs->personArray[i].m_Phone
<< " 住址:" << abs->personArray[i].m_Addr
<<
endl;
}
}
system("pause");//任意键继续
system("cls");//清屏
};
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;
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;
}
}
//年龄
int age = 0;
cout << "请输入年龄:" << endl;
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;
//电话
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");//清屏
}
}
//检测联系人是否存在 如果存在返回联系人所在数组中的具体位置(下标编号)不存在返回-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 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");//清屏
};
void findPerson(Addressbooks* abs)
{
cout << "请输入要查找的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1) {
cout << abs->personArray[ret].m_Phone << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");//任意键继续
system("cls");//清屏
};
void changePerson(Addressbooks* abs)
{
cout << "请输入要修改的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1) {
cout << "请输入要修改的手机号:" << endl;
string phone;
cin >> phone;
abs->personArray[ret].m_Phone = phone;
cout << "修改成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");//任意键继续
system("cls");//清屏
};
int main()
{
struct Addressbooks abs;
abs.m_Size = 0;
int select = 0;
while (true)
{
showMenu();
cin >> select;
switch (select)
{
case 1: //1.添加联系人
addPerson(&abs);
break;
case 2: //2.显示联系人
showPerson(&abs);
break;
case 3: //3.删除联系人
deletePerson(&abs);
break;
case 4: //4.查找联系人
findPerson(&abs);
break;
case 5: //5.修改联系人
changePerson(&abs);
break;
case 6: //6.清空联系人
abs.m_Size = 0;
break;
case 0: //0.退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
【c++基础练习】通讯录管理系统
最新推荐文章于 2024-07-22 09:38:37 发布