一、显示功能菜单
功能描述: 显示用户可执行的功能。
代码实现
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(){
int select = 0;
while(true){
showMenu();
cin >> select;
switch(select){
case 1: //添加联系人
break;
case 2: //显示联系人
break;
case 3: //删除联系人
break;
case 4: //查找联系人
break;
case 5: //修改联系人
break;
case 6: //清空联系人
break;
case 0: //退出通讯录
cout << "欢迎下次使用" << endl;
break;
default:
break;
}
}
}
三、添加联系人
功能描述: 实现逐一添加联系人的功能
代码实现
//1.联系人结构体
struct Person{
string m_name; //姓名
int m_sex; //性别 1男 2女
int m_age; //年龄
string m_phone; //电话
string m_addr; //住址
}
//2.通讯录结构体
#define MAX 1000 //通讯录最大人数
struct addressBooks{
Person pArray[MAX];
int m_size; //当前通讯录人员个数
}
//3.创建通讯录并初始化人数
addressbooks abs;
abs.m_size = 0;
//4.封装添加联系人函数
void addPerson(addressBooks *abs){
//判断通讯录是否已满
if(abs->m_szie >= MAX){
cout << "通讯录已满,无法添加" << endl;
return;
}
//姓名
cout << "请输入姓名:" << endl;
string name;
cin >> name;
abs->pArray[abs->m_size].m_name = name;
//性别
cout << "请输入性别(1男2女)" << endl;
int sex;
cin >> sex;
abs->pArray[abs->m.size].m_sex = sex;
//年龄
cout << "请输入年龄" << endl;
int age;
abs->pArray[abs->m_size].m_age = age;
//联系电话
cout << "请输入联系电话:" << endl;
string phone;
cin >> phone;
abs->pArray[abs->m_size].m_phone = phone;
//家庭住址
cout << "请输入家庭住址:" << endl;
string address;
cin >> address;
abs->pArray[abs->m_size].m_addr = address;
//通讯录人数增加
abs->m_size++;
cout << "添加成功" << endl;
cout << "按任意键继续" << endl;
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
}
四、显示联系人
功能描述: 遍历通讯录并全部输出
代码实现
void showPerson(addressBooks * abs){
if(abs->m_size == 0){
cout << "通讯录为空" << endl;
return;
}
for(int i = 0; i < abs->m_size; i++){
cout << "姓名:" << abs->pArray[i].m_name << '\t';
cout << "性别:" << (abs->pArray[i].m_sex == 1 ? "男" : "女") << '\t';
cout << "年龄:" << abs->pArray[i].m_age << '\t';
cout << "电话:" << abs->pArray[i].m_phone << '\t';
cout << "住址:" << abs->pArray[i].m_addr << endl;
}
cout << "按任意键继续" << endl;
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
}
五、删除联系人
功能描述: 遍历通讯录寻找目标联系人人姓名,并记录位置,实现删除联系人的操作。
代码实现
//按名字寻找联系人,如果找到返回下标,找不到返回0
int isExist(addressBooks * abs, string name){
for(int i = 0; i < abs->m_size; i++){
if(abs->pArray[i].m_name == name)
return i;
}
return -1;
}
void deletePerson(addressBooks * abs){
cout << "请输入您想删除的联系人的姓名:" << endl;
string name;
cin >> name;
int index = isExist(abs, name);
if(index != -1){
for(int i = index; i < abs->m_size; i++) //目标后的所有联系人前移一位
abs->pArray[i] = abs->pArray[i+1];
abs->m_size--; //通讯录人数减1
cout << "删除成功" << endl;
}
else
cout << "查无此人" << endl;
cout << "按任意键继续" << endl;
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
}
五、查找联系人
功能描述: 遍历通讯录寻找目标联系人姓名,并输出联系人信息。
代码实现
void findPerson(addressBooks * abs){
cout << "请输入您想查找的联系人姓名" << endl;
string name;
cin >> name;
int index = isExist(abs, name);
if(index != -1){
cout << "姓名:" << abs->pArray[index].m_name << "\t";
cout << "性别:" << abs->pArray[index].m_sex << "\t";
cout << "年龄:" << abs->pArray[index].m_age << "\t";
cout << "电话:" << abs->pArray[index].m_phone << "\t";
cout << "住址:" << abs->pArray[index].m_addr << endl;
}
else
cout << "查无此人" << endl;
cout << "按任意键继续" << endl;
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
}
六、修改联系人
功能描述: 遍历通讯录寻找目标联系人姓名,并修改联系人信息。
代码实现
void revisePerson(addressBooks * abs){
cout << "请输入您想修改的联系人的姓名" << endl;
string name;
cin >> name;
int index = isExist(abs, name);
if(index != -1){
//姓名
cout << "请输入姓名:" << endl;
cin >> abs->pArray[index].m_name;
//性别
cout << "请输入性别(1男2女):" << endl;
cin >> abs->pArray[index].m_sex;
//年龄
cout << "请输入年龄:" << endl;
cin >> abs->pArray[index].m_age;
//联系电话
cout << "请输入联系电话:" << endl;
cin >> abs->pArray[index].m_phone;
//家庭住址
cout << "请输入家庭住址:" << endl;
cin >> abs->pArray[index].m_addr;
cout << "修改成功" << endl;
}
else
cout << "查无此人" << endl;
cout << "按任意键继续" << endl;
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
}
七、清空联系人
功能描述: 清空通讯录中的所有信息。
代码实现
void clearPerson(addressBooks * abs){
abs->m_size = 0;
cout << "通讯录已清空" << endl;
cout << "按任意键继续" << endl;
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
}
八、完整代码
#include <iostream>
#define MAX 1000 //通讯录人数上限
using namespace std;
struct Person{ //联系人结构体
string m_name;
int m_sex;
int m_age;
string m_phone;
string m_addr;
};
struct addressBooks{
Person pArray[MAX]; //联系人结构体数组
int m_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;
}
//1.添加联系人
void addPerson(addressBooks * abs){
if(abs->m_size == MAX){
cout << "通讯录已满,无法添加" << endl;
return;
}
string m_name;
int m_sex;
int m_age;
string m_phone;
string m_addr;
//姓名
cout << "请输入姓名:" << endl;
cin >> abs->pArray[abs->m_size].m_name;
//性别
cout << "请输入性别(1男2女)" << endl;
cin >> abs->pArray[abs->m_size].m_sex;
//年龄
cout << "请输入年龄" << endl;
cin >> abs->pArray[abs->m_size].m_age;
//联系电话
cout << "请输入联系电话:" << endl;
cin >> abs->pArray[abs->m_size].m_phone;
//家庭住址
cout << "请输入家庭住址:" << endl;
cin >> abs->pArray[abs->m_size].m_addr;
//通讯录人数增加
abs->m_size++;
cout << "添加成功" << endl;
cout << "按任意键继续" << endl;
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
}
//2.显示联系人
void showPerson(addressBooks * abs){
if(abs->m_size == 0){
cout << "通讯录为空" << endl;
return;
}
for(int i = 0; i < abs->m_size; i++){
cout << "姓名:" << abs->pArray[i].m_name << '\t';
cout << "性别:" << (abs->pArray[i].m_sex == 1 ? "男" : "女") << '\t';
cout << "年龄:" << abs->pArray[i].m_age << '\t';
cout << "电话:" << abs->pArray[i].m_phone << '\t';
cout << "住址:" << abs->pArray[i].m_addr << endl;
}
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
}
//判断通讯录中是否有联系人
int isExist(addressBooks * abs, string name){
for(int i = 0; i < abs->m_size; i++){
if(name == abs->pArray[i].m_name)
return i;
}
return -1;
}
//3.查找联系人
void findPerson(addressBooks * abs){
cout << "请输入您想查找的联系人的姓名" << endl;
string name;
cin >> name;
int index = isExist(abs, name);
if(index != -1){
cout << "姓名:" << abs->pArray[index].m_name << "\t";
cout << "性别:" << abs->pArray[index].m_sex << "\t";
cout << "年龄:" << abs->pArray[index].m_age << "\t";
cout << "电话:" << abs->pArray[index].m_phone << "\t";
cout << "住址:" << abs->pArray[index].m_addr << endl;
}
else
cout << "查无此人" << endl;
cout << "按任意键继续" << endl;
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
}
//4.删除联系人
void deletePerson(addressBooks * abs){
cout << "请输入您想删除的联系人的姓名" << endl;
string name;
cin >> name;
int index = isExist(abs, name);
if(index != -1){
for(int i = index; i < abs->m_size; i++)
abs->pArray[i] = abs->pArray[i+1];
abs->m_size--;
cout << "删除成功" << endl;
}
else
cout << "查无此人" << endl;
cout << "按任意键继续" << endl;
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
}
//5.修改联系人
void revisePerson(addressBooks * abs){
cout << "请输入您想修改的联系人的姓名" << endl;
string name;
cin >> name;
int index = isExist(abs, name);
if(index != -1){
//姓名
cout << "请输入姓名:" << endl;
string name;
cin >> abs->pArray[index].m_name;
//性别
cout << "请输入性别(1男2女):" << endl;
cin >> abs->pArray[index].m_sex;
//年龄
cout << "请输入年龄:" << endl;
cin >> abs->pArray[index].m_age;
//联系电话
cout << "请输入联系电话:" << endl;
cin >> abs->pArray[index].m_phone;
//家庭住址
cout << "请输入家庭住址:" << endl;
cin >> abs->pArray[index].m_addr;
cout << "修改成功" << endl;
}
else
cout << "查无此人" << endl;
cout << "按任意键继续" << endl;
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
}
//清空联系人
void clearPerson(addressBooks * abs){
abs->m_size = 0;
cout << "通讯录已清空" << endl;
cout << "按任意键继续" << endl;
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
}
int main(){
addressBooks abs;
abs.m_size = 0;
int select;
while(true){
showMenu();
cin >> select;
switch(select){
case 1:
addPerson(&abs); //添加联系人
break;
case 2:
showPerson(&abs); //显示联系人
break;
case 3:
findPerson(&abs); //查找联系人
break;
case 4:
deletePerson(&abs); //删除联系人
break;
case 5:
revisePerson(&abs); //修改联系人
break;
case 6:
clearPerson(&abs); //清空联系人
break;
case 0:
cout << "欢迎下次使用" << endl;
cout << "按任意键继续" << endl;
system("pause"); //按任意键继续
system("cls"); //按完键后清屏
break;
default :
break;
}
}
}