通讯录管理系统

#include
#include
#define Max 1000//注意没有分号
using namespace std;
//定义联系人结构体
struct people
{
string m_name;
int m_age;
//以零代表男1代表女
int m_sex;
string m_add;
string m_tell;
};
struct addbooks
{
struct people p[Max];
int num = 0;//结构体可以初始化
}ren;
//查找联系人的函数
int Find()
{
int j;//j表示需要查找这个名字在p中那个数组
string str;
cout << "请输入你要查找的名字\n";
cin >> str;
//通过比较名字字符串,实现找到i然后利用i输出全部信息
for (int i = 0; i < ren.num; i++)
{
if (str == ren.p[i].m_name)
{
j = i;
return j;
}
}
return -1;
}
//显示菜单
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 Insert()
{
if (ren.num < Max)
{
int i = ren.num;
cout << "请输入名字:" << endl;
cin >> ren.p[i].m_name;
cout << "请输入性别:" << endl;
cin >> ren.p[i].m_sex;
cout << "请输入年龄:" << endl;
cin >> ren.p[i].m_age;
cout << "请输入住址:" << endl;
cin >> ren.p[i].m_add;
cout << "请输入电话:" << endl;
cin >> ren.p[i].m_tell;
ren.num++;
}
else
cout << "通讯录已满\n";
}
//显示联系人
void ShowPeo()
{
if (ren.num == 0)
cout << "通讯录为空\n";
for (int i = 0; i < ren.num; i++)
{
cout << "姓名:" << ren.p[i].m_name << endl;
cout << "年龄:" << ren.p[i].m_age << endl;
cout << "地址:" << ren.p[i].m_add << endl;
cout << "电话:" << ren.p[i].m_tell << endl;
if (ren.p[i].m_sex == 0)
{
cout << "性别:男\n";
}
else
cout << "性别:女\n";
}

}
//删除联系人
void Dele()//用后一个覆盖前一个
{
int j = Find();
if (j >= 0)
{
for (int i = j; i < ren.num; i++)
ren.p[i] = ren.p[i + 1];
ren.num--;
}
else
cout << "查无此人" << endl;
}
//查找联系人
void Search()
{
int j;//j表示需要查找这个名字在p中那个数组
j = Find();
if (j>=0)
{
cout << "姓名:" << ren.p[j].m_name << endl;
cout << "年龄:" << ren.p[j].m_age << endl;
cout << "地址:" << ren.p[j].m_add << endl;
cout << "电话:" << ren.p[j].m_tell << endl;
if (ren.p[j].m_sex == 0)
{
cout << "性别:男\n";
}
else
cout << "性别:女\n";
}
else
cout << "查无此人\n";
}
//修改联系人
void Change()
{
int i = Find();
if (i >= 0)
{
cout << "请输入名字:" << endl;
cin >> ren.p[i].m_name;
cout << "请输入性别:" << endl;
cin >> ren.p[i].m_sex;
cout << "请输入年龄:" << endl;
cin >> ren.p[i].m_age;
cout << "请输入住址:" << endl;
cin >> ren.p[i].m_add;
cout << "请输入电话:" << endl;
cin >> ren.p[i].m_tell;
}
else
cout << "查无此人\n";
}
//清空联系人
void Cover()
{
ren.num = 0;//逻辑上清空
cout << "联系人已清空\n";
}
//主函数
int main(void)
{
int a;
while (1)
{
ShowMenu();
cin >> a;
if(a!=0)
{
switch (a)
{
case 1:Insert();
break;
case 2:ShowPeo();
break;
case 3:Dele();
break;
case 4:Search();
break;
case 5:Change();
break;
case 6:Cover();
break;
}
}
else
break;
//cout << "\f" << endl;//换页
//system("cls");//清屏函数
}
return 0;

}

你可能感兴趣的:(通讯录管理系统)