今天是接触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 deletePhoneN