整体代码十分简单,但有一点值得初学者注意,比如输入年龄的时候,应该存入一个整形的数字,此时就需要判断键盘输入的数据类型是否为整形,当数据类型不是整形的时候,就cout一条字符串告诉用户输入内容格式不正确,重新输入。
通过正则表达式来实现这个判别功能。代码如下:
在代码开头需加上#include "regex"
bool IsRegexInput(string str) // 使用正则表达式进行匹配,
{
bool flag;
flag = true;
regex r("\\d{1,3}");
while (!(flag = regex_match(str, r)))
{
return flag;
}
return flag;
}
?下面是功能函数的代码片段(以输入年龄为例),
string age;
int i_age;
cout << "请输入年龄:" << endl;
cin >> age;
while (!IsRegexInput(age)) // 判断输入的内容是否为整形
{
cout << "输入内容格式有误,请输入数字:" << endl;
cin >> age;
}
i_age = atoi(age.c_str());//输入没有问题,进行字符串转换为整形
abs->person[abs->renshu].age = i_age;
全部代码如下:
#include <iostream>
#include <string>
#include <cstdlib>
#include "regex"
using namespace std;
#define max 100
struct lianxiren
{
string name;
int sex;
int age;
string mobile_number;
string address;
};
struct tongxunlu
{
struct lianxiren person[max];
int renshu=0;
};
bool IsRegexInput(string str) // 使用正则表达式进行匹配,
{
bool flag;
flag = true;
regex r("\\d{1,3}");
while (!(flag = regex_match(str, r)))
{
return flag;
}
return flag;
}
void show_caidan()
{
cout << "* 1.添加联系人 *" << endl;
cout << "* 2.显示联系人 *" << endl;
cout << "* 3.删除联系人 *" << endl;
cout << "* 4.查找联系人 *" << endl;
cout << "* 5.修改联系人 *" << endl;
cout << "* 6.清空联系人 *" << endl;
cout << "* 7.退出通讯录 *" << endl;
}
void add_person(tongxunlu * abs)
{
if (abs->renshu > max)
{
cout << "通讯录已满" << endl;
system("pause");
}
else
{
cout << "请输入姓名:" << endl;
string name;
cin >> name;
abs->person[abs->renshu].name = name;
cout << "请输入性别:1=男 2=女" << endl;
string sex;
int i_sex;
while (true)
{
cin >> sex;
while (!IsRegexInput(sex)) // 判断输入的内容是否为整形
{
cout << "输入内容格式有误,请输入数字:"<< endl;
cin >> sex;
}
i_sex = atoi(sex.c_str());//输入没有问题,进行字符串转换为整形
if (i_sex == 1 || i_sex == 2)
{
abs->person[abs->renshu].sex = i_sex;
break;
}
else
{
cout << "请输入正确的性别 :)";
}
}
string age;
int i_age;
cout << "请输入年龄:" << endl;
cin >> age;
while (!IsRegexInput(age)) // 判断输入的内容是否为整形
{
cout << "输入内容格式有误,请输入数字:" << endl;
cin >> age;
}
i_age = atoi(age.c_str());//输入没有问题,进行字符串转换为整形
abs->person[abs->renshu].age = i_age;
cout << "请输入手机号码:" << endl;
string number;
cin >> number;
abs->person[abs->renshu].mobile_number = number;
cout << "请输入地址:" << endl;
string address;
cin >> address;
abs->person[abs->renshu].address = address;
abs->renshu++;
cout << "已成功添加联系人 :)" << endl;
system("pause");
system("cls");
}
}
int is_exist(tongxunlu * abs)
{
if (abs->renshu == 0)
{
cout << "通讯录为空" << endl;
return 0;
}
return 1;
}
void cout_person(tongxunlu * abs,int i)
{
string sex;
if (abs->person[i].sex == 1)
{
sex = "男";
}
else
{
sex = "女";
}
cout << abs->person[i].name
<< "\t" << sex
<< "\t" << abs->person[i].age
<< "\t" << abs->person[i].mobile_number
<< "\t" << abs->person[i].address << endl;
}
void show_person(tongxunlu * abs)
{
is_exist(abs);
for (int i = 0; i < abs->renshu; i++)
{
cout_person(abs, i);;
}
system("pause");
system("cls");
}
void delete_person(tongxunlu* abs)
{
int flag = is_exist(abs);
if (flag == 1)
{
string name;
int i;
cout << "请输入要删除的联系人姓名:" << endl;
cin >> name;
if (name != "-1")
{
for (i = 0; i < abs->renshu; i++)
{
if (abs->person[i].name == name)
{
for (int j = i; i < abs->renshu - 1; j++)
{
abs->person[j] = abs->person[j + 1];
}
abs->renshu--;
cout << "已成功删除该联系人" << endl;
break;
}
}
if (i >= abs->renshu)
{
cout << "该联系人不存在" << endl;
}
}
}
system("pause");
system("cls");
}
void locate_person(tongxunlu* abs)
{
is_exist(abs);
string name;
cout << "请输入要查找的联系人姓名:" << endl;
cin >> name;
for (int i = 0; i < abs->renshu; i++)
{
if (abs->person[i].name == name)
{
cout_person(abs,i);
break;
}
}
system("pause");
system("cls");
}
void modify_person(tongxunlu* abs)
{
int flag=is_exist(abs);
if (flag == 1)
{
string l_name;
int i;
cout << "请输入要修改的联系人姓名:" << endl;
cin >> l_name;
for (i = 0; i < abs->renshu; i++)
{
if (abs->person[i].name == l_name)
{
cout << "修改前联系人的信息如下:" << endl;
cout_person(abs, i);
cout << "不需要修改的项可以输入-1跳过" << endl;
cout << "请输入姓名:" << endl;
string name;
cin >> name;
if (name != "-1")
{
abs->person[i].name = name;
}
cout << "请输入性别:1=男 2=女" << endl;
string sex;
int i_sex;
cin >> sex;
if (sex != "-1")
{
while (!(IsRegexInput(sex)))
{
cout << "输入内容格式有误,请输入数字:" << endl;
cin >> sex;
}
i_sex = atoi(sex.c_str());
while (true)
{
cin >> sex;
if (i_sex == 1 || i_sex == 2)
{
abs->person[i].sex = i_sex;
break;
}
else
{
cout << "请输入正确的性别 :)";
}
}
}
string age;
int i_age;
cout << "请输入年龄:(输入-1跳过)" << endl;
cin >> age;
if (age != "-1")
{
while (!(IsRegexInput(age)))
{
cout << "输入内容格式有误,请输入数字:" << endl;
cin >> age;
}
i_age = atoi(age.c_str());
abs->person[i].age = i_age;
}
cout << "请输入手机号码:" << endl;
string number;
cin >> number;
if (number != "-1")
{
abs->person[i].mobile_number = number;
}
cout << "请输入地址:" << endl;
string address;
cin >> address;
if (address != "-1")
{
abs->person[i].address = address;
}
cout << "已成功修改联系人 :)" << endl;
cout << "修改后的联系人信息如下:" << endl;
cout_person(abs, i);
break;
}
}
if (i >= abs->renshu)
{
cout << "该联系人不存在" << endl;
}
}
system("pause");
system("cls");
}
void clear_person(tongxunlu* abs)
{
abs->renshu = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
int main()
{
struct tongxunlu li1;
int select = 0;
while (true)
{
show_caidan();
cout << "请输入功能号:" << endl;
cin >> select;
switch (select)
{
case 1:
add_person(&li1);
break;
case 2:
show_person(&li1);
break;
case 3:
delete_person(&li1);
break;
case 4:
locate_person(&li1);
break;
case 5:
modify_person(&li1);
break;
case 6:
clear_person(&li1);
break;
case 7:
cout << "欢迎下次使用 :)" << endl;
system("pause");
return 0;
/*exit(0); */
default:
break;
}
}
system("pause");
return 0;
}
希望对你有所帮助。
|