使用C++中的简单结构,来编写通讯录管理系统。
Windows 上的安装:
Windows 上的安装:Visual Studio Community 2019。
为了在 Windows 上安装 GCC,您需要安装 MinGW。为了安装 MinGW,请访问 MinGW 的主页 mingw-w64.org,进入 MinGW 下载页面,下载最新版本的 MinGW 安装程序,命名格式为 MinGW-.exe。
当安装 MinGW 时,您至少要安装 gcc-core、gcc-g++、binutils 和 MinGW runtime,但是一般情况下都会安装更多其他的项。
添加您安装的 MinGW 的 bin 子目录到您的 PATH 环境变量中,这样您就可以在命令行中通过简单的名称来指定这些工具。
当完成安装时,您可以从 Windows 命令行上运行 gcc、g++、ar、ranlib、dlltool 和其他一些 GNU 工具。
代码所涉及的主题框架有两个:1,结构体的定义、2函数体的创建。
使用 Visual Studio (Graphical Interface) 编译
1、下载及安装 Visual Studio Community 2019。
官网:https://visualstudio.microsoft.com/zh-hans/
2、打开 Visual Studio Community
3、点击 File -> New -> Project
通讯录实现效果
链接: link.zhang@ikeli_ichen
1,头文件操作:
2,创建显示操作:
3,创建人的结构体
4,创建通讯录结构体
5,添加人员信息

6,显示通讯录所有人员信息
7,判断查找的人是否存在
8,删除指定的联系人
9,查找指定的联系人
10,修改指定的联系人


11,清空所有的数据
12,系统测试

以下是整个代码:
#include <iostream>
#include <string>
#define MAX 100//设置通讯录最大储存容量
using namespace std;
//显示提示操作
void showMenu()
{
cout << "=======通讯录管理系统=======" << endl;
cout << "\t1,<添加联系人>" << endl;
cout << "\t2,<显示联系人>" << endl;
cout << "\t3,<删除联系人>" << endl;
cout << "\t4,<查找联系人>" << endl;
cout << "\t5,<修改联系人>" << endl;
cout << "\t6,<清空联系人>" << endl;
cout << "\t0,<退出系统!>" << endl;
cout << "=============================" << endl;
}
//创建人的结构体
struct Person
{
string m_Name;
int m_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
//创建通讯录结构体
struct AdressBooks
{
struct Person personArray[MAX];
int m_size;//记录联系人个数
};
//添加人员信息
void addPerson(AdressBooks *abs)
{
if (abs->m_size == MAX)
{
cout << "当前通讯录已满,不可添加!" << endl;
return;
}
else
{
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->m_size].m_Name = name;
while (true)
{
int sex;
cout << "请输入性别(1->男,2->女)" << endl;
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_size].m_Sex = sex;
break;
}
else
{
cout << "你输入有误,请重新输入!" << endl;
}
}
int age;
cout << "请输入你的年龄:" << endl;
cin >> age;
abs->personArray[abs->m_size].m_Age = age;
string phone;
cout << "请输入你的手机号码:" << endl;
cin >> phone;
abs->personArray[abs->m_size].m_Phone = phone;
string address;
cout << "请输入你的地址:" << endl;
cin >> address;
abs->personArray[abs->m_size].m_Addr = address;
abs->m_size++;
cout << "==========================" << endl;
cout << "你已成功添加联系人信息!" << endl;
system("pause");//请按任意键
system("cls");//清屏操作
}
}
//显示通讯录所有人员信息
void showPerson(AdressBooks *abs)
{
if (abs->m_size == 0)
{
cout << "当前没有任何联系人信息" << endl;
}
else
{
cout << "通讯录信息有:" << endl;
for (int i = 0; i < abs->m_size; i++)
{
cout << "名字:" << abs->personArray[i].m_Name
<< "\t性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女")
<< "\t年龄:" << abs->personArray[i].m_Age
<< "\t号码:" << abs->personArray[i].m_Phone
<< "\t住址:" << abs->personArray[i].m_Addr << endl;
}
}
system("pause");
system("cls");
}
//判断查找的人是否存在
int isEsist(AdressBooks* abs, string name)
{
for (int i = 0; i < abs->m_size; i++)
{
if (name == abs->personArray[i].m_Name)
{
return i;
}
}
return -1;
}
//删除指定的联系人
void deletePerson(AdressBooks *abs)
{
cout << "请输入要删除的名字:" << endl;
string name;
cin >> name;
int ret = isEsist(abs,name);
if (ret != -1)
{
for (int i = ret; i < abs->m_size; i++)
{
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_size--;
cout << "删除成功!" << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
//查找指定的联系人
void findPerson(AdressBooks *abs)
{
cout << "请输入要查找的联系人" << endl;
string name;
cin >> name;
int ret = isEsist(abs, name);
if (ret != -1)
{
cout << "名字:" << abs->personArray[ret].m_Name
<< "\t性别:" << (abs->personArray[ret].m_Sex == 1 ? "男" : "女")
<< "\t年龄:" << abs->personArray[ret].m_Age
<< "\t号码:" << abs->personArray[ret].m_Phone
<< "\t住址:" << abs->personArray[ret].m_Addr << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
//修改指定的联系人
void modifyPerson(AdressBooks* abs)
{
cout << "请输入要修改的联系人" << endl;
string name;
cin >> name;
int ret = isEsist(abs, name);
if (ret != -1)
{
cout << "请输入要更改的名字:" << endl;
string e_name;
cin >> e_name;
abs->personArray[ret].m_Name = e_name;
while (true)
{
int e_sex;
cout << "请输入更改的性别(1->男,2->女)" << endl;
cin >> e_sex;
if (e_sex == 1 || e_sex == 2)
{
abs->personArray[ret].m_Sex = e_sex;
break;
}
else
{
cout << "你输入有误,请重新输入!" << endl;
}
}
cout << "请输入要更改的年龄:" << endl;
int e_age;
cin >> e_age;
abs->personArray[ret].m_Age = e_age;
cout << "请输入要更改的号码:" << endl;
string e_phone;
cin >> e_phone;
abs->personArray[ret].m_Phone = e_phone;
cout << "请输入要更改的地址:" << endl;
string e_addr;
cin >> e_addr;
abs->personArray[ret].m_Addr = e_addr;
cout << "更改成功!" << endl;
}
else
{
cout << "查无此人!" << endl;
}
system("pause");
system("cls");
}
//清空所有的数据
void cleanPerson(AdressBooks* abs)
{
abs->m_size = 0;
cout << "通讯录已清空!" << endl;
system("pause");
system("cls");
}
//系统测试
int main()
{
AdressBooks abs;//创建通讯录变量
abs.m_size = 0;//当前人员个数
while(true)
{
int select;
showMenu();
cout << "请输入你选择的数字:" << endl;
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:
cleanPerson(&abs);
break;
case 0:
cout << "<==========退出系统!==========>" << endl;
return 0;
default:
break;
}
}
system("pause");
return 0;
}
版权声明:本文为weixin_47195300原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。