1:通讯录设计要点
? ? 1:添加联系人:向通讯录中添加新人(包括:性别,年龄,联系电话,家庭住址),并且最多记录1000人 ? ? 2:显示联系人:显示通讯录中所有联系人信息 ? ? 3:删除联系人:按照姓名进行删除指定联系人 ? ? 4:查找联系人:按照姓名查找联系人 ? ? 5:修改联系人:按照姓名修改联系人 ? ? 6:清空联系人:按照姓名清空联系人 ? ? 7:退出通讯录:退出当前使用的通讯录
2:设计思路
/**
本教程主要利用C++来实现一个通讯管理系统,系统中需要实现如下功能:
1:添加联系人:向通讯录中添加新人(包括:性别,年龄,联系电话,家庭住址),并且最多记录1000人
2:显示联系人:显示通讯录中所有联系人信息
3:删除联系人:按照姓名进行删除指定联系人
4:查找联系人:按照姓名查找联系人
5:修改联系人:按照姓名修改联系人
6:清空联系人:按照姓名清空联系人
7:退出通讯录:退出当前使用的通讯录
*/
// 引入C++标准包
#include <iostream>
#include <string>
// #define MAX_NUMBER 2
using namespace std;
// const int MAX_NUMBER2 = 3;
// 定义常量通讯录最大值 (auto 让编译其自己推断变量的类型)
constexpr auto MAX = 3;
// 定义联系人结构体
struct Person
{
string name;
int sex;
int age;
string phoneNamer;
string address;
};
struct addressbook
{
struct Person perArray[MAX];
// struct Person personArr[MAX_NUMBER2];
// struct Person personArr[MAX_NUMBER];
int person_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;
}
int isExist(addressbook* personBook , string name) {
for (int i = 0; i < personBook->person_size;i++)
{
if (personBook->perArray[i].name == name)
{
// 从通讯录中找到了某个人
return i;
}
}
return -1;
}
void addPerson(addressbook *addBook) {
if (addBook->person_size < MAX)
{
cout << "请输入姓名:" << endl;
string name;
cin >> name;
// addBook 操作指针指向的哪个对象
addBook->perArray[addBook->person_size].name = name;
cout << "请输入性别对应的序号:1--男 2---女" << endl;
int sex;
// 通过while死循环持续性的读取用户输入的性别
while(true) {
cin >> sex;
if ((sex ==1) || (sex ==2))
{
addBook->perArray[addBook->person_size].sex = sex;
break;
}
else
{
cout << "您输入的信息有误,请重新出入" << endl;
}
}
int age = 0;
cout << "请输入年龄" << endl;
cin >> age;
addBook->perArray[addBook->person_size].age = age;
string namuber;
cout << "请输入电话号码:" << endl;
cin >> namuber;
addBook->perArray[addBook->person_size].phoneNamer = namuber;
string address;
cout << "请输入地址" << endl;
cin >> address;
addBook->perArray[addBook->person_size].address = address;
// 联系人添加成功
addBook->person_size++;
cout << "联系人已成功添加到通讯录" << endl;
}
else
{
cout << "通讯录联系人已满,请删除部分联系人再添加!" << endl;
}
system("pause");
system("cls");
}
// 显示联系人
void showPerson(addressbook person) {
if (person.person_size == 0) {
cout << "您的通讯录列表为空" << endl;
}
else
{
for (int i = 0; i < person.person_size; i++)
{
cout << "序号:" << i + 1 << ": "
<< "姓名: " << person.perArray[i].name << ": "
<< "性别: " << person.perArray[i].sex << ": "
<< "年龄: " << person.perArray[i].age << ": "
<< "电话: " << person.perArray[i].phoneNamer << ": "
<< "住址: " << person.perArray[i].address << " "
<< endl;
}
}
system("pause");
system("cls");
}
// 删除联系人
void deletePerson(addressbook* person) {
string name;
cout << "请输入您要删除的联系人姓名:" << endl;
cin >> name;
int isExis = isExist(person, name);
if (isExis != -1)
{
for (int i = isExis; i < person->person_size; i++)
{
person->perArray[i] = person->perArray[i + 1];
}
person->person_size--;
cout << "删除成功" << endl;
}
else
{
cout << "对不起,通讯录没有此人" << endl;
}
system("pause");
system("cls");
}
// 查找联系人
void findPerson(addressbook* address) {
string name;
cout << "请输入您想要查找的联系人" << endl;
cin >> name;
int exist = isExist(address, name);
if (exist != -1)
{
cout << "该联系人信息如下:" << endl;
cout << "姓名: " << address->perArray[exist].name << " "
<< "性别: " << address->perArray[exist].sex << " "
<< "年龄: " << address->perArray[exist].age << " "
<< "电话: " << address->perArray[exist].phoneNamer << " "
<< "住址: " << address->perArray[exist].address << " " << endl;
}
else
{
cout << "查无此人喔!" << endl;
}
system("pause");
system("cls");
}
void modifyPerson(addressbook *person) {
string modifyName;
cout << "请输入修改后的姓名: " << endl;
cin >> modifyName;
int exist = isExist(person, modifyName);
if (exist != -1)
{
person->perArray[exist].name = modifyName;
while (true)
{
int modifySex;
cout << "请输入修改后的性别: (1:男 2:女) " << endl;
cin >> modifySex;
if (modifySex == 1 || modifySex ==2)
{
person->perArray[exist].sex = modifySex;
break;
}
else
{
cout << "您应当输入1或者2,请重新输入" << endl;
}
}
int modifyAge;
cout << "请输入修改后的年龄: ";
cin >> modifyAge;
person->perArray[exist].age = modifyAge;
string modifyPhoneNum;
cout << "请输入修改后的电话: ";
cin >> modifyPhoneNum;
person->perArray[exist].phoneNamer = modifyPhoneNum;
string modifyAddress;
cout << "请输入修改后的地址: ";
cin >> modifyAddress;
person->perArray[exist].address = modifyAddress;
cout << "修改成功!" << endl;
}
else
{
cout << "查无此人,故无法修改" << endl;
}
system("pause");
system("cls");
}
// 清空通讯录
void clearPersonAddress(addressbook *personBook) {
string ensure;
cout << "您确定要是清空所有联系人信息吗?注意此操作不可逆,请谨慎操作,请输入\"我同意\" " << endl;
cin >> ensure;
if (ensure == "我同意")
{
personBook->person_size = 0;
for (int i = 0; i < personBook->person_size; i++)
{
personBook->perArray[i].address = "";
personBook->perArray[i].name = "";
personBook->perArray[i].phoneNamer = "";
personBook->perArray[i].age =0;
personBook->perArray[i].sex =0;
}
cout << "已成功清空通讯录列表" << endl;
}
else
{
cout << "撤销清空联系人列表" << endl;
}
system("pause");
system("cls");
}
int main()
{
std::cout << "通许录管理系统 \n";
struct addressbook address;
address.person_size = 0;
int userSelect = -1;
while (true)
{
showMenu();
cout << "请在下方输入您向选择的功能(输入下面数字即可)" << endl;
cin >> userSelect;
switch (userSelect)
{
case 1:
addPerson(&address);
break;
case 2:
showPerson(address);
break;
case 3:
deletePerson(&address);
break;
case 4:
findPerson(&address);
break;
case 5:
modifyPerson(&address);
break;
case 6:
clearPersonAddress(&address);
break;
case 0:
cout << "退出系统成功,欢迎您下次使用!" << endl;
return 0;
default:
system("pause");
break;
}
}
}
3:运行结果
?
?
|