以下是我写的一个通讯录,但是它的使用仅限于一次程序执行,如何每次执行都可以使用之前的数据
//Book.h
#include
using namespace std;
constexpr auto Max = 500;
//定义联系人的结构体
struct person
{
string name;
double age = 0;
string phone;
string address;
int sex = 0;
};
//定义通讯录数组
struct adds
{
person presonarray[Max];
int size = 0;
};
class interface
{
};
//定义工具类
class tool
{
public:int is_number(string x);
double change(string x);
};
//定义通讯本
class address_book
{
public:
int compare(string name);
void showmenu(); //显示菜单
void add(); //添加联系人
void show_contacts(); //显示联系人
void seek(); //查找联系人
void revise(); //修改联系人
void all_clear(); //清空所有联系人
void delete_person(); //删除联系人
adds a;
};
//Book.cpp
#include"Book.h"
int address_book::compare(string name)
{
for (int i = 0; i < a.size; i++)
{
if (a.presonarray[i].name == name)
{
return i;
}
}
return -1;
}
//显示菜单
void address_book::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 address_book::add()
{
tool tool;
cout << "请输入联系人的姓名:" << endl;
string name;
cin >> name;
a.presonarray[a.size].name = name;
string age;
double num = 0;
do
{
cout << "年龄(输入正整数):" << endl;
cin >> age;
int x = tool.is_number(age);
if (x == 1)
{
break;
}
else
{
cout << "输入有误,请输入正整数" << endl;
}
} while (true);
num = tool.change(age);
a.presonarray[a.size].age = num;
while (true)
{
cout << "性别: 1、男 2、女(输入数字)" << endl;
int sex = 0;
cin >> sex;
if (sex == 1 || sex == 2)
{
a.presonarray[a.size].sex = sex;
break;
}
else
{
cout << "输入有误请重新输入" << endl;
}
}
cout << "电话:" << endl;
string phone;
cin >> phone;
a.presonarray[a.size].phone = phone;
cout << "地址" << endl;
string address;
cin >> address;
a.presonarray[a.size].address = address;
a.size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
//显示联系人
void address_book::show_contacts()
{
if (a.size == 0)
{
cout << "暂无联系人" << endl;
system("pause");
system("cls");
}
else
{
for (int i = 0; i < a.size; i++)
{
cout << "姓名:" << a.presonarray[i].name << "\t";
cout << "年龄:" << a.presonarray[i].age << "\t";
cout << "性别:" << (a.presonarray[i].sex == 1 ? "男" : "女") << "\t";
cout << "电话:" << a.presonarray[i].phone << "\t";
cout << "地址:" << a.presonarray[i].address << endl;
}
system("pause");
system("cls");
}
}
//查找联系人
void address_book::seek() {
string name;
cout << "请输入查找联系人的姓名:" << endl;
cin >> name;
if (compare(name) == -1) {
cout << "查无此人" << endl;
system("pause");
system("cls");
}
else {
cout << "姓名" << a.presonarray[compare(name)].name << "\t";
cout << "年龄:" << a.presonarray[compare(name)].age << "\t";
cout << "性别:" << (a.presonarray[compare(name)].sex == 1 ? "男" : "女") << "\t";
cout << "电话:" << a.presonarray[compare(name)].phone << "\t";
cout << "地址:" << a.presonarray[compare(name)].address << endl;
system("pause");
system("cls");
}
}
//修改联系人的信息
void address_book::revise()
{
cout << "请输入需要修改联系人的姓名:" << endl;
string name;
cin >> name;
int ret = compare(name);
if (ret == -1) {
cout << "查无此人" << endl;
}
else {
cout << "姓名" << a.presonarray[compare(name)].name << "\t";
cout << "年龄:" << a.presonarray[compare(name)].age << "\t";
cout << "性别:" << (a.presonarray[compare(name)].sex == 1 ? "男" : "女") << "\t";
cout << "电话:" << a.presonarray[compare(name)].phone << "\t";
cout << "地址:" << a.presonarray[compare(name)].address << endl;
cout << "请输入联系人的姓名:" << endl;
string name;
cin >> name;
a.presonarray[ret].name = name;
cout << "年龄:" << endl;
int age = 0;
cin >> age;
a.presonarray[ret].age = age;
while (true)
{
cout << "性别: 1、男 2、女" << endl;
int sex = 0;
cin >> sex;
if (sex == 1 || sex == 2) {
a.presonarray[ret].sex = sex;
break;
}
else {
cout << "输入有误请重新输入" << endl;
}
}
cout << "电话:" << endl;
string phone;
cin >> phone;
a.presonarray[ret].phone = phone;
cout << "地址" << endl;
string address;
cin >> address;
a.presonarray[ret].address = address;
cout << "修改成功" << endl;
system("pause");
system("cls");
}
}
//清除所有联系人
void address_book::all_clear() {
for (int i = 0; i < a.size; i++) {
a.presonarray[i] = a.presonarray[a.size];
}
a.size = 0;
cout << "清除成功" << endl;
system("pause");
system("cls");
}
//删除选中的联系人
void address_book::delete_person() {
cout << "请输入要删除的联系人姓名:" << endl;
string name;
cin >> name;
if (compare(name) == -1) {
cout << "查无此人" << endl;
}
else {
for (int i = compare(name); i < a.size; i++) {
a.presonarray[i] = a.presonarray[i + 1];
}
cout << "删除成功" << endl;
a.size--;
}
system("pause");
system("cls");
}
//判断字符串是否全部由数字组成
int tool::is_number(string x) {
int n = 0;
n = x.length();
int a = 0;
for (int i = 0; i < n; i++) {
if (x[i] >= '0' && x[i] <= '9') {
a++;
}
}
if (a == n) {
return 1;
}
else {
return 0;
}
}
//将字符串变为整形
double tool::change(string x) {
double n = x.length();
double num = 0;
for (int i = 0; i < n; i++) {
num += ((double)x[i] - 48) * pow(10, n - i - 1);
}
return num;
}
//main.cpp
#include"Book.h"
int main()
{
address_book b;
tool tool;
while (true)
{
b.showmenu();
string select;
cin >> select;
int select1 = (int)tool.change(select);
switch (select1)
{
case 1:
b.add();
break;
case 2:
b.seek();
break;
case 3:
b.show_contacts();
break;
case 4:
b.delete_person();
break;
case 5:
b.revise();
break;
case 6:
b.all_clear();
break;
case 0:
system("cls");
b.showmenu();
cout << "欢迎下次使用" << endl;
return 0;
default:
cout << "请输入0~6之间的整数" << endl;
system("pause");
system("cls");
break;
}
}
return 0;
}