本文实例为大家分享了C++实现通讯录系统项目的具体代码,供大家参考,具体内容如下
制作一个具有添加联系人、删除联系人、修改联系人等功能的通讯录系统
效果图:
代码如下:
#include <iostream>
using namespace std;
#include <string>
#define Max 1000
//创建联系人结构体
struct person
{
string p_name;
int p_sex; //1、男 2、女
int p_age;
string p_phone;
string p_address;
};
//创建通讯录结构体
struct addressbooks
{
//保存的联系人数组
struct person personarr[Max];
//当前记录的人数
int p_size;
};
//菜单
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;
}
//添加联系人
void addperson(addressbooks* abs)
{
//先判断通讯录是否已满
if (abs->p_size == Max)
{
cout << "通讯录已满" << endl;
return;
}
else
{
string name;
cout << "请输入姓名: " << endl;
cin >> name;
abs->personarr[abs->p_size].p_name = name;
cout << "请输入性别: " << endl;
cout << "输入1 -- 男" << endl;
cout << "输入2 -- 女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
//输入1或者2,退出循环. 输入其他数字重新循环
abs->personarr[abs->p_size].p_sex = sex;
break;
}
cout << "请重新输入" << endl;
}
}
//年龄
cout << "请输入年龄" << endl;
int age = 0;
cin >> age;
abs->personarr[abs->p_size].p_age = age;
//电话
cout << "请输入联系电话" << endl;
string phone;
cin >> phone;
abs->personarr[abs->p_size].p_phone = phone;
//地址
cout << "请输入地址" << endl;
string address;
cin >> address;
abs->personarr[abs->p_size].p_address = address;
//更新通讯录
abs->p_size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
//显示联系人
void showperson(addressbooks* abs)
{
if (abs->p_size == 0)
{
cout << "当前联系人为空" << endl;
}
else
{
for (int i = 0; i < abs->p_size; i++)
{
cout << "姓名: " << abs->personarr[i].p_name << "\t";
cout << "性别: " << (abs->personarr[i].p_sex == 1 ? "男" : "女") << "\t";
cout << "年龄: " << abs->personarr[i].p_age << "\t";
cout << "电话: " << abs->personarr[i].p_phone << "\t";
cout << "地址: " << abs->personarr[i].p_address << endl;
}
}
//清屏回到最初菜单
system("pause");
system("cls");
}
//检测联系人是非存在通讯录中,如果存在,返回该联系人在通讯录中的位置,不存在返回-1
int isExist(addressbooks* abs, string name)
{
for (int i = 0; i < abs->p_size; i++)
{
if (abs->personarr[i].p_name == name)
{
return i;
}
return -1; //遍历整个通讯录没有,则返回值为-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->p_size; i++)
{
//后面的数据全部往前覆盖一格
abs->personarr[i] = abs->personarr[i + 1];
}
abs->p_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->personarr[ret].p_name << "\t";
cout << "性别: " << abs->personarr[ret].p_sex << "\t";
cout << "年龄: " << abs->personarr[ret].p_age << "\t";
cout << "电话 " << abs->personarr[ret].p_phone << "\t";
cout << "地址: " << abs->personarr[ret].p_address << endl;
}
else
{
cout << "通讯录中不存在该联系人" << endl;
}
system("pause");
system("cls");
}
//修改联系人
void modifyperson(addressbooks* abs)
{
cout << "请输入您要查找的联系人姓名: " << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
string name;
cout << "请输入新的姓名: " << endl;
cin >> name;
abs->personarr[ret].p_name = name;
int sex = 0;
cout << "请输入性别: 1--男 2--女" << endl;
cout << "请输入性别: " << endl;
cout << "输入1 -- 男" << endl;
cout << "输入2 -- 女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
//输入1或者2,退出循环. 输入其他数字重新循环
abs->personarr[abs->p_size].p_sex = sex;
break;
}
cout << "请重新输入" << endl;
}
int age = 0;
cout << "请输入年龄: " << endl;
cin >> age;
abs->personarr[ret].p_age = age;
string phone;
cout << "请输入新的电话: " << endl;
cin >> phone;
abs->personarr[ret].p_phone = phone;
string address;
cout << "请输入新的地址: " << endl;
cin >> address;
abs->personarr[ret].p_address = address;
cout << "修改成功!" << endl;
}
else
{
cout << "通讯录中无该联系人" << endl;
}
system("pause");
system("cls");
}
//清空联系人
void emptyperson(addressbooks* abs)
{
cout << "确认此操作?" << endl;
cout << "输入: 1--确认" << endl;
cout << "输入: 其他--否" << endl;
int select2 = 0;
cin >> select2;
if (select2 == 1)
{
//将该通讯录中的联系人数量清零
abs->p_size = 0;
cout << " 已清空 " << endl;
}
system("pause");
system("cls");
}
int main()
{
addressbooks abs;
abs.p_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、修改联系人
modifyperson(&abs);
break;
case 6: //6、清空联系人
emptyperson(&abs);
break;
case 0: //0、退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。