C++学习日记通讯录管理-创新互联
通讯录管理
创新互联公司-专业网站定制、快速模板网站建设、高性价比陈巴尔虎网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式陈巴尔虎网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖陈巴尔虎地区。费用合理售后完善,十载实体公司更值得信赖。#include
using namespace std;
#include
constexpr auto MAX = 1000;
struct person
{
string name;//名字
int age{};//年龄
int sex{};//性别 1男 2女
string phone;//电话号
string addr;//地址
};
struct adderessbooks
{
struct person person_array[MAX];
int m_size{};
};
void add_person(adderessbooks* abs)
{
if (abs->m_size == MAX)
{
cout<< "通讯录已满"<< endl;
return;
}
else
{
//姓名
string name;
cout<< "请输入姓名"<< endl;
cin >>name;
abs->person_array[abs->m_size].name = name;
//性别
int sex=0;
cout<< "请输入性别,1为男,2为女"<< endl;
while (true)
{
cin >>sex;
if (sex == 1 || sex == 2)
{
abs->person_array[abs->m_size].sex = sex;
break;
}
else
{
cout<< "输入格式错误,请重新输入"<< endl;
}
}
//年龄
int age=0;
cout<< "请输入年龄"<< endl;
cin >>age;
abs->person_array[abs->m_size].age = age;
//电话
string phone;
cout<< "请输入电话号"<< endl;
cin >>phone;
abs->person_array[abs->m_size].phone = phone;
//地址
string addr;
cout<< "请输入地址"<< endl;
cin >>addr;
abs->person_array[abs->m_size].addr = addr;
//更新通讯录人数
abs->m_size++;
cout<< "成功添加"<< endl;
system("pause");
system("cls");
}
}
void show_person(adderessbooks* abs)
{
if (abs->m_size == 0)
{
cout<< "无联系人"<< endl;
system("pause");
system("cls");
}
else
{
for (int i = 0; i< abs->m_size; i++)
{
cout<< "姓名 "<< abs->person_array[i].name<< "\t";
cout<< "性别 "<< (abs->person_array[i].sex == 1 ? "男":"女" )<< "\t";
cout<< "年龄 "<< abs->person_array[i].age<< "\t";
cout<< "电话号 "<< abs->person_array[i].phone<< "\t";
cout<< "住址 "<< abs->person_array[i].addr<< endl;
}
system("pause");
system("cls");
}
}
int is_exist(adderessbooks* abs, string name)
{
for (int i = 0; i< abs->m_size; i++)
{
if (abs->person_array[i].name == name)
{
return i;
}
}
return -1;
}
void delete_person(adderessbooks* abs)
{
cout<< "请输入要删除人姓名"<< endl;
string name;
cin >>name;
int num=is_exist(abs, name);
if (num == -1)
{
cout<< "查无此人"<< endl;
system("pause");
system("cls");
}
else
{
for (int i = num; i< abs->m_size; i++)
{
abs->person_array[i] = abs->person_array[i + 1];
}
abs->m_size--;
cout<< "删除成功"<< endl;
system("pause");
system("cls");
}
}
void find_person(adderessbooks* abs)
{
cout<< "请输入要查找人姓名"<< endl;
string name;
cin >>name;
int num = is_exist(abs, name);
if (num == -1)
{
cout<< "查无此人"<< endl;
system("pause");
system("cls");
}
else
{
cout<< "姓名 "<< abs->person_array[num].name<< "\t";
cout<< " 性别 "<< (abs->person_array[num].sex == 1 ? "男" : "女")<< "\t";
cout<< " 年龄 "<< abs->person_array[num].age<< "\t";
cout<< "电话号 "<< abs->person_array[num].phone<< "\t";
cout<< "住址 "<< abs->person_array[num].addr<< endl;
system("pause");
system("cls");
}
}
void change_person(adderessbooks* abs)
{
cout<< "请输入要修改人姓名"<< endl;
string name;
cin >>name;
int num = is_exist(abs, name);
if (num == -1)
{
cout<< "查无此人"<< endl;
system("pause");
system("cls");
}
else
{
//姓名
string name;
cout<< "请输入姓名"<< endl;
cin >>name;
abs->person_array[num].name = name;
//性别
int sex;
cout<< "请输入性别,1为男,2为女"<< endl;
while (true)
{
cin >>sex;
if (sex == 1 || sex == 2)
{
abs->person_array[num].sex = sex;
break;
}
else
{
cout<< "输入格式错误,请重新输入"<< endl;
}
}
//年龄
int age;
cout<< "请输入年龄"<< endl;
cin >>age;
abs->person_array[num].age = age;
//电话
string phone;
cout<< "请输入电话号"<< endl;
cin >>phone;
abs->person_array[num].phone = phone;
//地址
string addr;
cout<< "请输入地址"<< endl;
cin >>addr;
abs->person_array[num].addr = addr;
cout<< "成功修改"<< endl;
system("pause");
system("cls");
}
}
void empty_person(adderessbooks* abs)
{
abs->m_size = 0;
cout<< "已清空"<< endl;
system("pause");
system("cls");
}
void show_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;
}
int main()
{
adderessbooks abs;
abs.m_size = 0;
int select_num = 0;
while (true)
{
show_menu();
cin >>select_num;
switch (select_num)
{
case 1:
add_person(&abs);
break;
case 2:
show_person(&abs);
break;
case 3:
delete_person(&abs);
break;
case 4:
find_person(&abs);
break;
case 5:
change_person(&abs);
break;
case 6:
empty_person(&abs);
break;
case 0:
cout<< "欢迎下次使用"<< endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
网站名称:C++学习日记通讯录管理-创新互联
标题链接:http://scyanting.com/article/coegoo.html