#include <iostream>
#include <string>
#include <string.h>
using namespace std;
#define MAX 1000
typedef struct _PERSON
{
string name;
int sex;
int age;
string phoneNum;
string address;
} PERSON;
typedef struct _ADDRESS_BOOK
{
PERSON personArray[MAX];
int personNum;
} ADDRESS_BOOK;
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 personAdd(ADDRESS_BOOK *addressBook)
{
string name = "";
int sex = 0;
int age = 0;
string phoneNum = "";
string address = "";
if (addressBook->personNum >= MAX)
{
cout << "通讯录已满" << endl;
return;
}
cout << "请输入姓名" << endl;
cin >> name;
addressBook->personArray[addressBook->personNum].name = name;
cout << "请输入性别" << endl;
cout << "1-男性" << endl;
cout << "2-女性" << endl;
cin >> sex;
addressBook->personArray[addressBook->personNum].sex = sex;
cout << "请输入年龄" << endl;
cin >> age;
addressBook->personArray[addressBook->personNum].age = age;
cout << "请输入电话号码" << endl;
cin >> phoneNum;
addressBook->personArray[addressBook->personNum].phoneNum = phoneNum;
cout << "请输入地址" << endl;
cin >> address;
addressBook->personArray[addressBook->personNum].address = address;
addressBook->personNum++;
cout << "添加完毕" << endl;
}
void personShow(ADDRESS_BOOK addressBook)
{
int i = 0;
if (0 == addressBook.personNum)
{
cout << "通讯录为空" << endl;
return;
}
for (i = 0; i < addressBook.personNum; i++)
{
cout << "姓名:" << addressBook.personArray[i].name << "\t";
cout << "性别:" << (addressBook.personArray[i].sex == 1 ? "男" : "女") << "\t";
cout << "年纪:" << addressBook.personArray[i].age << "\t";
cout << "电话号码:" << addressBook.personArray[i].phoneNum << "\t";
cout << "地址:" << addressBook.personArray[i].address << endl;
}
}
int _isExist(ADDRESS_BOOK addressBook, string name)
{
int i = 0;
for (i = 0; i < addressBook.personNum; i++)
{
if (addressBook.personArray[i].name == name)
{
return i;
}
}
return -1;
}
void personDel(ADDRESS_BOOK *addressBook)
{
string name = "";
int ret = 0;
int i = 0;
cout << "请输入要删除的姓名" << endl;
cin >> name;
ret = _isExist(*addressBook, name);
if (-1 == ret)
{
cout << "查无此人" << endl;
return;
}
for (i = ret; i < addressBook->personNum - 1; i++)
{
addressBook->personArray[i] = addressBook->personArray[i + 1];
}
addressBook->personNum--;
cout << "删除联系人成功!" << endl;
}
void personFind(ADDRESS_BOOK addressBook)
{
string name = "";
int ret = 0;
cout << "请输入姓名" << endl;
cin >> name;
ret = _isExist(addressBook, name);
if (-1 == ret)
{
cout << "未找到此人" << endl;
return;
}
cout << "姓名:" << addressBook.personArray[ret].name << "\t";
cout << "性别:" << (addressBook.personArray[ret].sex == 1 ? "男" : "女") << "\t";
cout << "年纪:" << addressBook.personArray[ret].age << "\t";
cout << "电话号码:" << addressBook.personArray[ret].phoneNum << "\t";
cout << "地址:" << addressBook.personArray[ret].address << endl;
}
void personModify(ADDRESS_BOOK *addressBook)
{
string name = "";
int sex = 0;
int age = 0;
string phoneNum = "";
string address = "";
int ret = 0;
cout << "请输入姓名" << endl;
cin >> name;
ret = _isExist(*addressBook, name);
if (-1 == ret)
{
cout << "未找到此人" << endl;
return;
}
cout << "请输入姓名" << endl;
cin >> name;
addressBook->personArray[ret].name = name;
cout << "请输入性别" << endl;
cout << "1-男性" << endl;
cout << "2-女性" << endl;
cin >> sex;
addressBook->personArray[ret].sex = sex;
cout << "请输入年龄" << endl;
cin >> age;
addressBook->personArray[ret].age = age;
cout << "请输入电话号码" << endl;
cin >> phoneNum;
addressBook->personArray[ret].phoneNum = phoneNum;
cout << "请输入地址" << endl;
cin >> address;
addressBook->personArray[ret].address = address;
}
void personClean(ADDRESS_BOOK *addressBook)
{
addressBook->personNum = 0;
}
int main(void)
{
int select = 0;
ADDRESS_BOOK addressBook;
memset(&addressBook, 0, sizeof(addressBook));
cout << addressBook.personNum << endl;
while (true)
{
showMenu();
cin >> select;
switch (select)
{
case 1:
personAdd(&addressBook);
break;
case 2:
personShow(addressBook);
break;
case 3:
personDel(&addressBook);
break;
case 4:
personFind(addressBook);
break;
case 5:
personModify(&addressBook);
break;
case 6:
personClean(&addressBook);
break;
case 0:
cout << "欢迎下次使用" << endl;
return 0;
break;
default:
break;
}
}
return 0;
}
通讯录管理系统_C++
最新推荐文章于 2024-11-18 18:38:00 发布