1、题目详情
通讯录是一种工具,用于记录亲人、朋友的联系信息。
这个教程旨在使用C++来开发一个通讯录管理系统,其中包括以下功能:
- 添加联系人:向通讯录中添加新的联系人,包括姓名、性别、年龄、联系电话、家庭住址。最多可以记录1000人。
- 显示联系人:展示通讯录中所有联系人的信息。
- 删除联系人:按照姓名删除指定联系人。
- 查找联系人:按照姓名查找指定联系人的信息。
- 修改联系人:按照姓名修改指定联系人的信息。
- 清空联系人:清空通讯录中所有的联系人信息。
- 退出通讯录:退出当前通讯录的使用。
2、系统设计(主要函数)
2.1结构体的创建
题目要求需要使用结构体来存储学生信息,因此需要创建一个结构体来定义学生的信息。在结构体中,要包含学生的姓名、性别、年龄、电话号码和地址等信息。
struct Preson {
string name;
int sex;
int age;
string Phone;
string addr;
};
struct Addressbooks {
struct Preson personAddressbooks[max];
int size;
};
2.2、main函数
这段代码是一个简单的主函数,其中调用了两个函数:showMenu() 和 choice()。通过showMenu()函数,程序展示了一个菜单,为用户提供了不同的选择。而通过choice()函数,程序引导用户做出具体的选择。最后,函数返回0,表示程序顺利执行结束。尽管代码很简洁,但它是整个程序的核心,负责引导用户进入程序的主要功能。
int main()
{
showMenu();
Addressbooks abs;
abs.size = 0;
int select;
cout << "请输入你选择的功能:";
cin >> select;
while (select)
{
switch (select)
{
case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
deletePreson(&abs);
break;
case 4:
findPerson(&abs);
break;
case 5:
modifyPerson(&abs);
break;
case 6:
cleanPerson(&abs);
break;
case 0:
cout << "欢迎下次使用!" << endl;
system("pause");
select = 0;
break;
default:
break;
}
system("pause");
system("cls");
showMenu();
cout << "请输入你选择的功能:";
cin >> select;
}
return 0;
}
2.3showMenu函数
这段代码定义了一个名为showMenu()的函数,用于展示一个通讯录管理系统的菜单界面,包括各种操作选项。
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;
}
2.4、addPerson函数
这段代码定义了一个名为addPerson()的函数,用于向通讯录中添加新的联系人信息。函数首先检查通讯录是否已满,如果已满则提示无法添加,否则依次要求用户输入姓名、性别、年龄、地址和电话信息,并将这些信息存储到通讯录中。最后,通讯录的大小增加,并输出添加成功的提示信息。这个函数实现了向通讯录中添加新的联系人信息的功能。
void addPerson(Addressbooks* abs)
{
if (abs->size == max)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
cout << "请输入姓名:";
string name;
cin >> name;
abs->personAddressbooks[abs->size].name = name;
cout << "请输入性别(1-男 2-女):" ;
// cout << "1-----男" << endl;
//cout << "2-----女" << endl;
int sexInput = 0;
while (true)
{
cin >> sexInput;
if (sexInput == 1 || sexInput == 2)
{
abs->personAddressbooks[abs->size].sex = sexInput;
break;
}
else
{
cout << "输入有误,请重新输入:" ;
}
}
cout << "请输入年龄:";
cin >> abs->personAddressbooks[abs->size].age;
cout << "请输入地址:";
cin >> abs->personAddressbooks[abs->size].addr;
cout << "请输入电话:";
cin >> abs->personAddressbooks[abs->size].Phone;
abs->size++;
cout << "添加成功" << endl;
}
}
2.5、showPerson函数
这段代码定义了一个名为showPerson()的函数,用于展示通讯录中存储的联系人信息。如果通讯录为空,则输出“当前记录为空”;否则,遍历通讯录中的每个联系人,依次输出其姓名、性别、年龄、电话和地址信息。这个函数实现了展示通讯录中联系人信息的功能。
void showPerson(Addressbooks* abs)
{
if (abs->size == 0)
{
cout << "当前记录为空" << endl;
}
else
{
for (int i = 0; i < abs->size; i++)
{
cout << "姓名:" << abs->personAddressbooks[i].name << "\t";
cout << "性别:" << (abs->personAddressbooks[i].sex == 1 ? "男":"女") << "\t";
cout << "年龄:" << abs->personAddressbooks[i].age << "\t";
cout << "电话:" << abs->personAddressbooks[i].Phone << "\t";
cout << "住址:" << abs->personAddressbooks[i].addr << endl;
}
}
}
2.6、deletePreson函数
这段代码定义了一个名为deletePerson()的函数,用于从通讯录中删除指定联系人信息。函数首先要求用户输入要删除的联系人姓名,然后检查是否存在该联系人。如果存在,则将该联系人后面的信息向前移动一个位置,最后将通讯录大小减一表示删除成功。
void deletePreson(Addressbooks* abs)
{
cout << "请输入你要删除的联系人:" ;
string name;
cin >> name;
int ret = isExist(abs,name);
if (ret == -1)
cout << "未查到此人" << endl;
else
{
for (int i = ret; i < abs->size; i++)
{
abs->personAddressbooks[i] = abs->personAddressbooks[i + 1];
}
abs->size--;
cout << "删除成功" << endl;
}
}
2.7、findPerson函数
这段代码定义了一个名为findPerson()的函数,用于在通讯录中查找指定联系人信息。用户输入要查找的联系人姓名,函数检查是否存在该联系人并输出其信息,否则输出“查无此人”。
void findPerson(Addressbooks* abs)
{
cout << "请输入要查找的联系人:";
string name;
cin >> name;
int ret = isExist(abs,name);
if (ret != -1)
{
cout << "姓名:" << abs->personAddressbooks[ret].name << "\t";
cout << "性别:" << abs->personAddressbooks[ret].sex << "\t";
cout << "年龄:" << abs->personAddressbooks[ret].age << "\t";
cout << "电话:" << abs->personAddressbooks[ret].Phone << "\t";
cout << "住址:" << abs->personAddressbooks[ret].addr << endl;
}
else
cout << "查无此人" << endl;
}
2.8、modifyPerson函数
这段代码定义了一个名为modifyPerson()的函数,用于修改通讯录中指定联系人的信息。用户输入要修改的联系人姓名,然后选择要修改的信息类型并输入新的信息进行修改。如果找到该联系人,则根据用户选择更新相应的信息;否则输出“查无此人”
void modifyPerson(Addressbooks* abs)
{
cout << "请输入要修改的联系人:";
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
int num;
cout << "请输入要修改人的信息(1.姓名 2.性别 3.年龄 4.电话 5.住址):" << endl;
cin >> num;
switch (num)
{
case 1:
{cout << "请输入修改后的姓名:";
string newName;
cin >> newName;
abs->personAddressbooks[ret].name = newName;
break; }
case 2:
{ cout << "请输入要修改的性别(1-男 2-女):";
//cout << "1-----男" << endl;
//cout << "2-----女" << endl;
int sexInput = 0;
while (true)
{
cin >> sexInput;
if (sexInput == 1 || sexInput == 2)
{
abs->personAddressbooks[ret].sex = sexInput;
break;
}
else
{
cout << "输入有误,请重新输入:" ;
}
}
break; }
case 3:
{cout << "请输入要修改的年龄:";
int age;
cin >> age;
abs->personAddressbooks[ret].age = age;
break; }
case 4:
{ cout << "请输入要修改的电话:";
int phone;
cin >> phone;
abs->personAddressbooks[ret].Phone = phone;
break; }
case 5:
{ cout << "请输入要修改的住址:";
string address;
cin >> address;
abs->personAddressbooks[ret].addr = address;
break; }
default:
cout << "输入有误,请重新输入:" << endl;
break;
}
}
else
cout << "查无此人" << endl;
}
2.9、celanPerson函数
这段代码定义了一个名为cleanPerson()的函数,用于清空整个通讯录。函数将通讯录中的联系人数量设为0,表示通讯录已经被清空。输出消息“通讯录已清空”。
void cleanPerson(Addressbooks* abs)
{
abs->size = 0;
cout << "通讯录已清空" << endl;
}
3、整个程序代码
#include <iostream>
#include <string>
#define max 1000
using namespace std;
struct Preson {
string name;
int sex;
int age;
string Phone;
string addr;
};
struct Addressbooks {
struct Preson personAddressbooks[max];
int 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->size == max)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
cout << "请输入姓名:";
string name;
cin >> name;
abs->personAddressbooks[abs->size].name = name;
cout << "请输入性别(1-男 2-女):" ;
// cout << "1-----男" << endl;
//cout << "2-----女" << endl;
int sexInput = 0;
while (true)
{
cin >> sexInput;
if (sexInput == 1 || sexInput == 2)
{
abs->personAddressbooks[abs->size].sex = sexInput;
break;
}
else
{
cout << "输入有误,请重新输入:" ;
}
}
cout << "请输入年龄:";
cin >> abs->personAddressbooks[abs->size].age;
cout << "请输入地址:";
cin >> abs->personAddressbooks[abs->size].addr;
cout << "请输入电话:";
cin >> abs->personAddressbooks[abs->size].Phone;
abs->size++;
cout << "添加成功" << endl;
}
}
void showPerson(Addressbooks* abs)
{
if (abs->size == 0)
{
cout << "当前记录为空" << endl;
}
else
{
for (int i = 0; i < abs->size; i++)
{
cout << "姓名:" << abs->personAddressbooks[i].name << "\t";
cout << "性别:" << (abs->personAddressbooks[i].sex == 1 ? "男":"女") << "\t";
cout << "年龄:" << abs->personAddressbooks[i].age << "\t";
cout << "电话:" << abs->personAddressbooks[i].Phone << "\t";
cout << "住址:" << abs->personAddressbooks[i].addr << endl;
}
}
}
int isExist(Addressbooks *abs,string name)
{
for (int i = 0; i < abs->size; i++)
{
if (abs->personAddressbooks[i].name == name)
{
return i;
}
}
return -1;
}
void deletePreson(Addressbooks* abs)
{
cout << "请输入你要删除的联系人:" ;
string name;
cin >> name;
int ret = isExist(abs,name);
if (ret == -1)
cout << "未查到此人" << endl;
else
{
for (int i = ret; i < abs->size; i++)
{
abs->personAddressbooks[i] = abs->personAddressbooks[i + 1];
}
abs->size--;
cout << "删除成功" << endl;
}
}
void findPerson(Addressbooks* abs)
{
cout << "请输入要查找的联系人:";
string name;
cin >> name;
int ret = isExist(abs,name);
if (ret != -1)
{
cout << "姓名:" << abs->personAddressbooks[ret].name << "\t";
cout << "性别:" << abs->personAddressbooks[ret].sex << "\t";
cout << "年龄:" << abs->personAddressbooks[ret].age << "\t";
cout << "电话:" << abs->personAddressbooks[ret].Phone << "\t";
cout << "住址:" << abs->personAddressbooks[ret].addr << endl;
}
else
cout << "查无此人" << endl;
}
void modifyPerson(Addressbooks* abs)
{
cout << "请输入要修改的联系人:";
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
int num;
cout << "请输入要修改人的信息(1.姓名 2.性别 3.年龄 4.电话 5.住址):" << endl;
cin >> num;
switch (num)
{
case 1:
{cout << "请输入修改后的姓名:";
string newName;
cin >> newName;
abs->personAddressbooks[ret].name = newName;
break; }
case 2:
{ cout << "请输入要修改的性别(1-男 2-女):";
//cout << "1-----男" << endl;
//cout << "2-----女" << endl;
int sexInput = 0;
while (true)
{
cin >> sexInput;
if (sexInput == 1 || sexInput == 2)
{
abs->personAddressbooks[ret].sex = sexInput;
break;
}
else
{
cout << "输入有误,请重新输入:" ;
}
}
break; }
case 3:
{cout << "请输入要修改的年龄:";
int age;
cin >> age;
abs->personAddressbooks[ret].age = age;
break; }
case 4:
{ cout << "请输入要修改的电话:";
int phone;
cin >> phone;
abs->personAddressbooks[ret].Phone = phone;
break; }
case 5:
{ cout << "请输入要修改的住址:";
string address;
cin >> address;
abs->personAddressbooks[ret].addr = address;
break; }
default:
cout << "输入有误,请重新输入:" << endl;
break;
}
}
else
cout << "查无此人" << endl;
}
void cleanPerson(Addressbooks* abs)
{
abs->size = 0;
cout << "通讯录已清空" << endl;
}
int main()
{
showMenu();
Addressbooks abs;
abs.size = 0;
int select;
cout << "请输入你选择的功能:";
cin >> select;
while (select)
{
switch (select)
{
case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
deletePreson(&abs);
break;
case 4:
findPerson(&abs);
break;
case 5:
modifyPerson(&abs);
break;
case 6:
cleanPerson(&abs);
break;
case 0:
cout << "欢迎下次使用!" << endl;
system("pause");
select = 0;
break;
default:
break;
}
system("pause");
system("cls");
showMenu();
cout << "请输入你选择的功能:";
cin >> select;
}
return 0;
}
4、运行截图展示(部分)
最后
完成这个系统的过程确实需要花费大量时间和精力,涉及到大量的代码编写和逻辑设计。感谢您的辛勤付出和分享。如果您觉得这个系统对您有帮助,欢迎点赞、收藏,留言反馈。如果您有任何问题或需要进一步帮助,也欢迎私信我,我会尽力回复您的。感谢您的支持和理解!