照着B站上写的,然后加了点自己的写法

  • 头文件
#pragma once
#include<iostream>
#include<string>
#define Size 100
#define OK 1
#define Error -1
using namespace std;
// 结构体,通讯录人
struct Person
{
// 名字
string m_Name;
// 性别 1男 2 女
int m_Sex;
// 年龄
int m_Age;
// 电话
string m_Phone;
// 地址
string m_Addr;
};
// 通讯录
struct AddressBooks
{
struct Person personArray[Size];
// 通讯录个数
int m_size;
};
// 显示界面
void showMenu() {

cout << "*******************************************" << endl;
cout << "************** 1.0 添加联系人 *************" << endl;
cout << "************** 2.0 显示联系人 *************" << endl;
cout << "************** 3.0 删除联系人 *************" << endl;
cout << "************** 4.0 查找联系人 *************" << endl;
cout << "************** 5.0 修改联系人 *************" << endl;
cout << "************** 6.0 清空联系人 *************" << endl;
cout << "************** 0.0 退出通讯录 *************" << endl;
cout << "*******************************************" << endl;
}
// 添加联系人
void addPerson(AddressBooks* addressBooks) {
// 判断通讯录慢了没
if (addressBooks->m_size == Size) {
cout << "通讯录已满,无法添加!" << endl;
return;
}
else {
// 名字
// 性别 1 男 2 女
// 年龄
// 电话
// 地址
string name;
cout << "请输入姓名:" << endl;
cin >> name;
addressBooks->personArray[addressBooks->m_size].m_Name = name;
int sex;
cout << "请输入性别(数字) 1: 男 2: 女:" << endl;
cin >> sex;
addressBooks->personArray[addressBooks->m_size].m_Sex = sex;
int age;
cout << "请输入年龄:" << endl;
cin >> age;
addressBooks->personArray[addressBooks->m_size].m_Age = age;
string phone;
cout << "请输入电话:" << endl;
cin >> phone;
addressBooks->personArray[addressBooks->m_size].m_Phone = phone;
string address;
cout << "请输入地址:" << endl;
cin >> address;
addressBooks->personArray[addressBooks->m_size].m_Addr = address;
cout << "添加成功!" << endl;
addressBooks->m_size++;
}
}
// 显示联系人
void showPerson(AddressBooks addressbooks) {
if (addressbooks.m_size == 0) {
cout << "通讯录为空!" << endl;
return;
}
else {
cout << "查询成功,记录如下:" << endl;
for (size_t i = 0; i < addressbooks.m_size; i++)
{
cout << "\t姓名:" << addressbooks.personArray[i].m_Name
<< "\t性别:" << addressbooks.personArray[i].m_Sex
<< "\t年龄:" << addressbooks.personArray[i].m_Age
<< "\t电话" << addressbooks.personArray[i].m_Phone
<< "\t地址" << addressbooks.personArray[i].m_Addr << endl;
}
return;
}
}
// 名字是否存在,返回下标
size_t nameIsExistBooks(AddressBooks *addressbooks, string name) {
size_t temp;
if (addressbooks->m_size == 0) {
cout << "通讯录为空" << endl;
return -1;
}
else
{
for (size_t i = 0; i < addressbooks->m_size; i++)
{
if (addressbooks->personArray[i].m_Name == name) {
temp = i;
// 返回下标索引
return temp;
}
}
return -1;
}
}
// 电话是否存在 返回下标
size_t phoneIsExistBooks(AddressBooks *addressbooks, string phone) {
size_t temp;
if (addressbooks->m_size == 0) {
cout << "通讯录为空" << endl;
return -1;
}
else
{
for (size_t i = 0; i < addressbooks->m_size; i++)
{
if (addressbooks->personArray[i].m_Name == phone) {
temp = i;
// 返回下标索引
return temp;
}
}
return -1;
}
}
// 是否为空
bool isNullBooks(AddressBooks *addressbooks) {
return addressbooks->m_size == 0;
}
int deletePerson(AddressBooks *addressbooks)
{
string name, phone;
int select;
int end = addressbooks->m_size;
cout << "请输入选项:按照名字删除(1),按照电话删除(2):" << endl;
cin>>select;
if (select == 1) {
// 名字删除
cout << "请输入名字:" << endl;
cin >> name;
size_t index = nameIsExistBooks(addressbooks, name);
if (index < 0)
{
cout << "查无此人!" << endl;
return Error;
}
else
{
for (size_t i = index; i<end; i++)
{
addressbooks->personArray[i] = addressbooks->personArray[i + 1];
addressbooks->m_size--;
cout << "删除成功!" << endl;
return OK;
}
}
}
else if (select == 2) {
// 电话删除
cout << "请输入电话:" << endl;
cin >> name;
size_t index = nameIsExistBooks(addressbooks, phone);
if (index < 0)
{
cout << "查无此人!" << endl;
return Error;
}
else
{
for (size_t i = index; i < end; i++)
{
addressbooks->personArray[i] = addressbooks->personArray[i + 1];
addressbooks->m_size--;
cout << "删除成功!" << endl;
return OK;
}
}
}
else {
// 其他选项
cout << "操作失误!删除失败!" << endl;
return Error;
}
}