头文件:
#include<iostream>
#include<string>
#define MAX 1000
using namespace std;
//联系人结构体
struct Person {
string name;
int age;
string address;
string gender;
string phone;
};
//通讯录结构体
struct Addressbooks {
Person personAddress[MAX];
int size;
};
int CheckHave(Addressbooks* ps, string name);//检查所给联系人在哪个位置
void SelectPerson(Addressbooks* ps);//显示指定联系人
void print(Addressbooks* ps);//显示目前所有的联系人
void menu();//菜单
void DeletePerson(struct Addressbooks* ps);//删除联系人
void addPerson(struct Addressbooks* ps);//添加联系人
void upDataPerson(struct Addressbooks* ps);//修改联系人
void cleanPerson(struct Addressbooks* ps);//清空通讯录
函数:
#include"TelPhone.h"
int CheckHave(Addressbooks* ps, string name)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->personAddress[i].name == name)
{
return i;
}
}
return -1;
}
void SelectPerson(Addressbooks* ps)
{
cout << "输入需要查找的联系人的姓名" << endl;
string name;
cin >> name;
int flag = CheckHave(ps, name);
if (flag == -1) {
cout << "通讯录中没有此联系人" << endl;
system("pause");
system("cls");
return;
}
else {
cout << "姓名:\t" << "年龄:\t" << "性别:\t" << "电话号码:\t" << "地址:\t" << endl;
cout << ps->personAddress[flag].name << "\t" <<
ps->personAddress[flag].age << "\t" <<
ps->personAddress[flag].gender << "\t" <<
ps->personAddress[flag].phone << "\t" <<
ps->personAddress[flag].address << "\t" <<
endl;
system("pause");
system("cls");
}
}
void print(Addressbooks* ps)
{
if (ps->size == 0)
{
cout << "当前通讯录中没有联系人" << endl;
system("pause");
system("cls");
return;
}
cout << "姓名:\t" << "年龄:\t" << "性别:\t" << "电话号码:\t" << "地址:\t" << endl;
for (int i = 0; i < ps->size; i++) {
cout << ps->personAddress[i].name << "\t" <<
ps->personAddress[i].age << "\t" <<
ps->personAddress[i].gender << "\t" <<
ps->personAddress[i].phone << "\t" <<
ps->personAddress[i].address << "\t" <<
endl;
}
system("pause");
system("cls");
}
void menu()
{
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 DeletePerson(struct Addressbooks* ps)
{
//判断通讯录是不是存在数据
if (ps->size == 0) {
cout << "没有联系人数据,请添加后重试" << endl;
system("pause");
system("cls");
return;
}
string name;
cout << "输入要删除的联系人" << endl;
cin >> name;
int flag = CheckHave(ps, name);
if (flag == -1)
{
cout << "当前通讯录没有此联系人" << endl;
return;
}
else
{
int cur = flag;
while (cur < ps->size)
{
ps->personAddress[cur] = ps->personAddress[cur + 1];
cur++;
}
ps->size--;
cout << "删除成功" << endl;
}
}
void addPerson(struct Addressbooks* ps)
{
// 判读通讯录是否已经满了
if (ps->size == MAX)
{
cout << "通讯录已满" << endl;
system("pause");
system("cls");
return;
}
cout << "输入一个姓名" << endl;
string name;
cin >> name;
ps->personAddress[ps->size].name = name;
while (1) {
cout << "输入性别" << endl;
string gender;
cin >> gender;
if (!(gender == "男" || gender == "女"))
{ //0 -1
cout << "性别错误 输入 0 退出 1 重新输入" << endl;
int flag;
cin >> flag;
if (!flag) {
return;
}
else {
continue;
}
}
ps->personAddress[ps->size].gender = gender;
break;
}
while (1) {
cout << "输入年龄" << endl;
int age;
cin >> age;
if (age < 0 || age>150)
{
cout << "年龄错误 输入 0 退出 1 重新输入" << endl;
int flag;
cin >> flag;
if (!flag) {
break;
}
else {
continue;
}
}
ps->personAddress[ps->size].age = age;
break;
}
while (1) {
cout << "输入电话号码" << endl;
string phone;
cin >> phone;
if (phone.length() != 11)
{
cout << "电话号码的长度错误 输入 0 退出 1 重新输入" << endl;
int flag;
cin >> flag;
if (!flag) {
break;
}
else {
continue;
}
}
ps->personAddress[ps->size].phone = phone;
break;
}
cout << "输入地址" << endl;
string address;
cin >> address;
ps->personAddress[ps->size].address = address;
ps->size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
void upDataPerson(struct Addressbooks* ps) {
string name;
cout << "输入要修改的联系人" << endl;
cin >> name;
int flag = CheckHave(ps, name);
if (flag == -1)
{
cout << "当前通讯录没有此联系人" << endl;
system("pause");
system("cls");
return;
}
else {
int temp;
cout << "输入 1 修改姓名" << endl;
cout << "输入 2 修改地址" << endl;
cout << "输入 3 修改年龄" << endl;
cout << "输入 4 修改电话" << endl;
cout << "输入 5 修改性别" << endl;
cin >> temp;
switch (temp) {
case 1: {
//修改姓名
cout << "输入新的姓名" << endl;
string name;
cin >> name;
ps->personAddress[flag].name = name;
cout << "修改成功" << endl;
break;
}
case 2: {
//修改地址
cout << "输入新的地址" << endl;
string address;
cin >> address;
ps->personAddress[flag].address = address;
cout << "修改成功" << endl;
break;
}
case 3:
{
//修改年龄
while (1) {
cout << "输入新的年龄" << endl;
int age;
cin >> age;
if (age < 0 || age>150)
{
cout << "年龄错误 输入 0 退出 1 重新输入" << endl;
int t;
cin >> t;
if (!t) {
break;
}
else {
continue;
}
}
ps->personAddress[flag].age = age;
cout << "修改成功" << endl;
break;
}
break;
}
//修改电话
case 4: {
while (1) {
cout << "输入新的电话号码" << endl;
string phone;
cin >> phone;
if (phone.length() != 11)
{
cout << "电话号码的长度错误 输入 0 退出 1 重新输入" << endl;
int t;
cin >> t;
if (!t) {
break;
}
else {
continue;
}
}
ps->personAddress[flag].phone = phone;
cout << "修改成功" << endl;
break;
}
break;
}
//修改性别
case 5: {
while (1) {
cout << "输入新的性别" << endl;
string gender;
cin >> gender;
if (!(gender == "男" || gender == "女"))
{ //0 -1
cout << "性别错误 输入 0 退出 1 重新输入" << endl;
int t;
cin >> t;
if (!t) {
return;
}
else {
continue;
}
}
ps->personAddress[flag].gender = gender;
cout << "修改成功" << endl;
break;
}
break;
}
default: {
cout << "没有此选项" << endl;
break;
}
}
}
system("pause");
system("cls");
}
void cleanPerson(struct Addressbooks* ps)
{
//删除所有
ps->size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
测试:
#include"TelPhone.h"
int main()
{
Addressbooks abs;
abs.size = 0;
int select = 0;
while (true)
{
menu();
cin >> select;
switch (select)
{
case 1: //添加联系人
addPerson(&abs);
break;
case 2: //显示联系人
print(&abs);
break;
case 3: //删除联系人
DeletePerson(&abs);
break;
case 4: //查找联系人
SelectPerson(&abs);
break;
case 5: //修改联系人
upDataPerson(&abs);
break;
case 6: //清空联系人
cleanPerson(&abs);
break;
case 0: //退出通讯录
cout << "欢迎下次使用" << endl;
return 0;
break;
default:
break;
}
}
return 0;
}
标签:ps,cout,实现,C++,flag,通讯录,size,personAddress,name
From: https://blog.csdn.net/2301_82086893/article/details/143467838