#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
/*联系人结构体*/
struct Person
{
string m_Name;
int m_Age;
int m_Sex;
string m_Phone;
string m_Address;
};
/*通讯录结构体*/
typedef struct Phones
{
struct Person data;
struct Phones *next;
int length;
int max_length;
} *Phone_ptr;
/*功能函数*/
Phone_ptr Head;
void showMenu();
void phone_init(Phone_ptr &L);
void insert_phone_number(Phone_ptr L);
void phone_print(Phone_ptr L);
bool phone_delete(Phone_ptr L);
void phone_search(Phone_ptr L);
/********************************
*函数名:main
*函数功能:通讯录的整体框架
*********************************/
int main()
{
int a;
bool flag;
phone_init(Head);
while (true)
{
system("cls");
showMenu();
cout << "请输入选项" << endl;
cin >> a;
switch (a)
{
case 1: //添加联系人
insert_phone_number(Head);
break;
case 2: //显示联系人
phone_print(Head);
break;
case 3: //删除联系人
{
flag = phone_delete(Head);
if (flag)
{
cout << "删除成功" << endl;
}
else
{
cout << "删除失败" << endl;
}
}
break;
case 4: //查找联系人
phone_search(Head);
break;
case 0: //退出通讯录
cout << "欢迎下次使用\n";
system("pause");
return 0;
break;
}
system("pause");
}
return 0;
}
/********************************
*函数名:showMenu
*函数功能:显示菜单
********************************/
void showMenu()
{
cout << "***************************************" << endl;
cout << "******** 1、添加联系人 ****************" << endl;
cout << "******** 2、显示联系人 ****************" << endl;
cout << "******** 3、删除联系人 ****************" << endl;
cout << "******** 4、查找联系人 ****************" << endl;
cout << "******** 0、退出通讯录 ****************" << endl;
}
/*******************************
*函数名:phone_init
*函数功能:通讯录初始化
********************************/
void phone_init(Phone_ptr &L)
{
L = new struct Phones;
L->length = 0;
L->max_length = MAX;
L->next = NULL;
}
/*******************************
*函数名:insert_phone_number
*函数功能:插入联系人信息
********************************/
void insert_phone_number(Phone_ptr L)
{
if (L == NULL)
{
cout << "false" << endl;
return;
}
if (L->length <= L->max_length)
{
struct Phones *point = new struct Phones;
cout << "请输入姓名: ";
cin >> point->data.m_Name;
cout << endl;
cout << "请输入年龄: ";
cin >> point->data.m_Age;
cout << endl;
cout << "请输入性别:1--男 2--女 ";
cin >> point->data.m_Sex;
cout << endl;
cout << "请输入手机号码: ";
cin >> point->data.m_Phone;
cout << endl;
cout << "请输入家庭住址: ";
cin >> point->data.m_Address;
cout << endl;
point->next = L->next;
L->next = point;
}
else
{
cout << "存储空间不足,请删除内容释放存储空间" << endl;
return;
}
}
/*******************************
*函数名:phone_print
*函数功能:打印通讯录
********************************/
void phone_print(Phone_ptr L)
{
Phone_ptr sp = L->next;
cout << "姓名\t" << "年龄\t" << "性别\t" << "手机号\t\t" << "家庭住址" << endl;
while (sp != NULL)
{
cout << sp->data.m_Name << "\t" << sp->data.m_Age<<"\t" << sp->data.m_Sex<<"\t" << sp->data.m_Phone<<"\t"<<sp->data.m_Address << endl;
sp=sp->next;
}
}
/*******************************
*函数名称:phone_delete
*函数功能:删除指定联系人
********************************/
bool phone_delete(Phone_ptr L)
{
if (L->next == NULL)
{
cout << "通讯录为空 ";
return false;
}
else
{
string name;
cout << "请输入要删除的联系人姓名: ";
cin >> name;
cout << endl;
Phone_ptr temp = L->next;
Phone_ptr ptr = L;
while (temp != NULL)
{
if (temp->data.m_Name == name)
{
ptr->next = temp->next;
return true;
}
else
{
ptr = ptr->next;
temp = temp->next;
}
}
return false;
}
}
/*******************************
*函数名称:phone_search
*函数功能:查找指定联系人并打印
********************************/
void phone_search(Phone_ptr L)
{
if (L->next == NULL)
{
cout << "通讯录为空 查找失败";
}
else
{
string name;
int flag=0;
cout << "请输入要查找联系人姓名: ";
cin >> name;
cout << endl;
Phone_ptr temp = L->next;
while (temp != NULL)
{
if (temp->data.m_Name == name)
{
cout << "姓名\t" << "年龄\t" << "性别\t" << "手机号\t\t" << "家庭住址" << endl;
cout << temp->data.m_Name << "\t" << temp->data.m_Age << "\t" << temp->data.m_Sex \
<< "\t" << temp->data.m_Phone << "\t" << temp->data.m_Address << endl;
return;
}
else
{
temp = temp->next;
}
}
if (temp == NULL && flag == 0)
{
cout << "联系人不存在" << endl;
return;
}
}
}
C++通讯录(功能:增、删、查)
最新推荐文章于 2022-03-27 11:20:46 发布