通讯录管理系统(C++)
前言
本程序采用VS2019开发实现
博主尽可能完善并健壮了程序功能,如有不理想之处,请加以指正,博主会在第一时间修改完善。原创不易,望"推荐","关注","收藏","转发",谢谢!!!
网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、小程序设计、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了柯桥免费建站欢迎大家使用!
代码
点击查看代码
#include
#include
#include
using namespace std;
#define MAX 10
// 联系人结构体
struct Person {
// 姓名
string m_name;
// 性别 1--男;2--女
int m_sex = 0;
// 年龄
int m_age = 0;
// 电话号码
string m_phoneNum;
// 地址
string m_Address;
};
// 通讯录结构体
struct AddressBook {
// 通讯录中保存联系人的数组
struct Person person[MAX];
// 通讯录中当前联系人的个数
int m_Size = 0;
};
// 正则表达式匹配
bool match(string input, string express) {
// 匹配结果存放声明
smatch results;
regex pattern(express);
return regex_search(input, results, pattern);
}
// 判断姓名
bool jugeName(AddressBook* abs, string name) {
bool ret = match(name, "^([\xa1-\xff]{4,8}(@[\xa1-\xff]{4,8}){0,11})$|^([A-Z][a-z]{2,27}( [A-Z][a-z]{2,27}){0,11})$");
if (abs->m_Size == 0) {
if (ret) return true;
else return false;
}else {
if (ret) {
for (int i = 0; i < abs->m_Size; i++) {
if (abs->person[i].m_name._Equal(name)) return false;
}
}
}
return true;
}
// 判断电话号码
bool jugePhoneNum(AddressBook* abs, string phoneNum) {
bool ret = match(phoneNum, "^1(3[0-9]|4[0]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])[0-9]{8}$");
if (abs->m_Size == 0) {
if (ret) return true;
else return false;
}
else {
if (ret) {
for (int i = 0; i < abs->m_Size; i++) {
if (abs->person[i].m_phoneNum._Equal(phoneNum)) return false;
}
}
}
return true;
}
// 判断年龄
bool jugeAge(string age) {
if (match(age, "^([7-9]|[1-9][0-9]|1[01][0-9]|1[23]0)$")) return true;
return false;
}
// 判断性别
bool jugeSex(string sex) {
if (match(sex, "^([12])$")) return true;
return false;
}
// 判断地址信息
bool jugeAddress(string address) {
if (match(address, "^([^省]+省|.+自治区|[^市]+市)([^自治州]+自治州|[^市]+市|[^县]+县|[^区]+区|[^盟]+盟|[^地区]+地区|.+区划)([^市]+市|[^镇]+镇|[^县]+县|[^旗]+旗|.+区)$")) return true;
return false;
}
// 判断联系人是否存在(存在返回下标,不存在返回-1)
int IsExist(const AddressBook* const abs, string name) {
for (int i = 0; i < abs->m_Size; i++) {
// 找到用户输入的姓名了
if (abs->person[i].m_name._Equal(name)) {
return i;
}
}
return -1;
}
// 错误信息
void ErrorMessage() {
cout << "输入格式不合理,请按要求重新输入..." << endl << endl;
}
// 显示具体联系人信息
void showSpecificPersonMessage(struct Person person) {
setlocale(LC_ALL, "Zh-Hans"); // 地域设置
wchar_t nameChar[2] = L"·";
cout << "姓名:";
for (unsigned int i = 0; i < person.m_name.length(); i++) {
if (person.m_name[i] == '@') wcout << nameChar[0];
else cout << person.m_name[i];
}
cout<< "\t\t性别:" << (person.m_sex == 1 ? "男" : "女")
<< "\t年龄:" << person.m_age
<< "\t电话号码:" << person.m_phoneNum
<< "\t地址:" << person.m_Address << endl << endl;
}
// 功能:健壮姓名的输入
bool formatName(AddressBook* abs, int Num) {
while (true) {
string name;
cout << "请输入目标姓名(如需停止输入,请输入\"q\"):";
getline(cin, name);
if (!name._Equal("q")) {
if (jugeName(abs, name)) {
abs->person[Num].m_name = name;
cout << "该姓名可以录入..." << endl << endl;
break;
}
if (abs->m_Size == 0) ErrorMessage();
else cout << "存在该姓名,不能录入..." << endl << endl;
}else return false;
}
return true;
}
// 功能:健壮性别的输入
void formatSex(AddressBook* abs, int Num) {
string sex;
while (true) {
cout << "请输入目标性别(格式:1--男 2--女):";
getline(cin, sex);
if (jugeSex(sex)) {
abs->person[Num].m_sex = atoi(sex.c_str());
break;
}
ErrorMessage();
}
}
// 功能:健壮年龄输入
void formatAge(AddressBook* abs, int Num) {
while (true) {
string age;
cout << "请输入目标年龄:";
getline(cin, age);
if (jugeAge(age)) {
abs->person[Num].m_age = atoi(age.c_str());
break;
}
ErrorMessage();
}
}
// 功能:健壮电话号码输入
bool formatePhoneNum(AddressBook* abs, int Num) {
while (true) {
string phoneNum;
cout << "请输入目标电话号码(如需停止输入,请输入\"q\"):";
getline(cin, phoneNum);
if (!phoneNum._Equal("q")) {
if (jugePhoneNum(abs, phoneNum)) {
abs->person[Num].m_phoneNum = phoneNum;
cout << "该电话号码可以录入..." << endl << endl;
break;
}
if (abs->m_Size == 0) ErrorMessage();
else cout << "存在该电话号码,不能录入..." << endl << endl;
}else return false;
}
return true;
}
// 功能:健壮地址信息输入
void formateAddress(AddressBook* abs, int Num) {
while (true) {
string address;
cout << "请输入目标地址信息:";
getline(cin, address);
if (jugeAddress(address)) {
abs->person[Num].m_Address = address;
break;
}
ErrorMessage();
}
}
// 功能:健壮输入信息
bool formatInputMessage(AddressBook* abs) {
// 姓名
bool ret1 = formatName(abs, abs->m_Size);
if (ret1) {
// 性别
formatSex(abs, abs->m_Size);
// 年龄
formatAge(abs, abs->m_Size);
}else return false;
// 电话号码
bool ret2 = formatePhoneNum(abs, abs->m_Size);
if (ret2) {
// 地址
formateAddress(abs, abs->m_Size);
}else return false;
return true;
}
// 1.添加联系人
void addPerson(AddressBook* abs) {
// 判断通讯录是否已满,如果满了就不再添加
if (abs->m_Size == MAX) {
cout << "通讯录已满,请删除不常用的联系人后重新添加..." << endl;
return;
}else {
// 添加具体的联系人信息
bool ret = formatInputMessage(abs);
if (ret){
abs->m_Size++;
cout << "添加成功..." << endl << endl;
}else cout << "添加失败..." << endl << endl;
}
}
// 2.显示联系人
void showPerson(const AddressBook * const abs) {
if (abs->m_Size == 0) cout << "当前记录为空..." << endl << endl;
else {
cout << "所有联系人信息如下:" << endl << endl;
for (int i = 0; i < abs->m_Size; i++) {
showSpecificPersonMessage(abs->person[i]);
}
}
}
// 3.删除联系人
bool deletePerson(AddressBook *abs, string name){
int ret = IsExist(abs, name);
if (ret != -1) {
cout << "存在该联系人..." << endl;
for (int i = ret; i < abs->m_Size; i++) {
abs->person[i] = abs->person[i + 1];
}
abs->m_Size--;
}else {
cout << "查无此人..." << endl;
return false;
}
return true;
}
// 4.查找联系人
void findByName(const AddressBook* const abs, string name) {
int ret = IsExist(abs, name);
if (ret != -1) {
cout << endl;
cout << "存在该联系人,信息如下..." << endl;
showSpecificPersonMessage(abs->person[ret]);
}else {
cout << "查无此人..." << endl;
}
}
// 封装修改函数
void modifyPersonInner(AddressBook* abs, int ret) {
cout << "联系人信息包含以下几项:" << endl;
cout << "1.姓名\t2.性别\t3.年龄\t4.电话号码\t5.地址" << endl << "注意:输入数字0则保存修改信息并返回主菜单..." << endl << endl;
while (true){
string Num;
cout << "请输入要修改信息的数字编号:";
getline(cin, Num);
if (Num._Equal("1")) {
formatName(abs, ret);
}else if (Num._Equal("2")) {
formatSex(abs, ret);
}else if (Num._Equal("3")) {
formatAge(abs, ret);
}else if (Num._Equal("4")) {
formatePhoneNum(abs, ret);
}else if (Num._Equal("5")) {
formateAddress(abs, ret);
}else if (Num._Equal("0")) {
cout << "修改成功..." << endl << endl;
cout << "修改后的信息如下:" << endl;
showSpecificPersonMessage(abs->person[ret]);
break;
}else ErrorMessage();
}
}
// 5.修改联系人
void modifyPersonOuter(AddressBook* abs, string name) {
int ret = IsExist(abs, name);
if (ret != -1) {
cout << endl;
cout << "存在该联系人,信息如下..." << endl;
showSpecificPersonMessage(abs->person[ret]);
cout << endl;
cout << "********** 进入修改页面 ************" << endl;
modifyPersonInner(abs, ret);
}
else {
cout << "查无此人..." << endl;
}
}
// 6.清空联系人
void clearAll(AddressBook *abs) {
if (abs->m_Size == 0) return;
while (true) {
string SURE;
cout << "确定要清空通讯录吗?(Y/N):";
getline(cin, SURE);
if (SURE.length() == 1) {
if (SURE._Equal("y") || SURE._Equal("Y")) {
cout << "您选择YES,请稍等..." << endl;
abs->m_Size = 0;
cout << "记录清空成功..." << endl << endl;
break;
}else if (SURE._Equal("N") || SURE._Equal("n")) {
cout << "您选择NO, 请稍等..." << endl << endl;
break;
}
}
ErrorMessage();
}
}
// 显示菜单
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;
cout << "注意:\n1.姓名不能重复且只能输入 [@,空格] 两种特殊字符,如姓名中有[·],请以[@]代替\n" << "2.性别:1--男,2--女\n"
<< "3.手机号码不能重复且需符合国家规定\n"
<< "4.年龄为 7 - 130 之间的整数\n" << "5.地址信息:标准的 [省市区,市区镇,市县镇]...\n\n";
}
int main() {
// 创建通讯录结构体变量
AddressBook abs;
while (true) {
showMenu();
// 输入通讯录的功能编号
string Num;
while (true) {
cout << "请输入选择的功能编号:";
getline(cin, Num);
if (match(Num, "^([0-6])$")) break;
ErrorMessage();
}
switch (atoi(Num.c_str())) {
case 1: // 1.添加联系人
{
addPerson(&abs); // 利用地址传递可以修改实参
showPerson(&abs);
system("pause");
system("cls");
}
break;
case 2: // 2.显示联系人
{
showPerson(&abs);
system("pause");
system("cls");
}
break;
case 3: // 3.删除联系人
{
if (abs.m_Size != 0) {
string name;
cout << "请输入要删除的联系人姓名:";
getline(cin, name);
bool ret = deletePerson(&abs, name);
if (ret == true) {
cout << "删除成功..." << endl << endl;
showPerson(&abs);
}else {
cout << "删除失败..." << endl << endl;
}
}else cout << "当前记录为空..." << endl << endl;
system("pause");
system("cls");
}
break;
case 4: // 4.查找联系人
{
if (abs.m_Size != 0) {
string name;
cout << "请输入要查找的联系人姓名:";
getline(cin, name);
findByName(&abs, name);
}else cout << "当前记录为空..." << endl << endl;
system("pause");
system("cls");
}
break;
case 5: // 5.修改联系人
{
showPerson(&abs);
if (abs.m_Size != 0) {
string name;
cout << "请输入要修改的联系人姓名:";
getline(cin, name);
modifyPersonOuter(&abs, name);
}
system("pause");
system("cls");
}
break;
case 6: // 6.清空联系人
{
clearAll(&abs);
showPerson(&abs);
cout << "正在返回主菜单, ";
system("pause");
system("cls");
}
break;
case 0: // 0.退出通讯录
cout << "欢迎下次使用..." << endl;
system("pause");
return 0;
break;
}
}
return 0;
}
网站标题:通讯录管理系统(C++)
网站链接:http://azwzsj.com/article/dsogoes.html