[C++] 纯文本查看 复制代码 #include "menu.h"
int main()
{
// 创建通讯录
AddressBooks abs;
abs.size = 0;
int select = 0;
while (true)
{
// system("clear");
showMenu();
cin >> select;
switch (select)
{
case 1:
addPerson(&abs);
break;
case 2:
showPerson(&abs);
break;
case 3:
deletePerson(&abs);
break;
case 4:
findPerson(&abs);
break;
case 5:
modifyPerson(&abs);
break;
case 6:
cleanPerson(&abs);
break;
case 0:
cout << "欢迎下次使用" << endl;
return 0;
break;
default:
cout << "输入错误,请重新输入" << endl;
break;
}
}
return 0;
}
menu.h
[C++] 纯文本查看 复制代码 #pragma once // 编译器保证文件只包含一次
// 缺点:依赖编译器,影响代码移植性,物理文件只包含一次,如果拷贝多份还是会重复包含
// #ifndef--#define--#endif,使用的是宏定义,支持c环境即可,移植性好
#include <iostream>
#include <string>
using namespace std;
#define MAX_NUM 1000
struct Person
{
string name;
int sex; // 1男 2女
int age;
string phone;
string address;
};
struct AddressBooks
{
struct Person personArray[MAX_NUM];
int size; // 通讯录中人员个数
};
void showMenu();
void addPerson(AddressBooks *abs);
void showPerson(AddressBooks *abs);
void deletePerson(AddressBooks *abs);
void findPerson(AddressBooks *abs);
void modifyPerson(AddressBooks *abs);
void cleanPerson(AddressBooks *abs);
menu.cpp
[C++] 纯文本查看 复制代码 #include "menu.h"
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 setPerson(AddressBooks *abs, int pos)
{
cout << "请输入姓名:" << endl;
string name = "";
cin >> name;
abs->personArray[pos].name = name;
cout << "请输入性别:" << endl;
cout << "1 -- 男" << endl;
cout << "2 -- 女" << endl;
int sex = 0;
while (true)
{
cin >> sex; // sex是int类型,当输入字符时,会产生cin.fail,产生错误状态,输入流无法再次读取,会进入死循环
if (cin.fail())
{
cin.clear(); // 清除cin的错误状态
// cin.sync(); // 清除输入缓冲区,不生效,使用ignore
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "输入类型有误,请重新输入" << endl;
continue;
}
if (sex == 1 || sex == 2)
{
abs->personArray[pos].sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入年龄:" << endl;
int age = 0;
cin >> age;
abs->personArray[pos].age = age;
cout << "请输入联系电话:" << endl;
string phone = "";
cin >> phone;
abs->personArray[pos].phone = phone;
cout << "请输入家庭住址:" << endl;
string address = "";
cin >> address;
abs->personArray[pos].address = address;
}
void addPerson(AddressBooks *abs)
{
if (abs->size == MAX_NUM)
{
cout << "通讯录已满,无法添加" << endl;
}
else
{
setPerson(abs, abs->size);
// 更新通信录人数
abs->size++;
cout << "添加成功" << endl;
}
}
void printPerson(AddressBooks *abs, int pos)
{
cout << "姓名:" << abs->personArray[pos].name << "\t";
cout << "性别:" << (abs->personArray[pos].sex == 1 ? "男" : "女") << "\t";
cout << "年龄:" << abs->personArray[pos].age << "\t";
cout << "电话:" << abs->personArray[pos].phone << "\t";
cout << "住址:" << abs->personArray[pos].address << endl;
}
void showPerson(AddressBooks *abs)
{
if (abs->size == 0)
{
cout << "当前记录为空" << endl;
}
else
{
for (int i = 0; i < abs->size; i++)
{
printPerson(abs, i);
}
}
}
// 判断是否存在查询的人员,存在返回在数组中索引位置,不存在返回-1
int isExist(AddressBooks *abs, string name)
{
for (int i = 0; i < abs->size; i++)
{
if (abs->personArray[i].name == name)
{
return i;
}
}
return -1;
}
void deletePerson(AddressBooks *abs)
{
cout << "请输入您要删除的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
for (int i = ret; i < abs->size - 1; i++)
{
abs->personArray[i] = abs->personArray[i + 1];
}
abs->size--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
}
void findPerson(AddressBooks *abs)
{
cout << "请输入您要查找的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
printPerson(abs, ret);
}
else
{
cout << "查无此人" << endl;
}
}
void modifyPerson(AddressBooks *abs)
{
cout << "请输入您要修改的联系人:" << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
setPerson(abs, ret);
cout << "修改成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
}
void cleanPerson(AddressBooks *abs)
{
abs->size = 0;
cout << "通讯录已清空" << endl;
} |