c++通讯录管理系统
#include"iostream"
#include"string.h"
using namespace std;
# define MAX 1000
struct contacts {
string name;
int sex;//0-男,1-女
string telephone_number;
int age;
string address;
};
struct contactbook {
struct contacts contactsArray[MAX];
int m_number;//记录通讯录人数
};
void showmenu();//显示菜单
void Add_Contact(contactbook* bookone);//添加联系人
void show_contact(contactbook* bookone);//显示联系人
int detect_contact(contactbook* bookone, string name);//检测联系人是否在通讯录中,若在返回其位置
void dle_contact(contactbook* bookone, int location_number);//删除你输入姓名的联系人
void showpoint_er(contactbook* bookone);//显示指定联系人
void modify_contact(contactbook* bookone);//修改指定联系人
void clear_contact(contactbook* bookone);//清空联系人
int main()
{
int menu = 0;
contactbook bookone;
bookone.m_number = 0;
while (1)
{
showmenu();
cin >> menu;
switch (menu)
{
case 0://退出
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
case 1:Add_Contact(&bookone);//添加联系人
break;
case 2:show_contact(&bookone);//显示联系人
break;
case 3:
{
if (bookone.m_number == 0)cout << "通讯录为空"<<endl;
else
{
cout << "请输入你要删除的联系人" << endl;
string exam_name;
cin >> exam_name;
if (detect_contact(&bookone, exam_name) == -1)
{
cout << "您输入的联系人不在通讯录中" << endl;
}//检查输入的联系人是否在通讯录里
else
{
dle_contact(&bookone, detect_contact(&bookone, exam_name));
cout << "你想要删除的已经被删除" << endl;
}
}
system("pause");
system("cls");
break;
}
case 4:
{
showpoint_er(&bookone);//查找联系人
system("pause");
system("cls");
break;
}
case 5:modify_contact(&bookone);//修改联系人
break;
case 6:
clear_contact(&bookone);//清空联系人
break;
};
}
return 0;
}
void showmenu()//菜单功能
{
cout<< "**********************\n"
<< "**** 1、添加联系人 ****\n"
<< "**** 2、显示联系人 ****\n"
<< "**** 3、删除联系人 ****\n"
<< "**** 4、查找联系人 ****\n"
<< "**** 5、修改联系人 ****\n"
<< "**** 6、清空联系人 ****\n"
<< "**** 0、退出联系人 ****\n" << endl;
}
void Add_Contact(contactbook *bookone)//添加联系人
{
//判断人数是否了
if (bookone->m_number > MAX)
{
cout << "通讯录已经满了,无法添加!" << endl;
return;
}
else
{//添加具体联系人
//添加姓名
string name1;
cout << "请输入姓名" << endl;
cin >> name1;
bookone->contactsArray[bookone->m_number].name = name1;
// 添加性别
int sex;
cout << "请输入性别" << endl;
cout << "0——男,1——女" << endl;
while (true) {
cin >> sex;
if (sex == 1 || sex == 0) {
bookone->contactsArray[bookone->m_number].sex = sex;
break;
}
cout << "您输入有误,请重新输入" << endl;
}
//添加电话号码
string phone_number;
cout << "请输入电话号码" << endl;
cin >> phone_number;
bookone->contactsArray[bookone->m_number].telephone_number = phone_number;
//添加年龄
int age;
cout << "请输入年龄" << endl;
while (true) {
cin >> age;
if (age > 0 && age < 150) {
bookone->contactsArray[bookone->m_number].age = age;
break;
}
cout << "你输入有误,请重新输入";
}
//添加地址
string address;
cout << "请输入地址" << endl;
cin >> address;
bookone->contactsArray[bookone->m_number].address = address;
//更新通讯录人数
bookone->m_number++;
cout << "您添加人数成功" << endl;
system("pause");//按任意键继续
system("cls");
}
}
void show_contact(contactbook* bookone)
{
if (bookone->m_number == 0) {
cout << "当前通讯录无人" << endl;
system("pause");
system("cls");
}
else
{
for (int i = 0; i < bookone->m_number;i++)
{
cout << "姓名:" << bookone->contactsArray[i].name << "\t";
if (bookone->contactsArray[i].sex == 1)
{
cout << "性别:女" << "\t";
}
else
{
cout << "性别:男" << "\t";
}
cout << "年龄:" << bookone->contactsArray[i].age << "\t";
cout << "电话:" << bookone->contactsArray[i].telephone_number << "\t";
cout << "地址:" << bookone->contactsArray[i].address << endl;
}
system("pause");
system("cls");
}
}
int detect_contact(contactbook* bookone, string name)//检测联系人是否在通讯录中,存在返回位置,不存在返回-1
{
for (int i = 0; i < bookone->m_number; i++)
{
if (name == bookone->contactsArray[i].name)
{
return i;
}
}
return -1;
system("pause");
system("cls");
}
void dle_contact(contactbook* bookone, int location_number)//删除你输入姓名的联系人
{
for (int j = location_number; j < bookone->m_number; j++)
{
bookone->contactsArray[j] = bookone->contactsArray[j + 1];
}
bookone->m_number--;
}
void showpoint_er(contactbook* bookone)//显示指定联系人
{
cout << "请输入你要查找的人" << endl;
string name;
cin >> name;
int location= detect_contact(bookone, name);
if (location == -1)
{
cout << "查无此人" << endl;
}
else {
cout << "姓名:" << bookone->contactsArray[location].name << "\t";
if (bookone->contactsArray[location].sex == 1)
{
cout << "性别:女" << "\t";
}
else
{
cout << "性别:男" << "\t";
}
cout << "年龄:" << bookone->contactsArray[location].age << "\t";
cout << "电话:" << bookone->contactsArray[location].telephone_number << "\t";
cout << "地址:" << bookone->contactsArray[location].address << endl;
}
}
void modify_contact(contactbook* bookone)
{//修改姓名
cout << "请输入你要修改的人" << endl;
string name;
cin >> name;
int location = detect_contact(bookone, name);
if (location !=(-1))
{
string name1;
cout << "请输入姓名" << endl;
cin >> name1;
bookone->contactsArray[location].name = name1;
// 添加性别
int sex;
cout << "请输入性别" << endl;
cout << "0——男,1——女" << endl;
while (true) {
cin >> sex;
if (sex == 1 || sex == 0) {
bookone->contactsArray[location].sex = sex;
break;
}
cout << "您输入有误,请重新输入" << endl;
}
//添加电话号码
string phone_number;
cout << "请输入电话号码" << endl;
cin >> phone_number;
bookone->contactsArray[location].telephone_number = phone_number;
//添加年龄
int age;
cout << "请输入年龄" << endl;
while (true) {
cin >> age;
if (age > 0 && age < 150) {
bookone->contactsArray[location].age = age;
break;
}
cout << "你输入有误,请重新输入";
}
//添加地址
string address;
cout << "请输入地址" << endl;
cin >> address;
bookone->contactsArray[location].address = address;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void clear_contact(contactbook* bookone)
{
cout << "你是否真的要清空联系人" << endl;
cout << "0——不是" << endl;
cout << "1——是" << endl;
int flag = 0;
cin >> flag;
if (flag == 1)
{
bookone->m_number = 0;//
cout << "通讯录已经被清空" << endl;
}
system("pause");
system("cls");
}在这里插入代码片