前言
本程式采用VS2019開發實作
博主盡可能完善并健壯了程式功能,如有不理想之處,請加以指正,博主會在第一時間修改完善,原創不易,望"推薦","關注","收藏","轉發",謝謝!!!
代碼
點擊查看代碼
#include<iostream>
#include<string>
#include<regex>
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[01456879]|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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/423781.html
標籤:C++
下一篇:二叉樹的基本操作(C語言版)
