代码如下:文章来源地址https://www.toymoban.com/news/detail-537333.html
#include <bits/types/FILE.h>
#include <iostream>
#include <istream>
#include <type_traits>
using namespace std;
#include <string.h>
#define MAX 1000
// 函数的定义
void showMenu();
void addPerson(struct AddressBook * ab);
void showPerson(struct AddressBook * ab);
int isExit(AddressBook *ab, string name);
void deletePerson(AddressBook* ab, string name);
void findPerson(AddressBook * ab);
void emptyPerson(AddressBook *ab);
// 联系人的结构体
struct Person {
string m_Name; // 姓名
int m_sex; // 性别
int m_Age; // 年龄
string m_phone; // 电话
string m_Addr; // 地址
};
// 通讯录结构体
struct AddressBook {
struct Person PersonArray[MAX]; // 通讯录容量
int m_size; // 通讯录中人员数量
};
void showMenu() {
cout << "****菜单****" << endl;
cout << "1.添加联系人" << endl;
cout << "2.显示联系人" << endl;
cout << "3.删除联系人" << endl;
cout << "4.查找联系人" << endl;
cout << "5.修改联系人" << endl;
cout << "6.清空联系人" << endl;
cout << "0.清空" << endl;
cout << "************" << endl;
}
void addPerson(struct AddressBook* ab) {
// 通讯录是否已经满了
if (ab->m_size == MAX) {
cout << "通讯录已经满了" << endl;
} else {
string name;
cout << "请输入用户名:";
cin >> name;
ab->PersonArray[ab->m_size].m_Name = name;
int sex = 0;
while (true) {
cout << "请输入性别(1男;2女):";
cin >> sex;
if (sex == 1 || sex == 2) {
ab->PersonArray[ab->m_size].m_sex = sex;
break;
}
cout << "请重新输入!" << endl;
}
int age = 0;
while (true) {
cout << "请输入年龄:";
cin >> age;
if (0 < age && age < 100) {
ab->PersonArray[ab->m_size].m_Age = age;
break;
}
cout << "请重新输入:" << endl;
}
cout << "请输入电话:";
string phone;
cin >> phone;
ab->PersonArray[ab->m_size].m_phone = phone;
cout << "请输入地址:";
string addr;
cin >> addr;
ab->PersonArray[ab->m_size].m_Addr = addr;
ab->m_size++;
cout << "添加成功!" << endl;
;
// system("pause");
// system("cls");
}
};
void showPerson(struct AddressBook* ab) {
// 如果当前联系人为0
if (ab->m_size == 0) {
cout << "当前记录为空" << endl;
;
} else {
for (int i = 0; i < ab->m_size; i++) {
cout << "------------" << endl;
;
cout << "name: " << ab->PersonArray[i].m_Name << "\t";
cout << "sex: " << (ab->PersonArray[i].m_sex == 1 ? "男" : "女")
<< " ";
cout << "age: " << ab->PersonArray[i].m_Age << " ";
cout << "phone: " << ab->PersonArray[i].m_phone << " ";
cout << "addr: " << ab->PersonArray[i].m_Addr << endl;
}
}
};
// 检测联系人是否存在
int isExit(AddressBook* ab, string name) {
for (int i = 0; i < ab->m_size; i++) {
if (ab->PersonArray[i].m_Name == name) {
return i;
}
}
return -1;
}
// 删除联系人
void deletePerson(AddressBook* ab) {
string name;
cout << "*输入需要查找的用户名:";
cin >> name;
if (isExit(ab, name) != -1) {
cout << "*用户存在" << endl;
// 数据迁移
int ret = isExit(ab, name);
for (int i = ret; i < ab->m_size; i++) {
ab->PersonArray[i] = ab->PersonArray[i + 1];
}
ab->m_size--;
cout << "*删除成功!" << endl;
} else {
cout << "*用户不存在" << endl;
}
}
// 查找指定联系人
void findPerson(AddressBook* ab) {
cout << "请输入你要查找的联系人:" << endl;
string name;
cin >> name;
int ret = isExit(ab, name);
if (ret != -1) {
cout << "------------" << endl;
cout << "name: " << ab->PersonArray[ret].m_Name << "\t";
cout << "sex: " << (ab->PersonArray[ret].m_sex == 1 ? "男" : "女")
<< " ";
cout << "age: " << ab->PersonArray[ret].m_Age << " ";
cout << "phone: " << ab->PersonArray[ret].m_phone << " ";
cout << "addr: " << ab->PersonArray[ret].m_Addr << endl;
} else {
cout << "查无此人!" << endl;
;
}
}
// 修改指定联系人信息
void modifyPerson(AddressBook* ab) {
cout << "请输入你要修改的联系人:" << endl;
string name;
cin >> name;
int ret = isExit(ab, name);
if (ret != -1) {
int select;
int flag = true;
while (flag) {
cout << "请输入需要修改的信息(0-退出 1-姓名 2-年龄 3-性别 4-手机 "
"5-地址) ->";
cin >> select;
switch (select) {
case 0: {
flag = false;
cout << "退出成功!"<< endl;
break;
}
case 1: {
cout << "修改姓名为:" << endl;
string new_name;
cin >> new_name;
ab->PersonArray[ret].m_Name = new_name;
cout << "修改成功!" << endl;
break;
}
case 2: {
cout << "修改年龄为:" << endl;
int new_age;
cin >> new_age;
ab->PersonArray[ret].m_Age = new_age;
cout << "修改成功!" << endl;
break;
}
case 3: {
cout << "修改性别为(1-男生 2-女生):" << endl;
int new_sex;
cin >> new_sex;
if (new_sex == 1 || new_sex == 2) {
ab->PersonArray[ret].m_sex = new_sex;
cout << "修改成功!" << endl;
} else {
cout << "输入信息有误,重新输入!" << endl;
}
break;
}
case 4: {
cout << "修改手机号码为:" << endl;
string new_phone;
cin >> new_phone;
ab->PersonArray[ret].m_phone = new_phone;
cout << "修改成功!" << endl;
break;
}
default:
cout << "输入信息无效!";
break;
}
}
} else {
cout << "查无此人!";
}
}
// 清空联系人
void emptyPerson(AddressBook *ab){
ab->m_size = 0;
cout << "通讯录已经清空" << endl;
}
int main() {
// 通讯录
AddressBook ab;
ab.m_size = 0;
// 创建用户选择的标量
int select = 0;
showMenu();
while (true) {
cout << "*请输入相关操作:";
cin >> select;
switch (select) {
case 1:
addPerson(&ab);
break;
case 2:
showPerson(&ab);
break;
case 3: {
deletePerson(&ab);
break;
}
case 4:
findPerson(&ab);
break;
case 5:
modifyPerson(&ab);
break;
case 6:
emptyPerson(&ab);
break;
case 0:
cout << "*欢迎下次使用!";
return 0;
break;
default:
break;
}
};
return 0;
}
文章来源:https://www.toymoban.com/news/detail-537333.html
到了这里,关于C++ 通讯录案例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!