功能包括:
1,添加联系人
2,显示联系人
3,删除联系人
4,查找联系人
5,修改联系人
6,清空联系人
0,退出通讯录
仅提供参考
<一>头文件为:(通讯录.h)
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include<fstream>
#define max 1000
struct person
{
//姓名
string m_name="null";
//年龄
int m_age=0;
//性别
int m_sex=0;
//电话号码
string m_phone="null";
//家庭住址
string m_addr="null";
};
struct phonebook
{
//通讯录
struct person man[max];
//人员个数
int m_shu;
};
void showmenu();
void addperson(struct phonebook*);
void showperson(struct phonebook*);
void deletperson(struct phonebook*);
int search(phonebook*, string );
void do_search(struct phonebook*);
void modifyperson(struct phonebook*);
void all_delet(struct phonebook*);
<二>源文件:(通讯录.cpp)
#include"通讯录.h"
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 addperson(struct phonebook *abb)
{
if (abb->m_shu > max)
{
cout << "联系人已满" << endl;
return;
}
cout << "请输入添加人姓名:" << endl;
string name;
cin >> name;
abb->man[abb->m_shu].m_name=name;
cout << "请输入添加人性别:" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
int am=0;
do{
cin >> am;
if (am == 1 || am == 2)
{
abb->man[abb->m_shu].m_sex = am;
break;
}
else cout << "输入错误,请重输:" << endl;
} while (true);
cout << "请输入添加人的年龄:" << endl;
int age;
cin >> age;
abb->man[abb->m_shu].m_age=age;
cout << "请输入添加人的电话号码:" << endl;
string phonenum;
cin >> phonenum;
abb->man[abb->m_shu].m_phone=phonenum;
cout << "请输入添加人的家庭住址:" << endl;
string addr;
cin >> addr;
abb->man[abb->m_shu].m_addr=addr;
abb->m_shu++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
void showperson(phonebook* abb)
{
if (abb->m_shu == 0)
{
cout << "联系人为空" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < abb->m_shu; i++)
{
cout << "姓名: " << abb->man[i].m_name << '\t';
cout << "年龄: " << abb->man[i].m_age << '\t';
cout << "性别: " << (abb->man[i].m_sex==1?"男":"女") << '\t';
cout << "号码: " << abb->man[i].m_phone << '\t';
cout << "住址: " << abb->man[i].m_addr << endl;
}
system("pause");
system("cls");
return;
}
void deletperson(phonebook*abb)
{
cout << "请输入想要删除的联系人姓名:" << endl;
string name;
cin >> name;
int ss=search(abb, name);
if (ss == -1)
{
cout << "查无此人。" << endl;
system("pause");
system("cls");
}
else
{
for (; ss < abb->m_shu; ss++)
abb->man[ss] = abb->man[ss + 1];
abb->m_shu--;
cout << "删除成功" << endl;
system("pause");
system("cls");
return;
}
}
int search(phonebook *abb, string name)
{
int i = 0;
for (; i < abb->m_shu; i++)
{
if (abb->man[i].m_name == name) return i;
}
return -1;
}
void do_search(struct phonebook* abb)
{
cout << "请输入您想要查询人姓名:" << endl;
string name;
cin >> name;
int mm=search(abb, name);
if (mm== -1)
{
cout << "查无此人。" << endl;
}
else
{
cout << "您要的信息如下:" << endl;
cout << "姓名: " << abb->man[mm].m_name << '\t';
cout << "年龄: " << abb->man[mm].m_age << '\t';
cout << "性别: " << (abb->man[mm].m_sex == 1 ? "男" : "女") << '\t';
cout << "号码: " << abb->man[mm].m_phone << '\t';
cout << "住址: " << abb->man[mm].m_addr << endl;
}
system("pause");
system("cls");
return;
}
void modifyperson(phonebook* abb)
{
cout << "请输入修改联系人姓名:" << endl;
string name;
cin >> name;
int nn = search(abb, name);
if (nn == -1)
{
cout << "查无此人。" << endl;
}
else
{
cout << "姓名: " << abb->man[nn].m_name << '\t';
cout << "年龄: " << abb->man[nn].m_age << '\t';
cout << "性别: " << (abb->man[nn].m_sex == 1 ? "男" : "女") << '\t';
cout << "号码: " << abb->man[nn].m_phone << '\t';
cout << "住址: " << abb->man[nn].m_addr << endl;
cout << "请选择修改项目:" << endl;
cout << "1.修改姓名" << endl;
cout << "2.修改年龄" << endl;
cout << "3.修改性别" << endl;
cout << "4.修改号码" << endl;
cout << "5.修改住址" << endl;
int select = 0;
cin >> select;
if (select >= 1 && select <= 5)
{
switch (select)
{
case 1:
{
cout << "请输入修改后姓名:" << endl;
string name1;
cin >> name1;
abb->man[nn].m_name = name1;
}break;
case 2:
{
cout << "请输入修改后年龄:" << endl;
int age;
cin >> age;
abb->man[nn].m_age = age;
}break;
case 3:
{
cout << "请输入修改后性别:" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
int sex;
do
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abb->man[nn].m_sex = sex; break;
}
else cout << "输入错误,请重输:"<< endl;
} while (true);
}break;
case 4:
{
cout << "请输入修改后号码:" << endl;
string phone;
cin >> phone;
abb->man[nn].m_phone = phone;
}break;
case 5:
{
cout << "请输入修改后住址:" << endl;
string address;
cin >> address;
abb->man[nn].m_addr = address;
}break;
default:break;
}
cout << "修改成功" << endl;
}
else cout << "输入错误" << endl;
}
system("pause");
system("cls");
return;
}
void all_delet(phonebook*abb)
{
cout << "是否选择清空 ?" << endl;
cout << "Y" << endl;
cout << "N" << endl;
string aa;
do{
cin >> aa;
if (aa == "Y")
{
abb->m_shu = 0;
cout << "清空成功" << endl;
system("pause");
system("cls");
return;
}
else if (aa == "N")
{
system("pause");
system("cls");
return;
}
else cout << "输入错误,请重输:" << endl;
} while (true);
}
<三>main函数为:
#include"通讯录.h"
int main()
{
//定义一个通讯录
struct phonebook a_man;
a_man.m_shu = 0;
int select=0;
while (true)
{
//调用菜单选择功能
showmenu();
cout << "请选择你的功能" << endl;
cin >> select;
if (select >= 0 && select <= 6)
{
switch (select)
{
case 1: addperson(&a_man);
break;
case 2: showperson(&a_man);
break;
case 3: deletperson(&a_man);
break;
case 4: do_search(&a_man);
break;
case 5: modifyperson(&a_man);
break;
case 6: all_delet(&a_man);
break;
case 0:
{
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
}
break;
default:break;
}
}
else
{
cout << "输入错误,请重输" << endl;
system("pause");
system("cls");
}
}
}