C++实现通讯录功能
系统需要实现以下功能:
1.添加联系人:像通讯录中添加新人,信息包括(姓名,性别,年龄,联系电话,家庭住址),最多纪录1000人
2.显示联系人:显示通讯录中所有联系人的信息
3.删除联系人:按照姓名进行删除指定联系人
4.查找联系人:按照姓名查找指定联系人信息
5.修改联系人:按照姓名修改指定联系人信息
6.清空联系人:清空通讯录中所有信息
7.推出通讯录:退出当前使用的通讯录
解决思路:
首先需要设计两个结构体:
1.联系人结构体
struct Person {//联系人结构体
//联系人拥有的属性
string Lname;
string sex;
int age;
string phone;
string stress;
};
2.通讯录结构体
struct LianXiRen {//通讯录结构体
int sum;//联系人总数
struct Person P[N];//联系人,上限为1000
};
注:完整代码在运行结果后面
运行结果截图:
(1)目录 (2)添加第一个联系人
(3)添加第二个联系人
(4)添加第三个联系人
(5)显示联系人
(6)删除联系人one
(7)查看联系人one是否删除成功
(8)查找two
(9)修改联系人信息
(10)结束修改并查看是否修改成功
(11)清空通讯录并且查看是否成功清空
代码:
#include<iostream>
#include<string>
#define N 1000
using namespace std;
struct Person {//联系人结构体
//联系人拥有的属性
string Lname;
string sex;
int age;
string phone;
string stress;
};
struct LianXiRen {//通讯录结构体
int sum;//联系人总数
struct Person P[N];//联系人,上限为1000
};
//struct LianXiRen L;
//添加联系人
void add_P(struct LianXiRen* l) {
cout << "请输入要添加联系人的信息" << endl;
cout << "姓名:"; cin >> l->P[l->sum].Lname;
cout << "性别:"; cin >> l->P[l->sum].sex;
cout << "年龄:"; cin >> l->P[l->sum].age;
cout << "联系电话:"; cin >> l->P[l->sum].phone;
cout << "家庭住址:"; cin >> l->P[l->sum].stress;
l->sum++;
cout << "添加成功" << endl;
system("pause"); system("cls");
}
//显示第i个联系人
void dis_i_P(struct LianXiRen* l, int i) {
cout << "-------------------------------------------" << endl;
cout << "\t\t联系人的信息\t\t\t" << endl;
cout << "-------------------------------------------" << endl;
cout << "姓名:" << l->P[i].Lname << "\t\t\t\t\t" << endl;
cout << "-------------------------------------------" << endl;
cout << "性别:" << l->P[i].sex << "\t\t\t\t\t" << endl;
cout << "-------------------------------------------" << endl;
cout << "年龄:" << l->P[i].age << "\t\t\t\t\t" << endl;
cout << "-------------------------------------------" << endl;
cout << "联系电话:" << l->P[i].phone << "\t\t\t\t\t" << endl;
cout << "-------------------------------------------" << endl;
cout << "家庭住址:" << l->P[i].stress << "\t\t\t\t\t" << endl;
cout << "-------------------------------------------" << endl;
}
//查找联系人
int find_P(struct LianXiRen* l, string name) {
int i = 0;
while (l->P[i].Lname != name && i < l->sum) {
i++;
}
if (i == l->sum) i = -1;
return i;
}
//删除联系人
void del_P(struct LianXiRen* l, int p) {
int i;
for (i = p; i < l->sum; i++) {
l->P[i] = l->P[i + 1];
}
//cout <<"找到的联系人下标为:"<< p;//验证要删除的联系人是否查找正确
}
void mod_menu() {
cout << "__________________________________________________________________" << endl;
cout << "||\t\t\t信息修改选项\t\t\t\t||" << endl;
cout << "||--------------------------------------------------------------||" << endl;
cout << "||1.修改联系人姓名 2.修改联系人年龄\t\t\t||" << endl;
cout << "||--------------------------------------------------------------||" << endl;
cout << "||3.修改联系人电话 4.修改联系人地址\t\t\t||" << endl;
cout << "||--------------------------------------------------------------||" << endl;
cout << "||0.结束修改 \t\t\t\t||" << endl;
cout << "||______________________________________________________________||" << endl;
cout << "请输入要修改什么信息" << endl;
}
//修改联系人
void mo_P(struct LianXiRen* l,int p) {//已经知道要修改联系人的下标
int i;
string Lname;
string sex;
int age;
string phone;
string stress;
int flag = 1;
while (flag) {
mod_menu();
cin >> flag;
switch (flag) {
case 1:
cin >> Lname;
l->P[p].Lname = Lname;
break;
case 2:
cin >> age;
l->P[p].age = age;
break;
case 3:
cin >> phone;
l->P[p].phone = phone;
break;
case 4:
cin >> stress;
l->P[p].stress = stress;
break;
case 0:
break;
}
system("pause"); system("cls");
}
}
//清空通讯录
int r_All() {
return 0;
}
int con_Ch() {
int choice;
cout << "请输入要进行的操作:" << endl;
cin >> choice;
return choice;
}
void menu() {
cout << "__________________________________________________________________" << endl;
cout << "||\t\t\t通讯录系统\t\t\t\t||" << endl;
cout << "||--------------------------------------------------------------||" << endl;
cout << "||1.添加联系人 2.显示联系人\t\t\t\t||" << endl;
cout << "||--------------------------------------------------------------||" << endl;
cout << "||3.删除联系人 4.查找联系人\t\t\t\t||" << endl;
cout << "||--------------------------------------------------------------||" << endl;
cout << "||5.修改联系人 6.清空联系人\t\t\t\t||" << endl;
cout << "||--------------------------------------------------------------||" << endl;
cout << "||0.推出通讯录 \t\t\t\t||" << endl;
cout << "||______________________________________________________________||" << endl;
}
int main() {
struct LianXiRen L;
L.sum = 0;
string name;
//L.sum = 0;//通讯录人数初始化为0
int flag = 1;
while (flag) {
menu();
int choice;
choice = con_Ch();
switch (choice) {
case 0:
flag = 0;
break;
case 1:
add_P(&L);//进行添加联系人操作后,通讯录中已有的联系人数增加
//cout << L.sum << " ";//验证此时通讯录中人数是否正确
break;
case 2:
for (int i = 0; i < L.sum; i++) {
dis_i_P(&L, i);
}
system("pause"); system("cls");
break;
case 3:
int i;
cout << "请输入要删除联系人的姓名:" << endl;
cin >> name;
i = find_P(&L, name);
if (i >= 0) {
del_P(&L, i);
cout << "删除成功" << endl;
L.sum--;
}
else cout << "删除失败,通讯录中无此人"<<endl;
system("pause"); system("cls");
break;
case 4:
int j;
cout << "请输入要查找联系人的姓名:" << endl;
cin >> name;
j = find_P(&L, name);
if (j >= 0) {
dis_i_P(&L, j);
}
else cout << "查找失败,通讯录中无此人"<<endl;
system("pause"); system("cls");
break;
case 5:
//先调用find_P,找到要修改的联系人下标
int k;
cout << "请输入要修改联系人的姓名:" << endl;
cin >> name;
k= find_P(&L, name);
if (k == -1) {
cout << "查找失败" << endl;
}
else {
mo_P(&L, k);
}
break;
case 6:
L.sum = r_All();
break;
}
}
}