代码如下:
#include<iostream>
#include <sstream>
using namespace std;
#define Max 1000
//显示菜单函数
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;
}
/*
* 添加联系人实现步骤:
* 1. 设计联系人结构体
* 2.设计通讯录结构体
* 3.在main函数中创建通讯录
* 4.封装添加联系人函数
* 5.测试
*/
//设计联系人结构体
struct Person
{
string p_name;
int p_sex;
int p_age;
string p_phone;
string p_address;
};
//通讯录结构体
struct PhoneBook
{
struct Person pArray[Max]; //设置最大值
int b_size;//通讯录中实际存放联系人个数
};
//判断字符串是否为数字函数
bool isNum(string str)
{
stringstream sin(str);
double d;
char c;
if (!(sin >> d))
return false;
if (sin >> c)
return false;
return true;
}
//添加联系人函数
void addPerson(PhoneBook* pb)
{
//判断通讯录是否满
if (pb->b_size == Max) {
cout << "通讯录已满" << endl;
return;
}//添加
else {
string name;
cout << "请输入姓名:" << endl;
cin >> name;
pb->pArray[pb->b_size].p_name = name;//插入到通讯录数组,数组下标为当前通讯录中实体个数
int sex;
cout << "请输入性别" << endl;
while (true) {
cout << "1、男 2、女" << endl;
cin >> sex;
if (sex == 1 || sex == 2) {
pb->pArray[pb->b_size].p_sex = sex;
break;
}
else {
cout << "输入有误,请按要求重新输入" << endl;
}
}
int age;
cout << "请输入年龄" << endl;
cin >> age;
pb->pArray[pb->b_size].p_age = age;
string phone;
long long p1;//用于检验是否都输入的数字
cout << "请输入手机号" << endl;
while (true)
{
cin >> phone;
if (phone.size() != 11) {
cout << "您输入的手机号位数不对,请重新输入" << endl;
}
else if (!isNum(phone)) {
cout << "您输入的手机号格式不对,请重新输入" << endl;
cout << "请正确输入11位数字" << endl;
}
else {
pb->pArray[pb->b_size].p_phone = phone;
break;
}
}
string addr;
cout << "请输入地址" << endl;
cin >> addr;
pb->pArray[pb->b_size].p_address = addr;
//完成录入后 通讯录中数据个数增加
pb->b_size++;
cout << "添加联系人成功" << endl;
system("pause");
system("cls");//清屏 重新返回菜单
}
}
//显示联系人
void printPerson(PhoneBook* pb)
{
int a = pb->b_size;
if (a == 0) {
cout << "通讯录为空" << endl;
}
else {
for (int i = 0; i < pb->b_size; i++)
{
cout << "姓名:" << pb->pArray[i].p_name;
if (pb->pArray[i].p_sex == 1) {
cout << "\t性别: " << "男";
}
else {
cout << "\t性别: " << "女";
}
cout << "\t年龄: " << pb->pArray[i].p_age;
cout << "\t电话: " << pb->pArray[i].p_phone;
cout << "\t地址: " << pb->pArray[i].p_address << endl;
}
}
system("pause");
system("cls");
}
//检测联系人是否存在
//参数1 通讯录 参数2 输入的姓名
int isExist(PhoneBook* pb, string name)
{
for (int i = 0; i < pb->b_size; i++)
{
if (pb->pArray[i].p_name == name) {
return i; //找到返回下标
}
}
return -1;
}
//删除用户
void deletePerson(PhoneBook* pb)
{
cout << "请输入删除联系人的姓名" << endl;
string name;
cin >> name;
int r = isExist(pb, name);
if (r==-1) {
cout << "查无此人" << endl;
}
else {
for (int i = r; i < pb->b_size; i++)
{
pb->pArray[i] = pb->pArray[i + 1];
}
pb->b_size--;
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
//查找联系人
void selectPerson(PhoneBook* pb)
{
cout << "请输入姓名" << endl;
string name;
cin >> name;
int r = isExist(pb, name);
if (r != -1) {
cout << "姓名:" << pb->pArray[r].p_name;
if (pb->pArray[r].p_sex == 1) {
cout << "\t性别: " << "男";
}
else {
cout << "\t性别: " << "女";
}
cout << "\t年龄: " << pb->pArray[r].p_age;
cout << "\t电话: " << pb->pArray[r].p_phone;
cout << "\t地址: " << pb->pArray[r].p_address << endl;
}
else {
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//修改
void updatePerson(PhoneBook* pb)
{
cout << "请输入姓名" << endl;
string name;
cin >> name;
int r = isExist(pb, name);
if (r != -1) {
string name;
cout << "请输入姓名:" << endl;
cin >> name;
pb->pArray[r].p_name = name;//插入到通讯录数组,数组下标为当前通讯录中实体个数
int sex;
cout << "请输入性别" << endl;
while (true) {
cout << "1、男 2、女" << endl;
cin >> sex;
if (sex == 1 || sex == 2) {
pb->pArray[r].p_sex = sex;
break;
}
else {
cout << "输入有误,请按要求重新输入" << endl;
}
}
int age;
cout << "请输入年龄" << endl;
cin >> age;
pb->pArray[r].p_age = age;
string phone;
long long p1;//用于检验是否都输入的数字
cout << "请输入手机号" << endl;
while (true)
{
cin >> phone;
if (phone.size() != 11) {
cout << "您输入的手机号位数不对,请重新输入" << endl;
}
else if (!isNum(phone)) {
cout << "您输入的手机号格式不对,请重新输入" << endl;
cout << "请正确输入11位数字" << endl;
}
else {
pb->pArray[r].p_phone = phone;
break;
}
}
string addr;
cout << "请输入地址" << endl;
cin >> addr;
pb->pArray[r].p_address = addr;
cout << "修改联系人成功" << endl;
system("pause");
system("cls");//清屏 重新返回菜单
}
else {
cout << "查无此人" <<endl;
}
}
void cleanPerson(PhoneBook* pb)
{
pb->b_size = 0;
cout << "通讯录已经清空" << endl;
system("pause");
system("cls");
}
int main()
{
//创建通讯录变量
PhoneBook pb;
pb.b_size = 0;
int select = 0;
//用循环保证每次操作后依然能回到菜单页面
while (true)
{
//显示菜单
showMenu();
cout << "请输入你需要的操作的数字代号" << endl;
cin >> select;
switch (select)
{
case 1:
addPerson(&pb);//地址传递修饰实参
//1、添加联系人
break;
case 2:
//显示联系人
printPerson(&pb);
break;
case 3:
//3、删除联系人
deletePerson(&pb);
break;
case 4:
selectPerson(&pb);
// 4、查找联系人
break;
case 5:
updatePerson(&pb);
// 5、修改联系人
break;
case 6:
cleanPerson(&pb);
//6、清空联系人
break;
case 0:
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
// 0、退出通讯录
break;
default:
break;
}
}
}
运行图如下:
?
?
?
?
?
|