今天是接觸C++的第二天,學習了基礎內容之後用了兩個多小時的時間完成了一個通訊錄管理程式,功能相對簡單,代碼也不複雜,歡迎各位大佬指出不足之處 點擊查看代碼 #include<iostream> #include<string> #include<regex> using namespace std ...
今天是接觸C++的第二天,學習了基礎內容之後用了兩個多小時的時間完成了一個通訊錄管理程式,功能相對簡單,代碼也不複雜,歡迎各位大佬指出不足之處
點擊查看代碼
#include<iostream>
#include<string>
#include<regex>
using namespace std;
struct PhoneNumber
{
string name;//姓名
string sex;//性別
string phone;//聯繫電話
string area;//地址
};
//顯示菜單項
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 addPhoneNumber(PhoneNumber phoneNumber[]) {
string name;
string sex;
string phone;
string area;
string n;//判斷性別
bool flag = false;//判斷控制台正確輸入
cout << "請輸入姓名:" << endl;
cin >> name;
cout << "請選擇性別:\n" << "1-男\n" << "2-女" << endl;
do
{
cin >> n;
cin.clear();
cin.sync();
if (n == "1" || n == "2")
{
flag = true;
}
else
{
flag = false;
cout << "請輸入1或2:" << endl;
}
} while (!flag && cin.get());
if (n == "1") sex = "男";
else if (n == "2") sex = "女";
cout << "請輸入手機號:" << endl;
do
{
cin >> phone;
cin.clear();
cin.sync();
regex e("^(13[0-9]|14[5-9]|15[0-3,5-9]|16[2,5,6,7]|17[0-8]|18[0-9]|19[0-3,5-9])\\d{8}$");//手機號校驗
if (regex_match(phone, e))
{
flag = true;
}
else
{
flag = false;
cout << "請輸入合法的手機號:" << endl;
}
} while (!flag && cin.get());
cout << "請輸入地址:" << endl;
cin >> area;
for (int i = 0; i < 100; i++) {
if (phoneNumber[i].phone == phone) {
cout << "手機號已存在,請核對後重新錄入!" << endl;//保持手機號唯一性
system("pause");
addPhoneNumber(phoneNumber);
}
if (phoneNumber[i].phone.empty()) {
phoneNumber[i].name = name;
phoneNumber[i].sex = sex;
phoneNumber[i].phone = phone;
phoneNumber[i].area = area;
break;
}
}
cout << "錄入成功" << endl;
system("pause");//按任意鍵繼續
return;
}
//查看聯繫人信息
void showPhoneNumber(PhoneNumber phone[])
{
//列名欄
cout << "------------------------------------------------------" << endl;
cout << "姓名 性別 聯繫電話 地址 " << endl;
cout << "------------------------------------------------------" << endl;
for (int i = 0; i < 100; i++)//查看全部聯繫人
{
if (!phone[i].phone.empty()) {
cout << phone[i].name << "\t " << phone[i].sex << " " << phone[i].phone << " " << phone[i].area << endl;
}
}
system("pause");
return;
}
//刪除聯繫人
void deletePhoneNumber(PhoneNumber phone[])
{
string name;
string phoneNumber;
string::size_type idx;//全匹配查詢記憶體標識
bool flag = false;//聯繫人是否存在
cout << "請輸入待刪除聯繫人姓名:" << endl;
cin >> name;
cout << "找到以下符合條件的聯繫人:" << endl;
cout << "------------------------------------------------------" << endl;
cout << "姓名 性別 聯繫電話 地址 " << endl;
cout << "------------------------------------------------------" << endl;
for (int i = 0; i < 100; i++)
{
idx = phone[i].name.find(name);//模糊查找聯繫人姓名
if (idx != string::npos) {
flag = true;
cout << phone[i].name << "\t " << phone[i].sex << " " << phone[i].phone << " " << phone[i].area << endl;
}
}
if (flag) {
cout << "請輸入待刪除聯繫人聯繫電話:" << endl;
cin >> phoneNumber;
for (int i = 0; i < 100; i++)
{
if (phone[i].phone == phoneNumber) {//清除對應下標的數組元素屬性
phone[i].name = "";
phone[i].sex = "";
phone[i].phone = "";
phone[i].area = "";
}
}
cout << "已刪除該聯繫人!" << endl;
}
else {
cout << "沒有找到相關聯繫人信息!" << endl;
}
system("pause");
return;
}
//查詢聯繫人(模糊查詢)
void viewPhoneNumber(PhoneNumber phone[])
{
string name;
string phoneNumber;
string::size_type idx;
string::size_type idx1;
string::size_type idx2;
string::size_type idx3;
cout << "請輸入待查詢聯繫人信息:" << endl;
cin >> name;
cout << "找到以下符合條件的聯繫人:" << endl;
cout << "------------------------------------------------------" << endl;
cout << "姓名 性別 聯繫電話 地址 " << endl;
cout << "------------------------------------------------------" << endl;
for (int i = 0; i < 100; i++)
{
idx = phone[i].name.find(name);
idx1 = phone[i].sex.find(name);
idx2 = phone[i].phone.find(name);
idx3 = phone[i].area.find(name);
if (idx != string::npos || idx1 != string::npos || idx2 != string::npos || idx3 != string::npos) {//根據聯繫人任意信息進行模糊查詢
cout << phone[i].name << "\t " << phone[i].sex << " " << phone[i].phone << " " << phone[i].area << endl;
}
}
system("pause");
return;
}
//修改聯繫人信息
void updatePhoneNumber(PhoneNumber phone[])
{
string name;
string phoneNumber;
string::size_type idx;
bool flag1 = false;
cout << "請輸入待修改聯繫人姓名:" << endl;
cin >> name;
cout << "找到以下符合條件的聯繫人:" << endl;
cout << "------------------------------------------------------" << endl;
cout << "姓名 性別 聯繫電話 地址 " << endl;
cout << "------------------------------------------------------" << endl;
for (int i = 0; i < 100; i++)
{
idx = phone[i].name.find(name);
if (idx != string::npos) {
flag1 = true;
cout << phone[i].name << "\t " << phone[i].sex << " " << phone[i].phone << " " << phone[i].area << endl;
}
}
if (flag1) {
cout << "請輸入待修改聯繫人聯繫電話:" << endl;
cin >> phoneNumber;
for (int i = 0; i < 100; i++)
{
if (phone[i].phone == phoneNumber) {
string name1;
string sex1;
string phone1;
string area1;
string n;//判斷性別
bool flag = false;//判斷控制台正確輸入
cout << "請輸入姓名:" << endl;
cin >> name1;
cout << "請選擇性別:\n" << "1-男\n" << "2-女" << endl;
do
{
cin >> n;
cin.clear();
cin.sync();
if (n == "1" || n == "2")
{
flag = true;
}
else
{
flag = false;
cout << "請輸入1或2:" << endl;
}
} while (!flag && cin.get());
if (n == "1") sex1 = "男";
else if (n == "2") sex1 = "女";
cout << "請輸入手機號:" << endl;
do
{
cin >> phone1;
cin.clear();
cin.sync();
regex e("^(13[0-9]|14[5-9]|15[0-3,5-9]|16[2,5,6,7]|17[0-8]|18[0-9]|19[0-3,5-9])\\d{8}$");
if (regex_match(phone1, e))
{
flag = true;
}
else
{
flag = false;
cout << "請輸入合法的手機號:" << endl;
}
} while (!flag && cin.get());
cout << "請輸入地址:" << endl;
cin >> area1;
phone[i].name = name1;
phone[i].sex = sex1;
phone[i].phone = phone1;
phone[i].area = area1;
}
}
cout << "已成功修改該聯繫人信息!" << endl;
}
else {
cout << "沒有找到相關聯繫人信息!" << endl;
}
system("pause");
return;
}
//清空通訊錄
void clearPhoneNumber(PhoneNumber phone[])
{
string n;
cout << "確認執行清空操作(yes/no)?" << endl;//清空前確認
cin >> n;
if (n == "yes") {
for (int i = 0; i < 100; i++)
{
phone[i].name = "";
phone[i].sex = "";
phone[i].phone = "";
phone[i].area = "";
}
cout << "已清空通訊錄!" << endl;
}
cout << "已取消清空操作!" << endl;
system("pause");
return;
}
int main()
{
PhoneNumber phone[100];
showMenu();
while (1)//迴圈操作
{
int item;
cout << "請根據序號選擇功能項:" << endl;
cin >> item;
switch (item)
{
case 1:
addPhoneNumber(phone);
system("cls");//清空控制台
showMenu();
break;
case 2:
showPhoneNumber(phone);
system("cls");
showMenu();
break;
case 3:
deletePhoneNumber(phone);
system("cls");
showMenu();
break;
case 4:
viewPhoneNumber(phone);
system("cls");
showMenu();
break;
case 5:
updatePhoneNumber(phone);
system("cls");
showMenu();
break;
case 6:
clearPhoneNumber(phone);
system("cls");
showMenu();
break;
case 0:
exit(0);//退出程式
default:
system("cls");
showMenu();
break;
}
}
return 0;
}