#include <iostream>
#include <string>
using namespace std;
#define max 1000
struct newp {
string name;
int sex ;
int age ;
string number;
string add;
};
struct books {
struct newp a[max];
int size ;
};
static 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 add(books * abs) {
if (abs->size == max) {
cout << "通讯录存储空间已满,数据逸出" << endl;
return;
}
else
{
string name;
cout << "输入姓名:" << endl;
cin >> name;
abs->a[abs->size].name = name;
cout << "输入性别:\t 1 -- 男 \t 2 -- 女" << endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->a[abs->size].sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "输入年龄:" << endl;
int age = 0;
while (true) {
cin >> age;
if (age > 0 && age <= 150) {
abs->a[abs->size].age = age;
break;
}
cout << "输入有误,请重新输入" << endl;
}
string number;
cout << "输入手机号:" << endl;
cin >> number;
abs->a[abs->size].number = number;
string add;
cout << "输入地址:" << endl;
cin >> add;
abs->a[abs->size].add = add;
//更新通讯录
abs->size++;
cout << "** 完成存储 **" << endl;
system("pause");
system("cls");//清屏
}
}
static void show(books * abs) {
if (abs->size == 0) {
cout << "当前通讯录为空" << endl;
}
else
{
for (int i = 0; i < abs->size; i++) {
cout << "姓名:" << abs->a[i].name << "\t";
cout << "性别:" << (abs->a[i].sex == 1 ? "男":"女") << "\t";
cout << "输入年龄:" << abs->a[i].age << "\t";
cout << "手机号:" << abs->a[i].number << "\t";
cout << "输入地址:" << abs->a[i].add << endl;
}
}
system("pause");
system("cls");
}
static int isExist(books * abs, string name) {
for (int i = 0; i < abs->size; i++) {
if (abs->a[i].name == name)
{
return i;
}
}
return -1;
}
static void del(books * abs) {
string name;
cout << "请输入要删除的用户姓名" << endl;
cin >> name;
int ret = isExist(abs, name);
if ( ret != -1) {
for (int i = ret; i < abs->size; i++)
{
//数据前移
abs->a[i] = abs->a[i + 1];
}
//更新通讯录人数
abs->size--;
cout << "** 删除成功 **" << endl;
}
else
{
cout << "该用户不存在" << endl;
}
system("pause");
system("cls");
}
static void search(books * abs){
string name;
cout << "请输入要查找的用户姓名" << endl;
cin >> name;
int ret = isExist(abs, name);
if ( ret != -1) {
cout << "姓名:" << abs->a[ret].name << "\t";
cout << "性别:" << (abs->a[ret].sex == 1 ? "男" : "女") << "\t";
cout << "输入年龄:" << abs->a[ret].age << "\t";
cout << "手机号:" << abs->a[ret].number << "\t";
cout << "输入地址:" << abs->a[ret].add << endl;
}
else
{
cout << "不存在该联系人" << endl;
}
system("pause");
system("cls");
}
static void remark(books* abs) {
string name;
cout << "请输入要修改的用户姓名" << endl;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1) {
string na;
cout << "输入姓名:" << endl;
cin >> na;
abs->a[ret].name = name;
cout << "输入性别:\t 1 -- 男 \t 2 -- 女" << endl;
int sex = 0;
while (true) {
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->a[ret].sex = sex;
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "输入年龄:" << endl;
int age = 0;
while (true) {
cin >> age;
if (age > 0 && age <= 150) {
abs->a[ret].age = age;
break;
}
cout << "输入有误,请重新输入" << endl;
}
string number;
cout << "输入手机号:" << endl;
cin >> number;
abs->a[ret].number = number;
string add;
cout << "输入地址:" << endl;
cin >> add;
abs->a[ret].add = add;
cout << "** 完成修改 **" << endl;
}
else
{
cout << "该用户不存在" << endl;
}
system("pause");
system("cls");//清屏
}
static void clean(books * abs) {
abs->size = 0;
cout << "通信录已清空" << endl;
system("pause");
system("cls");
}
int main() {
books abs;
//初始化当前人员个数
abs.size = 0;
int k = 0;
while (true) { //true
showMenu();
cin >> k;
switch (k) {
case 1:add(&abs); break;//利用地址传递可以修改值
case 2:show(&abs); break;
case 3:del(&abs); break;
case 4:search(&abs); break;
case 5:remark(&abs); break;
case 6:clean(&abs); break;
case 0:cout << "** 退出程序 **" << endl;
system("pause");
return 0;
break;
default:cout << "请检查输入是否有误" << endl; break;
}
}
system("pause");
return 0;
}