目录
1--功能
利用C++实现通讯录管理系统,系统实现的功能如下:
① 添加联系人;② 显示联系人;③ 删除联系人;④ 查找联系人;
⑤ 修改联系人;⑥ 清空联系人;⑦ 退出通讯录
2--部分功能实现
2-1--整体框架搭建
设计菜单界面、设计联系人结构体、设计通讯录结构体、主函数设计:
# include <iostream>
# include <string>
#define MAX 1000 // 通讯录最大容量
// 菜单界面
void showMenu(){
std::cout << "*****************************" << std::endl;
std::cout << "***** 1. Add contact ********" << std::endl;
std::cout << "***** 2. Show contact *******" << std::endl;
std::cout << "***** 3. Delete contact *****" << std::endl;
std::cout << "***** 4. Find contact *******" << std::endl;
std::cout << "***** 5. Edit contact *******" << std::endl;
std::cout << "***** 6. Clear contact ******" << std::endl;
std::cout << "***** 0. Exit ***************" << std::endl;
std::cout << "*****************************" << std::endl;
}
// ********************
// 设计联系人结构体
struct Person{
std::string Name;
int Sex; // 1 男 2 女
int Age;
std::string Phone;
std::string Addr;
};
// 通讯录结构体
struct Addressbooks{
// 通讯录中保存的联系人数组
struct Person personArray[MAX];
// 通讯录中当前记录联系人的数目
int Size;
};
int main(){
// 创建通讯录结构体变量
Addressbooks abs;
abs.Size = 0;
int select = 0;
while (true){
// 显示菜单
showMenu();
std::cin >> select;
switch (select){
case 1: // 添加联系人
addPerson(&abs);
break;
case 2: // 显示联系人
showPerson(&abs);
break;
case 3: // 删除联系人
deletePerson(&abs);
break;
case 4: // 查找联系人
findPerson(&abs);
break;
case 5: // 修改联系人
modifyPerson(&abs);
break;
case 6: // 清空联系人
clearPerson(&abs);
break;
case 0: // 退出
std::cout << "Welcome Again, See You!" << std::endl;
return 0;
break;
}
}
system("pause");
return 0;
}
2-2--添加联系人
// 添加联系人
void addPerson(struct Addressbooks *abs){
if (abs->Size == MAX){
std::cout << "Contacts are full!" << std::endl;
return;
}
else{
// 添加具体联系人
std::string name; // 姓名
std::cout << "Please enter the name" << std::endl;
std::cin >> name;
abs->personArray[abs->Size].Name = name;
int Sex; // 性别
std::cout << "Please enter the Sex" << std::endl;
while(true){
std::cout << "1 -- Male" << std::endl;
std::cout << "2 -- Female" << std::endl;
std::cin >> Sex;
if (Sex == 1 || Sex == 2){
abs->personArray[abs->Size].Sex = Sex;
break;
}
else{
std::cout << "Wrong input! Please input again!" << std::endl;;
}
}
int Age; // 年龄
std::cout << "Please enter the Age" << std::endl;
std::cin >> Age;
abs->personArray[abs->Size].Age = Age;
std::string Phone; // 电话
std::cout << "Please enter the Phone Number" << std::endl;
std::cin >> Phone;
abs->personArray[abs->Size].Phone = Phone;
std::string Address; // 住址
std::cout << "Please enter the Address" << std::endl;
std::cin >> Address;
abs->personArray[abs->Size].Addr = Address;
// 更新通讯录当前总人数
abs -> Size ++;
std::cout << "Adding Sucessfully!" << std::endl;
// 清屏
system("pause"); // 任意键继续
system("cls"); // 清屏
}
}
2-3--显示联系人
// 显示联系人
void showPerson(struct Addressbooks *abs){
// 通讯录人数为0
if (abs->Size == 0){
std::cout << "Current record is empty!" << std::endl;
}
else{
for(int i = 0; i < abs->Size; i++){
std::cout << "Name: " << abs->personArray[i].Name << "\t";
std::cout << "Sex: " << (abs->personArray[i].Sex == 1 ? "Male":"Female") << "\t";
std::cout << "Age: " << abs->personArray[i].Age << "\t";
std::cout << "Phone: " << abs->personArray[i].Phone << "\t";
std::cout << "Address: " << abs->personArray[i].Addr << std::endl;
}
}
// 清屏
system("pause"); // 任意键继续
system("cls"); // 清屏
}
2-4--删除联系人
// 删除指定联系人
void deletePerson(struct Addressbooks *abs){
std::cout << "Please enter the name to delete" << std::endl;
std::string name;
std::cin >> name;
int ret = isExist(abs, name);
if (ret != -1){
for (int i = ret; i < abs->Size; i++){
abs->personArray[i] = abs->personArray[i + 1];
}
abs->Size--;
std::cout << "Delete the contact Sucessfully!" << std::endl;
}
else{
std::cout << "The contact does not exist!" << std::endl;
}
}
2-5--查找联系人
// 查找联系人
void findPerson(struct Addressbooks *abs){
std::cout << "Please enter the name" << std::endl;
std::string name;
std::cin >> name;
int ret = isExist(abs, name);
if (ret != -1){// 找到联系人
std::cout << "Name: " << abs->personArray[ret].Name << "\t";
std::cout << "Sex: " << (abs->personArray[ret].Sex == 1 ? "Male":"Female") << "\t";
std::cout << "Age: " << abs->personArray[ret].Age << "\t";
std::cout << "Phone: " << abs->personArray[ret].Phone << "\t";
std::cout << "Address: " << abs->personArray[ret].Addr << std::endl;
}
else{
std::cout << "The contact does not exist!" << std::endl;
}
// 清屏
system("pause"); // 任意键继续
system("cls"); // 清屏
}
2-6--修改联系人
//修改联系人
void modifyPerson(struct Addressbooks *abs){
std::cout << "Please enter the name:" << std::endl;
std::string name;
std::cin >> name;
int ret = isExist(abs, name);
if (ret != -1){ // 找到联系人
std::string name; // 姓名
std::cout << "Please enter a value to modify the name: " << std::endl;
std::cin >> name;
abs->personArray[ret].Name = name;
int sex = 0; //性别
std::cout << "1 --- Male" << std::endl;
std::cout << "2 --- Female" << std::endl;
std::cout << "Please enter a value to modify the sex: " << std::endl;
std::cin >> sex;
while(true){
if(sex == 1 || sex == 2){
abs->personArray[ret].Sex = sex;
break;
}
else{
std::cout << "Wrong input, please input again!" << std::endl;
}
}
// 年龄
std::cout << "Please enter a value to modify the age: " << std::endl;
int age = 0;
std::cin >> age;
abs->personArray[ret].Age = age;
// 电话
std::cout << "Please enter a value to modify the phone: " << std::endl;
std::string phone;
std::cin >> phone;
abs->personArray[ret].Phone = phone;
// 住址
std::cout << "Please enter a value to modify the address: " << std::endl;
std::string address;
std::cin >> address;
abs->personArray[ret].Addr = address;
std::cout << "Modify sucessfully!" << std::endl;
}
else{
std::cout << "The contact does not exist!" << std::endl;
}
// 清屏
system("pause"); // 任意键继续
system("cls"); // 清屏
}
2-7--清空联系人
// 清空联系人
void clearPerson(struct Addressbooks *abs){
abs->Size = 0; // 当前通讯录人数设为0,逻辑清空
std::cout << "The contacts have been cleared" << std::endl;
// 清屏
system("pause"); // 任意键继续
system("cls"); // 清屏
}
3--功能整体实现
# include <iostream>
# include <string>
#define MAX 1000 // 通讯录最大容量
// 菜单界面
void showMenu(){
std::cout << "*****************************" << std::endl;
std::cout << "***** 1. Add contact ********" << std::endl;
std::cout << "***** 2. Show contact *******" << std::endl;
std::cout << "***** 3. Delete contact *****" << std::endl;
std::cout << "***** 4. Find contact *******" << std::endl;
std::cout << "***** 5. Edit contact *******" << std::endl;
std::cout << "***** 6. Clear contact ******" << std::endl;
std::cout << "***** 0. Exit ***************" << std::endl;
std::cout << "*****************************" << std::endl;
}
// ********************
// 设计联系人结构体
struct Person{
std::string Name;
int Sex; // 1 男 2 女
int Age;
std::string Phone;
std::string Addr;
};
// 通讯录结构体
struct Addressbooks{
// 通讯录中保存的联系人数组
struct Person personArray[MAX];
// 通讯录中当前记录联系人的数目
int Size;
};
// *************
// 添加联系人
void addPerson(struct Addressbooks *abs){
if (abs->Size == MAX){
std::cout << "Contacts are full!" << std::endl;
return;
}
else{
// 添加具体联系人
std::string name; // 姓名
std::cout << "Please enter the name" << std::endl;
std::cin >> name;
abs->personArray[abs->Size].Name = name;
int Sex; // 性别
std::cout << "Please enter the Sex" << std::endl;
while(true){
std::cout << "1 -- Male" << std::endl;
std::cout << "2 -- Female" << std::endl;
std::cin >> Sex;
if (Sex == 1 || Sex == 2){
abs->personArray[abs->Size].Sex = Sex;
break;
}
else{
std::cout << "Wrong input! Please input again!" << std::endl;;
}
}
int Age; // 年龄
std::cout << "Please enter the Age" << std::endl;
std::cin >> Age;
abs->personArray[abs->Size].Age = Age;
std::string Phone; // 电话
std::cout << "Please enter the Phone Number" << std::endl;
std::cin >> Phone;
abs->personArray[abs->Size].Phone = Phone;
std::string Address; // 住址
std::cout << "Please enter the Address" << std::endl;
std::cin >> Address;
abs->personArray[abs->Size].Addr = Address;
// 更新通讯录当前总人数
abs -> Size ++;
std::cout << "Adding Sucessfully!" << std::endl;
// 清屏
system("pause"); // 任意键继续
system("cls"); // 清屏
}
}
//************
// 显示联系人
void showPerson(struct Addressbooks *abs){
// 通讯录人数为0
if (abs->Size == 0){
std::cout << "Current record is empty!" << std::endl;
}
else{
for(int i = 0; i < abs->Size; i++){
std::cout << "Name: " << abs->personArray[i].Name << "\t";
std::cout << "Sex: " << (abs->personArray[i].Sex == 1 ? "Male":"Female") << "\t";
std::cout << "Age: " << abs->personArray[i].Age << "\t";
std::cout << "Phone: " << abs->personArray[i].Phone << "\t";
std::cout << "Address: " << abs->personArray[i].Addr << std::endl;
}
}
// 清屏
system("pause"); // 任意键继续
system("cls"); // 清屏
}
//********************
// 删除联系人
// 检测联系人是否存在
int isExist(struct Addressbooks *abs, std::string name){
for (int i = 0; i < abs->Size; i++){
if(abs->personArray[i].Name == name){
return i; // 找到,返回索引下标
}
}
return -1; // 未找到
}
// 删除指定联系人
void deletePerson(struct Addressbooks *abs){
std::cout << "Please enter the name to delete" << std::endl;
std::string name;
std::cin >> name;
int ret = isExist(abs, name);
if (ret != -1){
for (int i = ret; i < abs->Size; i++){
abs->personArray[i] = abs->personArray[i + 1];
}
abs->Size--;
std::cout << "Delete the contact Sucessfully!" << std::endl;
}
else{
std::cout << "The contact does not exist!" << std::endl;
}
}
//****************
// 查找联系人
void findPerson(struct Addressbooks *abs){
std::cout << "Please enter the name" << std::endl;
std::string name;
std::cin >> name;
int ret = isExist(abs, name);
if (ret != -1){// 找到联系人
std::cout << "Name: " << abs->personArray[ret].Name << "\t";
std::cout << "Sex: " << (abs->personArray[ret].Sex == 1 ? "Male":"Female") << "\t";
std::cout << "Age: " << abs->personArray[ret].Age << "\t";
std::cout << "Phone: " << abs->personArray[ret].Phone << "\t";
std::cout << "Address: " << abs->personArray[ret].Addr << std::endl;
}
else{
std::cout << "The contact does not exist!" << std::endl;
}
// 清屏
system("pause"); // 任意键继续
system("cls"); // 清屏
}
//*********
//修改联系人
void modifyPerson(struct Addressbooks *abs){
std::cout << "Please enter the name:" << std::endl;
std::string name;
std::cin >> name;
int ret = isExist(abs, name);
if (ret != -1){ // 找到联系人
std::string name; // 姓名
std::cout << "Please enter a value to modify the name: " << std::endl;
std::cin >> name;
abs->personArray[ret].Name = name;
int sex = 0; //性别
std::cout << "1 --- Male" << std::endl;
std::cout << "2 --- Female" << std::endl;
std::cout << "Please enter a value to modify the sex: " << std::endl;
std::cin >> sex;
while(true){
if(sex == 1 || sex == 2){
abs->personArray[ret].Sex = sex;
break;
}
else{
std::cout << "Wrong input, please input again!" << std::endl;
}
}
// 年龄
std::cout << "Please enter a value to modify the age: " << std::endl;
int age = 0;
std::cin >> age;
abs->personArray[ret].Age = age;
// 电话
std::cout << "Please enter a value to modify the phone: " << std::endl;
std::string phone;
std::cin >> phone;
abs->personArray[ret].Phone = phone;
// 住址
std::cout << "Please enter a value to modify the address: " << std::endl;
std::string address;
std::cin >> address;
abs->personArray[ret].Addr = address;
std::cout << "Modify sucessfully!" << std::endl;
}
else{
std::cout << "The contact does not exist!" << std::endl;
}
// 清屏
system("pause"); // 任意键继续
system("cls"); // 清屏
}
//************
// 清空联系人
void clearPerson(struct Addressbooks *abs){
abs->Size = 0; // 当前通讯录人数设为0,逻辑清空
std::cout << "The contacts have been cleared" << std::endl;
// 清屏
system("pause"); // 任意键继续
system("cls"); // 清屏
}
//************
int main(){
// 创建通讯录结构体变量
Addressbooks abs;
abs.Size = 0;
int select = 0;
while (true){
// 显示菜单
showMenu();
std::cin >> select;
switch (select){
case 1: // 添加联系人
addPerson(&abs);
break;
case 2: // 显示联系人
showPerson(&abs);
break;
case 3: // 删除联系人
deletePerson(&abs);
break;
case 4: // 查找联系人
findPerson(&abs);
break;
case 5: // 修改联系人
modifyPerson(&abs);
break;
case 6: // 清空联系人
clearPerson(&abs);
break;
case 0: // 退出
std::cout << "Welcome Again, See You!" << std::endl;
return 0;
break;
}
}
system("pause");
return 0;
}