头文件部分
#pragma once
#include <iostream>
using namespace std;
#include <string>
#define MAX 1000
struct Person
{
string m_Name;//姓名
string m_Sex;//性别
string m_Phone;//电话
string m_Addr;//住址
int m_Age;
};
//通讯录结构体
struct Addressbooks
{
struct Person personArr[MAX];
int m_Size;
};
//声明
void Addperson(struct Addressbooks* txl);
void Showperson(struct Addressbooks* txl);
void Delperson(struct Addressbooks* txl);
void Findperson(struct Addressbooks* txl);
void Modifperson(struct Addressbooks* txl);
void Cleanperson(struct Addressbooks* txl);
int Isexist(struct Addressbooks* txl, string name);
通讯录各函数实现部分
#include "TXL.h"
//添加联系人
void Addperson(struct Addressbooks* txl)
{
if (txl->m_Size == MAX)
{
cout << "通讯录已满,无法添加!" << endl;
}
else
{
//添加姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
txl->personArr[txl->m_Size].m_Name = name;
//添加性别
string sex;
cout << "请输入姓别:" << endl;
cin >> sex;
txl->personArr[txl->m_Size].m_Sex = sex;
//添加年龄
int age;
cout << "请输入年龄:" << endl;
cin >> age;
txl->personArr[txl->m_Size].m_Age = age;
//添加电话
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
txl->personArr[txl->m_Size].m_Phone = phone;
//添加地址
string addr;
cout << "请输入地址:" << endl;
cin >> addr;
txl->personArr[txl->m_Size].m_Addr = addr;
}
txl->m_Size++;
cout << "添加成功" << endl;
system("pause");
//清屏操作
system("cls");
}
//显示联系人
void Showperson(struct Addressbooks* txl)
{
//判断通讯录人数是否为0
if (txl->m_Size == 0)
{
cout << "当前通讯录为空" << endl;
}
else
{
for (int i = 0; i < txl->m_Size; i++)
{
cout << " 姓名: " << txl->personArr[i].m_Name << "\t";
cout << " 性别: " << txl->personArr[i].m_Sex << "\t";
cout << " 年龄: " << txl->personArr[i].m_Age << "\t";
cout << " 电话: " << txl->personArr[i].m_Phone << "\t";
cout << " 地址: " << txl->personArr[i].m_Addr << endl;
}
}
system("pause");
system("cls");
}
//检测联系人是否存在
int Isexist(struct Addressbooks* txl, string name)
{
for (int i = 0; i < txl->m_Size; i++)
{
if (txl->personArr[i].m_Name == name)
{
return i;//找到返回此人的数组下标
}
}
return -1;//遍历结束都没找到返回-1
}
//删除联系人
void Delperson(struct Addressbooks* txl)
{
string name;
cout << "请输入要删除的联系人:" << endl;
cin >> name;
int ret = Isexist(txl, name);
if (ret != -1)
{
for (int i = ret; i < txl->m_Size; i++)
{
txl->personArr[i] = txl->personArr[i + 1];
}
cout << "删除成功" << endl;
txl->m_Size--;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//查找联系人
void Findperson(struct Addressbooks* txl)
{
string name;
cout << "请输入要查找的联系人:" << endl;
cin >> name;
int ret = Isexist(txl, name);
if (ret != -1)
{
cout << " 姓名: " << txl->personArr[ret].m_Name << "\t";
cout << " 性别: " << txl->personArr[ret].m_Sex << "\t";
cout << " 年龄: " << txl->personArr[ret].m_Age << "\t";
cout << " 电话: " << txl->personArr[ret].m_Phone << "\t";
cout << " 地址: " << txl->personArr[ret].m_Addr << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//修改联系人
void Modifperson(struct Addressbooks* txl)
{
string name;
cout << "请输入要修改的联系人:" << endl;
cin >> name;
int ret = Isexist(txl, name);
if (ret != -1)
{
//添加姓名
string name;
cout << "请输入姓名:" << endl;
cin >> name;
txl->personArr[ret].m_Name = name;
//添加性别
string sex;
cout << "请输入姓别:" << endl;
cin >> sex;
txl->personArr[ret].m_Sex = sex;
//添加年龄
int age;
cout << "请输入年龄:" << endl;
cin >> age;
txl->personArr[ret].m_Age = age;
//添加电话
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
txl->personArr[ret].m_Phone = phone;
//添加地址
string addr;
cout << "请输入地址:" << endl;
cin >> addr;
txl->personArr[ret].m_Addr = addr;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//清空联系人
void Cleanperson(struct Addressbooks* txl)
{
int num = 0;
cout << "清空将会把所有联系人清理调掉,请慎重选择:1、确定 2、取消" << endl;
cin >> num;
if (num == 1)
{
txl->m_Size = 0;
cout << "通讯录已被清空" << endl;
}
else
{
cout << "取消成功" << endl;
}
system("pause");
system("cls");
}
测试板块
#include"TXL.h"
void menu()
{
cout << "\t\t\t\t\t***** 1.添加联系人 *****" << endl;
cout << "\t\t\t\t\t***** 2.显示联系人 *****" << endl;
cout << "\t\t\t\t\t***** 3.删除联系人 *****" << endl;
cout << "\t\t\t\t\t***** 4.查找联系人 *****" << endl;
cout << "\t\t\t\t\t***** 5.修改联系人 *****" << endl;
cout << "\t\t\t\t\t***** 6.清空联系人 *****" << endl;
cout << "\t\t\t\t\t***** 0.退出程序 *****" << endl;
}
int main()
{
//创建通讯录结构体变量
struct Addressbooks txl;
//初始化通讯录人员个数
txl.m_Size = 0;
int input = 0;
do
{
menu();
cout << "请输入选择:" << endl;
cin >> input;
switch (input)
{
//添加联系人
case 1:
Addperson(&txl);
break;
//显示联系人
case 2:
Showperson(&txl);
break;
//删除联系人
case 3:
Delperson(&txl);
break;
//查找联系人
case 4:
Findperson(&txl);
break;
//修改联系人
case 5:
Modifperson(&txl);
break;
//清空联系人
case 6:
Cleanperson(&txl);
break;
//退出程序
case 0:
cout << "欢迎下次使用" << endl;
break;
default:
cout << "输入有误,请重新输入:" << endl;
break;
}
} while (input);
system("pause");
return 0;
}
将所有拼在一起就是完整代码