通过C++设计的一个通讯录管理系统,能实现对联系人的添加,显示,删除,查找,修改,清空的作用。
源代码#include
#include
#include
#define MAX 1000
using namespace std;
struct Person//联系人结构体
{
string name;
int age;
string sex;
string phone;
string addr;
};
struct addreassbooks//通讯录结构体
{
struct Person personarry[MAX];//通讯录中保存的联系人数组
int m_Size;//通讯录中当前记录的联系人数目
};
void mainMenu()//输出菜单
{
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(addreassbooks* abs)//将联系人的基本信息存入
{
if (abs->m_Size == MAX)//如果指针指向了最大值,即联系人已满
{
cout << "联系人已满,无法添加" << endl;
return;
}
//联系人未满执行以下 *** 作
string name;
cout << "请输入姓名" << endl;
cin >> name;
abs->personarry[abs->m_Size].name = name;
string sex;
cout << "请输入性别" << endl;
cin >> sex;
abs->personarry[abs->m_Size].sex = sex;
int age;
cout << "请输入年龄" << endl;
cin >> age;
abs->personarry[abs->m_Size].age = age;
string phone;
cout << "请输入电话" << endl;
cin >> phone;
abs->personarry[abs->m_Size].phone = phone;
string addr;
cout << "请输入地址" << endl;
cin >> addr;
abs->personarry[abs->m_Size].addr = addr;
//更新通讯录人数
abs->m_Size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
void printPerson(addreassbooks* abs)//显示联系人
{
if (abs->m_Size == 0)//如果没有存入联系人,即 m_Size为0
{
cout << "当前记录为空" << endl;
}
else
{
for (int i = 0; i < abs->m_Size; i++)//用循环分别展示每个联系人的详细信息
{
cout << "姓名\t" << abs->personarry[i].name << endl;
cout << "性别\t" << abs->personarry[i].sex << endl;
cout << "年龄\t" << abs->personarry[i].age << endl;
cout << "电话\t" << abs->personarry[i].phone << endl;
cout << "地址\t" << abs->personarry[i].addr << endl;
cout << "\n";
}
}
system("pause");
system("cls");
}
int checkPerson(addreassbooks* abs, string name)//查找联系人
{
for (int i = 0; i < abs->m_Size; i++)//遍历整个电话本
{
if (abs->personarry[i].name == name)//判断联系人的姓名与输入的名字相
{
return i;//相同返回 i
}
}
return -1;//不相同返回 -1
}
void deletePerson(addreassbooks* abs)//删除联系人
{
string dname;
cout << "请输入你要删除的人名" << endl;
cin >> dname;
int ret = checkPerson(abs, dname);
if (ret == -1)
{
cout << "查无此人" << endl;
}
//删除联系人的 *** 作,将指针往后移动一位
else
{
for (int i = ret; i < abs->m_Size; i++)
{
abs->personarry[i] = abs->personarry[i + 1];
}
abs->m_Size--;
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
void findPerson(addreassbooks* abs)//查找联系人
{
string fname;
cout << "请输入要查找的联系人姓名" << endl;
cin >> fname;
int result = checkPerson(abs, fname);
if (result == -1)
{
cout << "查无此人" << endl;
}
//输出要查找的联系人信息
else
{
cout << "姓名\t" << abs->personarry[result].name << endl;
cout << "性别\t" << abs->personarry[result].sex << endl;
cout << "年龄\t" << abs->personarry[result].age << endl;
cout << "电话\t" << abs->personarry[result].phone << endl;
cout << "地址\t" << abs->personarry[result].addr << endl;
}
system("pause");
system("cls");
}
void modifyPerson(addreassbooks* abs)//修改联系人
{
string mname;
cout << "请输入要修改的联系人姓名" << endl;
cin >> mname;
int result = checkPerson(abs, mname);
if (result == -1)
{
cout << "查无此人" << endl;
}
//填入修改之后的信息
else
{
string name;
cout << "请输入姓名" << endl;
cin >> name;
abs->personarry[result].name = name;
string sex;
cout << "请输入性别" << endl;
cin >> sex;
abs->personarry[result].sex = sex;
int age;
cout << "请输入年龄" << endl;
cin >> age;
abs->personarry[result].age = age;
string phone;
cout << "请输入电话" << endl;
cin >> phone;
abs->personarry[result].phone = phone;
string addr;
cout << "请输入地址" << endl;
cin >> addr;
abs->personarry[result].addr = addr;
cout << "修改成功" << endl;
}
system("pause");
system("cls");
}
void cleanPerson(addreassbooks*abs)//逻辑清空
{
abs->m_Size = 0;
cout << "通讯录清空成功!" << endl;
system("pause");
system("cls");
}
int main(void)
{
//创建通讯录结构体变量
addreassbooks abs;
//初始化通讯录中当前人员的个数
abs.m_Size = 0;
int select = 0;
while (1)
{
mainMenu();
cin >> select;
switch (select)
{
case 1://添加联系人
addPerson(&abs);
break;
case 2://显示联系人
printPerson(&abs);
break;
case 3://删除联系人
deletePerson(&abs);
break;
case 4://查找联系人
findPerson(&abs);
break;
case 5://修改联系人
modifyPerson(&abs);
break;
case 6://清空联系人
cleanPerson(&abs);
break;
case 0://退出通讯录
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
default:
break;
}
}
}
程序总结
通过创建这个通讯录管理系统让我们把学过的所有知识都联系在了一起,我感觉这一个通讯录管理系统最重要的就是你要学会熟练的运用结构体和对函数的递归调用。在涉及到对联系人的显示,删除,查找,修改功能时基本上每一步都需要用到查找联系人的函数,先判断联系人是否存在,然后再对其进行 *** 作。
这一个通讯录管理系统是一个很好的例子,要想掌握好一门语言就必须要能熟练的写出这个基础的通讯录管理系统,我们要把这个程序的每一个步骤都弄懂,为更深层次的学习打下坚实的基础。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)