1.添加联系人:向通讯录中添加联系人(姓名,性别,年龄,电话,住址)
2.显示联系人:显示目前通讯录中所有联系人的信息
3.删除联系人:在通讯录中删除指定联系人的信息
4.查找联系人:查找并显示出指定联系人的信息
5.修改联系人:修改指定联系人的信息
6.清空联系人:清空通讯录中所有联系人的信息
0.退出通讯录:推出通讯录
struct Member
{
string name;
int sex;
int age;
int phone;
string addr;
};//联系人
struct Addresslist
{
Member m_Arry[MAX];
int m_num = 0;
};//通讯录
要点:用户每次打开或操作完都要显示,屏幕上不能有太多信息干扰选项,尽可能美观。
void Menu()
{
cout << "-----------【主菜单】-----------" << endl;
cout << "---------1.添加联系人:----------" << endl;
cout << "---------2.显示联系人:---------" << endl;
cout << "---------3.删除联系人:---------" << endl;
cout << "---------4.查找联系人:---------" << endl;
cout << "---------5.修改联系人:---------" << endl;
cout << "---------6.清空联系人:---------" << endl;
cout << "---------0.退出通讯录:---------" << endl;
}//菜单
要点:通讯录满时不可添加并告知,不满时添加联系人
void Addmember(Addresslist *ads = NULL)
{
if (ads->m_num == MAX)
{
cout << "通讯录已满,无法添加联系人!" << endl;
return;
}//姓名
cout << "请输入姓名:" << endl;
cin >> ads->m_Arry[ads->m_num].name;
int sex = 0;
while (1)
{
cout << "请输入"<<ads->m_Arry[ads->m_num].name<<"的性别:" << endl
<< "0-------女" << endl
<< "1-------男" << endl;
cin >> sex;
if (sex == 0 || sex == 1)
{
ads->m_Arry[ads->m_num].sex = sex;
break;
}
cout << "您输入的性别有误,请重新输入。" << endl;
}//性别
cout << "请输入" << ads->m_Arry[ads->m_num].name << "的年龄:" << endl;
cin >> ads->m_Arry[ads->m_num].age;
cout << "请输入" << ads->m_Arry[ads->m_num].name << "的联系电话:" << endl;
cin >> ads->m_Arry[ads->m_num].phone;
cout << "请输入" << ads->m_Arry[ads->m_num].name << "的住址:" << endl;
cin >> ads->m_Arry[ads->m_num].addr;
cout << ads->m_Arry[ads->m_num].name << "联系人添加成功" << endl;
ads->m_num++;
system("pause");
system("cls");
}//添加联系人
要点:通讯录为空时告知
void Showmember(Addresslist *ads = NULL)
{
if (ads->m_num == 0)
{
cout << "通讯录中暂无联系人" << endl;
}
for (int i = 0; i < ads->m_num; i++)
{
cout << "姓名: " <<ads->m_Arry[i].name << "\t";
cout << "性别: " << (ads->m_Arry[i].sex == 1 ? "男" : "女") << "\t";
cout << "年龄: " <<ads->m_Arry[i].age << "\t";
cout << "电话: " <<ads->m_Arry[i].phone << "\t";
cout << "住址: " << ads->m_Arry[i].addr << "\t" << endl;
}
system("pause");
system("cls");
}//显示联系人
要点:先查找是否存在该联系人,若无告知,有则删除
要点:遍历通讯录,对于查找的联系人,若存在则返回其位置,若不存在返回-1
int Exist(Addresslist *ads, string name)
{
for (int i = 0; i < ads->m_num; i++)
{
if (ads->m_Arry[i].name == name)
{
return i;
}
}
return -1;
}//存在确认
void Deletemember(Addresslist *ads = NULL)
{
string name;
cout << "请输入您要删除的联系人姓名:" << endl;
cin >> name;
int ret = Exist(ads, name);
if ( ret == -1)
{
cout << "联系人不存在" << endl;
}
else
{
cout << "确定删除联系人" <<ads->m_Arry[ret].name<<"吗?"<< endl
<< "0-------取消\t" << "1-------确定" << endl;
int select = 0;
cin >>select;
if (ret == MAX)
{
ads->m_num--;
}
else if (select == 1)
{
for (int i = ret; i < ads->m_num - 1; i++)
{
ads->m_Arry[i] = ads->m_Arry[i + 1];
}
}
ads->m_num--;
cout << "删除完毕" << endl;
}
system("pause");
system("cls");
}//删除联系人
要点:判断联系人是否存在,存在则输出联系人信息,不存在则告知
void Findmember(Addresslist *ads = NULL)
{
string name;
cout << "请输入您要查找的联系人姓名:" << endl;
cin >> name;
int ret = Exist(ads, name);
if (ret == -1)
{
cout << "联系人不存在" << endl;
}
else
{
cout << "姓名: " << ads->m_Arry[ret].name << "\t";
cout << "性别: " << (ads->m_Arry[ret].sex == 1 ? "男" : "女") << "\t";
cout << "年龄: " << ads->m_Arry[ret].age << "\t";
cout << "电话: " << ads->m_Arry[ret].phone << "\t";
cout << "住址: " << ads->m_Arry[ret].addr << "\t" << endl;
}
system("pause");
system("cls");
}//查找联系人
要点:判断联系人是否存在,存在则修改联系人信息并输出修改后信息,不存在则告知
void Modifymember(Addresslist *ads = NULL)
{
string name;
cout << "请输入您要修改的联系人姓名:" << endl;
cin >> name;
int ret = Exist(ads, name);
if (ret == -1)
{
cout << "联系人不存在" << endl;
}
else
{
cout << "请输入修改后的姓名:" << endl;
cin >> ads->m_Arry[ret].name;
int sex = 0;
while (1)
{
cout << "请输入" << ads->m_Arry[ret].name << "的性别:" << endl
<< "0-------女" << endl
<< "1-------男" << endl;
cin >> sex;
if (sex == 0 || sex == 1)
{
ads->m_Arry[ret].sex = sex;
break;
}
cout << "您输入的性别有误,请重新输入。" << endl;
}//性别
cout << "请输入" << ads->m_Arry[ret].name << "的年龄:" << endl;
cin >> ads->m_Arry[ret].age;
cout << "请输入" << ads->m_Arry[ret].name << "的联系电话:" << endl;
cin >> ads->m_Arry[ret].phone;
cout << "请输入" << ads->m_Arry[ret].name << "的住址:" << endl;
cin >> ads->m_Arry[ret].addr;
}
system("pause");
system("cls");
}
要点:清空所有联系人
void Cleanmember(Addresslist *ads = NULL)
{
cout << "是否要清空通讯录" << endl
<< "0-------否\t" << "1-------是" << endl;
int ret = 0;
cin >> ret;
if (ret == 0)
{
system("pause");
system("cls");
}
else
{
ads->m_num = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
}//清空联系人
#include"function.h"
#include
#include
using namespace std;
int main()
{
Addresslist ads;
ads.m_num = 0;
int choice = 0;
while (1)
{
Menu();
cin >> choice;
switch (choice)
{
case 1://1.添加联系人
Addmember(&ads);
break;
case 2://2.显示联系人
Showmember(&ads);
break;
case 3://3.删除联系人
Deletemember(&ads);
break;
case 4://4.查找联系人
Findmember(&ads);
break;
case 5://5.修改联系人
Modifymember(&ads);
break;
case 6://6.清空联系人
Cleanmember(&ads);
break;
case 0://0.退出通讯录
cout << "感谢您使用本通讯录系统" << endl;
system("pause");
return 0;
default:
cout << "您输入的选项有误,请重新输入\n" << endl;
}
}
system("pause");
return 0;
}
跟着黑马课程学习C++时的一些笔记,代码并不是完全按照课程来写的,我是分文件编写的,功能函数和结构体都包含在一个头文件里,并且做了一些小修改,以我目前知识储量还无法做出太大优化,希望以后可以吧。