自己写的300行c++通讯录管理系统,可以实现如下功能:
具体代码如下:
#include<iostream>
using namespace std;
#define Max 1000//不要分号
//设计联系人结构体
struct Person
{
string m_Name;
int m_Sex;
int m_Age;
string m_Phone;
string m_Addr;
};
//设计通讯录结构体
struct Addressbooks
{
struct Person personArray[Max];
int m_Size = 0;
};
//1.添加联系人
void addPerson(Addressbooks *abs)
{
//判断通讯录是否已满
if (abs->m_Size == Max)
{
cout << "通讯录已满,无法添加!" << endl;
return;
}
else
{
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personArray[abs->m_Size].m_Name = name;
int sex = 0;
cout << "请输入性别:" << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女:" << endl;
while (true)
{
cin >> sex;
//如果输入1或者2就退出,因为输入正确。否则重新输入
if (sex == 1 || sex == 2)
{
abs->personArray[abs->m_Size].m_Sex = sex;
break;
}
else
{
cout << "输入有误,请重新输入" << endl;
}
}
cout << "请输入年龄: " << endl;
int age = 0;
cin >> age;
abs->personArray[abs->m_Size].m_Age = age;
cout << "请输入联系电话: " << endl;
string phone;
cin >> phone;
abs->personArray[abs->m_Size].m_Phone = phone;
cout << "请输入家庭住址: " << endl;
string address;
cin >> address;
abs->personArray[abs->m_Size].m_Addr = address;
abs->m_Size++;
cout << "添加成功" << endl;
system("pause");//请按任意键继续******
system("cls");//清屏操作******
}
}
//2.显示所有联系人
void showPerson(Addressbooks *abs)
{
if (abs->m_Size == 0)
{
cout << "当前通讯录中没有联系人" << endl;
}
else
{
for (int i = 0; i < abs->m_Size; i++)
{
cout << "姓名: " << abs->personArray[i].m_Name << "\t";
cout << "性别: " << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄: " << abs->personArray[i].m_Age << "\t";
cout << "电话: " << abs->personArray[i].m_Phone << "\t";
cout << "住址: " << abs->personArray[i].m_Addr << endl;
}
}
system("pause");
system("cls");
}
//检测联系人是否存在,如果存在返回联系人所在数组具体位置,不存在返回-1
int isExist(Addressbooks* abs, string name)
{
for (int i = 0; i < abs->m_Size; i++)
{
if (abs->personArray[i].m_Name == name)
{
return i;
}
}
return -1;
}
//3.删除联系人
void deletePerosn(Addressbooks *abs)
{
cout << "请那个输入您要删除的联系人: " << endl;
string name;
cin >> name;
int ret = isExist(abs, name);//注意这里abx不要*,因为abs已经是地址
if (ret == -1)
{
cout << "查无此人" << endl;
}
else
{
for (int i = ret; i < abs->m_Size; i++)//数据前移
{
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_Size--;
cout << "删除成功" << endl;
system("pause");
system("cls");
}
}
//4.查找联系人
void findPerson(Addressbooks *abs)
{
string person;
cout << "请输入需要查找的姓名: " << endl;
cin >> person;
for (int i = 0; i < abs->m_Size; i++)
{
if (abs->personArray[i].m_Name == person)
{
cout << "找到此人,其信息如下: " << endl;
cout << "姓名: " << abs->personArray[i].m_Name << "\t";
cout << "性别: " << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t";
cout << "年龄: " << abs->personArray[i].m_Age << "\t";
cout << "电话: " << abs->personArray[i].m_Phone << "\t";
cout << "住址: " << abs->personArray[i].m_Addr << endl;
return;
}
}
cout << "查无此人" << endl;
system("pause");
system("cls");
}
//5.修改联系人信息
void modifyPerson(Addressbooks* abs)
{
cout << "请输入需要修改的联系人姓名: " << endl;
string name;
cin >> name;
int ret = isExist(abs, name);
if (ret != -1)
{
//姓名
string name;
cout << "请输入姓名: " << endl;
cin >> name;
abs->personArray[ret].m_Name = name;
//性别
cout << "请输入性别: " << endl;
cout << "1 --- 男" << endl;
cout << "2 --- 女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 0 || sex == 1)
{
abs->personArray[ret].m_Sex = sex;
break;
}
else
{
cout << "输入错误,请重新输入" << endl;
}
}
//年龄
cout << "请输入年龄: " << endl;
int age = 0;
cin >> age;
abs->personArray[ret].m_Age = age;
//电话
cout << "请输入电话: " << endl;
string phone;
cin >> phone;
abs->personArray[ret].m_Phone = phone;
//住址
cout << "请输入住址: " << endl;
string address;
cin >> address;
abs->personArray[ret].m_Addr = address;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//6.清空所有联系人
void cleanPerson(Addressbooks* abs)
{
while (true)
{
cout << "请问您是否真的要清空所有联系人?" << endl;
int cho = 0;
cout << "是,请输入1。 否,请输入2" << endl;
cin >> cho;
if (cho == 1)
{
abs->m_Size = 0;
cout << "通讯录已清空" << endl;
break;
}
if (cho == 2)
{
break;
system("pause");
system("cls");
}
else
{
cout << "输入错误,请重新输入" << endl;
}
}
system("pause");
system("cls");
}
//菜单界面
void showMenu()
{
//创建通讯录结构体变量
cout << "***** 1.添加联系人 *****" << endl;
cout << "***** 2.显示联系人 *****" << endl;
cout << "***** 3.删除联系人 *****" << endl;
cout << "***** 4.查找联系人 *****" << endl;
cout << "***** 5.修改联系人 *****" << endl;
cout << "***** 6.清空联系人 *****" << endl;
cout << "***** 0.退出通讯录 *****" << endl;
}
int main()
{
int select = 0;
Addressbooks abs;
while(true)
{
showMenu();
cin >> select;
switch (select)
{
case 1://添加
addPerson(&abs);//利用地址传递可以改变值
break;
case 2://显示
showPerson(&abs);
break;
case 3://删除
deletePerosn(&abs);
break;
case 4://查找
findPerson(&abs);
break;
case 5://修改
modifyPerson(&abs);
break;
case 6://清空
cleanPerson(&abs);
break;
case 0://退出
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
}
如果对您有用,给我点赞收藏吧!