十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
本程序采用VS2019開(kāi)發(fā)實(shí)現(xiàn)
博主盡可能完善并健壯了程序功能,如有不理想之處,請(qǐng)加以指正,博主會(huì)在第一時(shí)間修改完善。原創(chuàng)不易,望"推薦","關(guān)注","收藏","轉(zhuǎn)發(fā)",謝謝!!!
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序設(shè)計(jì)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了柯橋免費(fèi)建站歡迎大家使用!
#include
#include
#include
using namespace std;
#define MAX 10
// 聯(lián)系人結(jié)構(gòu)體
struct Person {
// 姓名
string m_name;
// 性別 1--男;2--女
int m_sex = 0;
// 年齡
int m_age = 0;
// 電話號(hào)碼
string m_phoneNum;
// 地址
string m_Address;
};
// 通訊錄結(jié)構(gòu)體
struct AddressBook {
// 通訊錄中保存聯(lián)系人的數(shù)組
struct Person person[MAX];
// 通訊錄中當(dāng)前聯(lián)系人的個(gè)數(shù)
int m_Size = 0;
};
// 正則表達(dá)式匹配
bool match(string input, string express) {
// 匹配結(jié)果存放聲明
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;
}
// 判斷電話號(hào)碼
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, "^([^省]+省|.+自治區(qū)|[^市]+市)([^自治州]+自治州|[^市]+市|[^縣]+縣|[^區(qū)]+區(qū)|[^盟]+盟|[^地區(qū)]+地區(qū)|.+區(qū)劃)([^市]+市|[^鎮(zhèn)]+鎮(zhèn)|[^縣]+縣|[^旗]+旗|.+區(qū))$")) return true;
return false;
}
// 判斷聯(lián)系人是否存在(存在返回下標(biāo),不存在返回-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;
}
// 錯(cuò)誤信息
void ErrorMessage() {
cout << "輸入格式不合理,請(qǐng)按要求重新輸入..." << endl << endl;
}
// 顯示具體聯(lián)系人信息
void showSpecificPersonMessage(struct Person person) {
setlocale(LC_ALL, "Zh-Hans"); // 地域設(shè)置
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電話號(hào)碼:" << person.m_phoneNum
<< "\t地址:" << person.m_Address << endl << endl;
}
// 功能:健壯姓名的輸入
bool formatName(AddressBook* abs, int Num) {
while (true) {
string name;
cout << "請(qǐng)輸入目標(biāo)姓名(如需停止輸入,請(qǐng)輸入\"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 << "請(qǐng)輸入目標(biāo)性別(格式: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 << "請(qǐng)輸入目標(biāo)年齡:";
getline(cin, age);
if (jugeAge(age)) {
abs->person[Num].m_age = atoi(age.c_str());
break;
}
ErrorMessage();
}
}
// 功能:健壯電話號(hào)碼輸入
bool formatePhoneNum(AddressBook* abs, int Num) {
while (true) {
string phoneNum;
cout << "請(qǐng)輸入目標(biāo)電話號(hào)碼(如需停止輸入,請(qǐng)輸入\"q\"):";
getline(cin, phoneNum);
if (!phoneNum._Equal("q")) {
if (jugePhoneNum(abs, phoneNum)) {
abs->person[Num].m_phoneNum = phoneNum;
cout << "該電話號(hào)碼可以錄入..." << endl << endl;
break;
}
if (abs->m_Size == 0) ErrorMessage();
else cout << "存在該電話號(hào)碼,不能錄入..." << endl << endl;
}else return false;
}
return true;
}
// 功能:健壯地址信息輸入
void formateAddress(AddressBook* abs, int Num) {
while (true) {
string address;
cout << "請(qǐng)輸入目標(biāo)地址信息:";
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;
// 電話號(hào)碼
bool ret2 = formatePhoneNum(abs, abs->m_Size);
if (ret2) {
// 地址
formateAddress(abs, abs->m_Size);
}else return false;
return true;
}
// 1.添加聯(lián)系人
void addPerson(AddressBook* abs) {
// 判斷通訊錄是否已滿,如果滿了就不再添加
if (abs->m_Size == MAX) {
cout << "通訊錄已滿,請(qǐng)刪除不常用的聯(lián)系人后重新添加..." << endl;
return;
}else {
// 添加具體的聯(lián)系人信息
bool ret = formatInputMessage(abs);
if (ret){
abs->m_Size++;
cout << "添加成功..." << endl << endl;
}else cout << "添加失敗..." << endl << endl;
}
}
// 2.顯示聯(lián)系人
void showPerson(const AddressBook * const abs) {
if (abs->m_Size == 0) cout << "當(dāng)前記錄為空..." << endl << endl;
else {
cout << "所有聯(lián)系人信息如下:" << endl << endl;
for (int i = 0; i < abs->m_Size; i++) {
showSpecificPersonMessage(abs->person[i]);
}
}
}
// 3.刪除聯(lián)系人
bool deletePerson(AddressBook *abs, string name){
int ret = IsExist(abs, name);
if (ret != -1) {
cout << "存在該聯(lián)系人..." << endl;
for (int i = ret; i < abs->m_Size; i++) {
abs->person[i] = abs->person[i + 1];
}
abs->m_Size--;
}else {
cout << "查無(wú)此人..." << endl;
return false;
}
return true;
}
// 4.查找聯(lián)系人
void findByName(const AddressBook* const abs, string name) {
int ret = IsExist(abs, name);
if (ret != -1) {
cout << endl;
cout << "存在該聯(lián)系人,信息如下..." << endl;
showSpecificPersonMessage(abs->person[ret]);
}else {
cout << "查無(wú)此人..." << endl;
}
}
// 封裝修改函數(shù)
void modifyPersonInner(AddressBook* abs, int ret) {
cout << "聯(lián)系人信息包含以下幾項(xiàng):" << endl;
cout << "1.姓名\t2.性別\t3.年齡\t4.電話號(hào)碼\t5.地址" << endl << "注意:輸入數(shù)字0則保存修改信息并返回主菜單..." << endl << endl;
while (true){
string Num;
cout << "請(qǐng)輸入要修改信息的數(shù)字編號(hào):";
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.修改聯(lián)系人
void modifyPersonOuter(AddressBook* abs, string name) {
int ret = IsExist(abs, name);
if (ret != -1) {
cout << endl;
cout << "存在該聯(lián)系人,信息如下..." << endl;
showSpecificPersonMessage(abs->person[ret]);
cout << endl;
cout << "********** 進(jìn)入修改頁(yè)面 ************" << endl;
modifyPersonInner(abs, ret);
}
else {
cout << "查無(wú)此人..." << endl;
}
}
// 6.清空聯(lián)系人
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,請(qǐng)稍等..." << endl;
abs->m_Size = 0;
cout << "記錄清空成功..." << endl << endl;
break;
}else if (SURE._Equal("N") || SURE._Equal("n")) {
cout << "您選擇NO, 請(qǐng)稍等..." << endl << endl;
break;
}
}
ErrorMessage();
}
}
// 顯示菜單
void showMenu() {
cout << "**********************" << endl;
cout << "**** 1.添加聯(lián)系人 ****" << endl;
cout << "**** 2.顯示聯(lián)系人 ****" << endl;
cout << "**** 3.刪除聯(lián)系人 ****" << endl;
cout << "**** 4.查找聯(lián)系人 ****" << endl;
cout << "**** 5.修改聯(lián)系人 ****" << endl;
cout << "**** 6.清空聯(lián)系人 ****" << endl;
cout << "**** 0.退出通訊錄 ****" << endl;
cout << "**********************" << endl;
cout << "注意:\n1.姓名不能重復(fù)且只能輸入 [@,空格] 兩種特殊字符,如姓名中有[·],請(qǐng)以[@]代替\n" << "2.性別:1--男,2--女\n"
<< "3.手機(jī)號(hào)碼不能重復(fù)且需符合國(guó)家規(guī)定\n"
<< "4.年齡為 7 - 130 之間的整數(shù)\n" << "5.地址信息:標(biāo)準(zhǔn)的 [省市區(qū),市區(qū)鎮(zhèn),市縣鎮(zhèn)]...\n\n";
}
int main() {
// 創(chuàng)建通訊錄結(jié)構(gòu)體變量
AddressBook abs;
while (true) {
showMenu();
// 輸入通訊錄的功能編號(hào)
string Num;
while (true) {
cout << "請(qǐng)輸入選擇的功能編號(hào):";
getline(cin, Num);
if (match(Num, "^([0-6])$")) break;
ErrorMessage();
}
switch (atoi(Num.c_str())) {
case 1: // 1.添加聯(lián)系人
{
addPerson(&abs); // 利用地址傳遞可以修改實(shí)參
showPerson(&abs);
system("pause");
system("cls");
}
break;
case 2: // 2.顯示聯(lián)系人
{
showPerson(&abs);
system("pause");
system("cls");
}
break;
case 3: // 3.刪除聯(lián)系人
{
if (abs.m_Size != 0) {
string name;
cout << "請(qǐng)輸入要?jiǎng)h除的聯(lián)系人姓名:";
getline(cin, name);
bool ret = deletePerson(&abs, name);
if (ret == true) {
cout << "刪除成功..." << endl << endl;
showPerson(&abs);
}else {
cout << "刪除失敗..." << endl << endl;
}
}else cout << "當(dāng)前記錄為空..." << endl << endl;
system("pause");
system("cls");
}
break;
case 4: // 4.查找聯(lián)系人
{
if (abs.m_Size != 0) {
string name;
cout << "請(qǐng)輸入要查找的聯(lián)系人姓名:";
getline(cin, name);
findByName(&abs, name);
}else cout << "當(dāng)前記錄為空..." << endl << endl;
system("pause");
system("cls");
}
break;
case 5: // 5.修改聯(lián)系人
{
showPerson(&abs);
if (abs.m_Size != 0) {
string name;
cout << "請(qǐng)輸入要修改的聯(lián)系人姓名:";
getline(cin, name);
modifyPersonOuter(&abs, name);
}
system("pause");
system("cls");
}
break;
case 6: // 6.清空聯(lián)系人
{
clearAll(&abs);
showPerson(&abs);
cout << "正在返回主菜單, ";
system("pause");
system("cls");
}
break;
case 0: // 0.退出通訊錄
cout << "歡迎下次使用..." << endl;
system("pause");
return 0;
break;
}
}
return 0;
}