主要使用结构体创建通讯录和通讯录成员,利用全局函数实现对通讯录的添加、显示、删除、查找、修改、清空、以及退出通讯录。
实现界面
实现代码:
#include<iostream>
using namespace std;
#include<string>
#define max 1000
struct menbers
{
string name;
string sex;
int age;
string number;
string addres;
};
struct books
{
menbers people[max];
int size=0;
};
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;
}
void getmenbers(struct books *p)
{
if (p->size >= 1000)
{
cout << "通讯录已满" << endl;
return;
}
else
{
cout << "请输入添加联系人的姓名" << endl;
cin >> p->people[p->size].name;
cout << "请输入添加联系人的性别" << endl;
cin >> p->people[p->size].sex;
cout << "请输入添加联系人的年龄" << endl;
cin >> p->people[p->size].age;
cout << "请输入添加联系人的联系电话" << endl;
cin >> p->people[p->size].number;
cout << "请输入添加联系人的家庭住址" << endl;
cin >> p->people[p->size].addres;
cout << "添加成功!!" << endl;
p->size++;
}
system("pause");
system("cls");
}
void print(struct books*p)
{
if (p->size == 0)
{
cout << "通讯录为空" << endl;
}
else
{
for (int i = 0; i < p->size; i++)
{
cout << p->people[i].name<< " " << p->people[i].sex << " " << p->people[i].age << " " << p->people[i].number << " " << p->people[i].addres << endl;
}
}
system("pause");
system("cls");
}
void findmenbers(struct books* p)
{
if (p->size == 0)
{
cout << "通讯录为空" << endl;
return;
}
else
{
string name;
cout << "请输入需要查找的姓名" << endl;
cin >> name;
for (int i = 0; i < p->size; i++)
{
if (p->people[i].name == name)
{
cout << p->people[i].name << " " << p->people[i].sex << " " << p->people[i].age << " " << p->people[i].number << " " << p->people[i].addres << endl;
}
}
}
system("pause");
system("cls");
}
void deletemenbers(struct books* p)
{
if (p->size == 0)
{
cout << "通讯录为空" << endl;
return;
}
else
{
string name;
cout << "请输入需要删除的姓名" << endl;
cin >> name;
for (int i = 0; i < p->size; i++)
{
if (p->people[i].name == name)
{
for (int j = i; j < max+1; j++)
{
p->people[i].name = p->people[j+1].name;
p->people[i].sex = p->people[j+1].sex;
p->people[i].age = p->people[j+1].age;
p->people[i].number = p->people[j+1].number;
p->people[i].addres = p->people[j+1].addres;
}
}
cout << "删除成功" << endl;
p->size--;
}
}
system("pause");
system("cls");
}
void changemenbers(struct books* p)
{
if (p->size == 0)
{
cout << "通讯录为空" << endl;
return;
}
else
{
string name;
cout << "请输入需要修改联系人的姓名" << endl;
cin >> name;
for (int i = 0; i < p->size; i++)
{
if (p->people[i].name == name)
{
cout << "请输入修改联系人的姓名" << endl;
cin >> p->people[i].name;
cout << "请输入修改联系人的性别" << endl;
cin >> p->people[i].sex;
cout << "请输入修改联系人的年龄" << endl;
cin >> p->people[i].age;
cout << "请输入修改联系人的联系电话" << endl;
cin >> p->people[i].number;
cout << "请输入修改联系人的家庭住址" << endl;
cin >> p->people[i].addres;
cout << "修改成功!!" << endl;
}
}
}
system("pause");
system("cls");
}
void emptymenbers(struct books* p)
{
p->size = 0;
cout << "清空成功" << endl;
system("pause");
system("cls");
}
int main()
{
struct books b;
int select;
while (1)
{
showmenu();
cout << "请选择功能" << endl;
cin >> select;
switch (select)
{
case 0:cout << "欢迎下次使用" << endl;
system("pause");
return 0; break;
case 1:getmenbers(&b); break;
case 2:print(&b); break;
case 3:deletemenbers(&b); break;
case 4:findmenbers(&b); break;
case 5:changemenbers(&b); break;
case 6: emptymenbers(&b); break;
default:cout << "输入错误" << endl; break;
}
}
system("pause");
return 0;
}