这个通讯录管理系统是我听课后做的笔记,都是很基础的逻辑实现,第一次动手写了一个小案例感觉找到了一点方向,也希望能帮到一点忙!
通讯录是一个可以记录亲人、好友信息的工具。 本教程主要利用C++来实现一个通讯录管理系统 系统中需要实现的功能如下:
添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人 显示联系人:显示通讯录所有联系人信息
删除联系人:按照姓名进行删除指定联系人 查找联系人:按照姓名查看指定联系人信息 修改联系人:按照姓名重新修改指定联系人
清空联系人:清空通讯录中所有信息 退出通讯录:退出当前使用的通讯录
封装函数显示该界面,如:void showMenu() 在main函数中调用封装好的函数
void mean()
{
cout << "1、创建联系人" << endl;
cout << "2、显示练习人" << endl;
cout << "3、删除练习人" << endl;
cout << "4、查找联系人" << endl;
cout << "5、修改联系人" << endl;
cout << "6、清空联系人" << endl;
cout << "0、退出通讯录" << endl;
}
思路:根据用户不同的选择,进入不同的功能,可以选择switch分支结构,将整个架构进行搭建
当用户选择0的时候,执行退出,选择其他先不做操作,也不会退出程序
cout << "欢迎下次使用系统" << endl;
system("pause");
return 0;
添加联系人实现步骤:
设计联系人结构体
设计通讯录结构体
main函数中创建通讯录
封装添加联系人函数
测试添加联系人功能
5.1 设计联系人结构体
联系人信息包括:姓名、性别、年龄、联系电话、家庭住址
void addPerson(Arraybooks *abs)
{
if(abs -> Size == MAX){
cout <<"通讯录已满" << endl;
return;
}
else{
string name;
cout << "输入姓名" << endl;
cin >> name;
abs->ArrayList[abs->Size].name = name;
cout << "输入性别" << endl;
cout <<"1---男" << endl;
cout <<"2---女" <<endl;
int sex;
while(-1){
cin >> sex;
if(sex == 1 || sex == 2) {
abs->ArrayList[abs->Size].Sex = sex;
break;
}
cout << "输入有误,请重新输入";
}
cout << "请输入年龄" << endl;
int age = 0;
cin >> age;
abs->ArrayList[abs->Size].old = age;
cout << "请输入电话:" << endl;
string phone = " ";
cin >> phone;
abs->ArrayList[abs->Size].phone = phone;
cout << "请输入地址" << endl;
string address;
cin >> address;
abs->ArrayList[abs->Size].address = address;
abs->Size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
}
功能描述:显示通讯录中已有的联系人信息
显示联系人实现步骤:
封装显示联系人函数
测试显示联系人功能
oid showPerson(Arraybooks *abs)
{
if(abs->Size == 0){
cout << "记录为空" << endl;
}
else{
for(int i = 0;i < abs->Size;i++){
cout << "姓名:" << abs->ArrayList[i].name << "\t";
cout << "性别:" << (abs->ArrayList[i].Sex == 1?"男":"女") << "\t";
cout << "年龄: " << abs->ArrayList[i].old << "\t";
cout << "电话:" <<abs->ArrayList[i].phone << "\t";
cout << "地址: " << abs->ArrayList[i].address << endl;
}
}
system("pause");
system("cls");
}
1、封装检测联系人是否存在
2、封装删除联系人函数
3、测试删除联系人功能
7.1 封装检测联系人是否存在
删除联系人前,我们需要先判断用户的联系人是否存在,如果存在删除,不存在提示用户没有要删除的联系人,因此我们可以把检测联系人是否存在封装成一个函数中,如果存在,返回联系人在通讯录中的位置,不存在返回-1。
int isExist(Arraybooks * abs , string name)
{
for (int i = 0; i < abs->Size; ++i) {
if(abs->ArrayList[i].name == name){
return i;
}
}
return -1;
}
根据用户输入的联系人判断该通讯录中是否有此人
查找到进行删除,并提示删除成功
查不到提示查无此人
void DalatePerson(Arraybooks *abs )
{
cout << "输入你要删除的联系人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if(ret != -1){
for (int i = 0; i < abs->Size; ++i) {
abs->ArrayList[ret].name = abs->ArrayList[i+1].name;
abs->ArrayList[ret].Sex = abs->ArrayList[i+1].Sex;
abs->ArrayList[ret].old = abs->ArrayList[i+1].old;
abs->ArrayList[ret].phone = abs->ArrayList[i+1].phone;
abs->ArrayList[ret].address = abs->ArrayList[i+1].address;
abs->Size--;
}
cout << "删除成功" << endl;
}
else{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
1、封装查找联系人函数
2、测试查找指定联系人
8.1 封装查找联系人函数
void findPerson(Arraybooks * abs)
{
cout << "请输入要查找的人:" << endl;
string name;
cin >> name;
int ret = isExist(abs , name);
if(ret != -1){
cout << "姓名 : " << abs->ArrayList[ret].name<<endl;
cout << "性别 : " << abs->ArrayList[ret].Sex<< endl;
cout << "年龄 : " << abs->ArrayList[ret].old <<endl;
cout << "电话 : " << abs->ArrayList[ret].phone<<endl;
cout << "地址 :" << abs->ArrayList[ret].address<<endl;
}
else{
cout << "查无此人" <<endl;
}
system("pause");
system("cls");
}
1、封装修改联系人函数
2、测试修改联系人功能
void modifyPerson(Arraybooks * abs) {
cout << "输入你要修改的联系人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1) {
cout << "请输入姓名:" << endl;
string name;
cin >> name;
abs->ArrayList[ret].name = name;
cout << "请输入性别:" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
int sex;
cin >> sex;
while (true) {
if (sex == 1 || sex == 2) {
abs->ArrayList[ret].Sex = sex;
break;
} else {
cout << "输入错误" << endl;
}
}
cout << "输入年龄:" << endl;
int old = 0;
cin >> old;
abs->ArrayList[ret].old = old;
cout << "输入电话" << endl;
string phone;
cin >> phone;
abs->ArrayList[ret].phone = phone;
cout << "输入地址" << endl;
string address;
cin >> address;
abs->ArrayList[ret].address = address;
cout << "修改成功" << endl;
}
else{
cout << "查无此人" <<endl;
}
system("pause");
system("cls");
}
1、封装清空联系人函数
2、测试清空联系人
void cleanPerson(Arraybooks * abs){
abs->Size = 0;
cout << "清空完成" << endl;
system("pause");
system("cls");
}
#include
using namespace std;
#include
#define MAX 100
struct Person
{
string name;
int Sex;
int old;
string phone;
string address;
};
struct Arraybooks
{
struct Person ArrayList[MAX];
int Size;
};
void addPerson(Arraybooks *abs)
{
if(abs -> Size == MAX){
cout <<"通讯录已满" << endl;
return;
}
else{
string name;
cout << "输入姓名" << endl;
cin >> name;
abs->ArrayList[abs->Size].name = name;
cout << "输入性别" << endl;
cout <<"1---男" << endl;
cout <<"2---女" <<endl;
int sex;
while(-1){
cin >> sex;
if(sex == 1 || sex == 2) {
abs->ArrayList[abs->Size].Sex = sex;
break;
}
cout << "输入有误,请重新输入";
}
cout << "请输入年龄" << endl;
int age = 0;
cin >> age;
abs->ArrayList[abs->Size].old = age;
cout << "请输入电话:" << endl;
string phone = " ";
cin >> phone;
abs->ArrayList[abs->Size].phone = phone;
cout << "请输入地址" << endl;
string address;
cin >> address;
abs->ArrayList[abs->Size].address = address;
abs->Size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
}
void showPerson(Arraybooks *abs)
{
if(abs->Size == 0){
cout << "记录为空" << endl;
}
else{
for(int i = 0;i < abs->Size;i++){
cout << "姓名:" << abs->ArrayList[i].name << "\t";
cout << "性别:" << (abs->ArrayList[i].Sex == 1?"男":"女") << "\t";
cout << "年龄: " << abs->ArrayList[i].old << "\t";
cout << "电话:" <<abs->ArrayList[i].phone << "\t";
cout << "地址: " << abs->ArrayList[i].address << endl;
}
}
system("pause");
system("cls");
}
int isExist(Arraybooks * abs , string name)
{
for (int i = 0; i < abs->Size; ++i) {
if(abs->ArrayList[i].name == name){
return i;
}
}
return -1;
}
void DalatePerson(Arraybooks *abs )
{
cout << "输入你要删除的联系人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if(ret != -1){
for (int i = 0; i < abs->Size; ++i) {
abs->ArrayList[ret].name = abs->ArrayList[i+1].name;
abs->ArrayList[ret].Sex = abs->ArrayList[i+1].Sex;
abs->ArrayList[ret].old = abs->ArrayList[i+1].old;
abs->ArrayList[ret].phone = abs->ArrayList[i+1].phone;
abs->ArrayList[ret].address = abs->ArrayList[i+1].address;
abs->Size--;
}
cout << "删除成功" << endl;
}
else{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void findPerson(Arraybooks * abs)
{
cout << "请输入要查找的人:" << endl;
string name;
cin >> name;
int ret = isExist(abs , name);
if(ret != -1){
cout << "姓名 : " << abs->ArrayList[ret].name<<endl;
cout << "性别 : " << abs->ArrayList[ret].Sex<< endl;
cout << "年龄 : " << abs->ArrayList[ret].old <<endl;
cout << "电话 : " << abs->ArrayList[ret].phone<<endl;
cout << "地址 :" << abs->ArrayList[ret].address<<endl;
}
else{
cout << "查无此人" <<endl;
}
system("pause");
system("cls");
}
void modifyPerson(Arraybooks * abs) {
cout << "输入你要修改的联系人" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1) {
cout << "请输入姓名:" << endl;
string name;
cin >> name;
abs->ArrayList[ret].name = name;
cout << "请输入性别:" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
int sex;
cin >> sex;
while (true) {
if (sex == 1 || sex == 2) {
abs->ArrayList[ret].Sex = sex;
break;
} else {
cout << "输入错误" << endl;
}
}
cout << "输入年龄:" << endl;
int old = 0;
cin >> old;
abs->ArrayList[ret].old = old;
cout << "输入电话" << endl;
string phone;
cin >> phone;
abs->ArrayList[ret].phone = phone;
cout << "输入地址" << endl;
string address;
cin >> address;
abs->ArrayList[ret].address = address;
cout << "修改成功" << endl;
}
else{
cout << "查无此人" <<endl;
}
system("pause");
system("cls");
}
void cleanPerson(Arraybooks * abs){
abs->Size = 0;
cout << "清空完成" << endl;
system("pause");
system("cls");
}
void mean()
{
cout << "1、创建联系人" << endl;
cout << "2、显示练习人" << endl;
cout << "3、删除练习人" << endl;
cout << "4、查找联系人" << endl;
cout << "5、修改联系人" << endl;
cout << "6、清空联系人" << endl;
cout << "0、退出通讯录" << endl;
}
int main()
{
Arraybooks abs;
abs.Size = 0;
int istream = 0;
while(true){
mean();
cin >> istream;
switch(istream){
case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
DalatePerson(&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;
}
}
return 0;
}