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