无脑复制,直接提交作业,有问题留言!!!
通讯录的增删改查等操作
#include <iostream>
#include <string>
using namespace std;
#define MAX 1
//通讯录人员信息包括姓名、性别1--男 2--女、年龄、电话、住址
struct Person {
string name;
int sex;
int age;
string phone;
string addr;
};
//通讯录表包括人员信息和大小
struct List {
struct Person per[MAX];
int size;
};
//struct person* p;
//struct List* L;
//菜单列表
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 Add_person(struct List* L ) {
if (L -> size < MAX) {
cout << "请输入姓名:" << endl;
string name;
cin >> name;
L->per[L->size].name = name;
cout << "请输入性别1--男 2--女:" << endl;
int sex;
cin >> sex;
while (1) {
if (sex == 1 || sex == 2) {
L->per[L->size].sex = sex;
break;
}
else {
cout << "输入错误!请重新输入:" << endl;
cin >> sex;
}
}
cout << "请输入年龄:" << endl;
int age;
cin >> age;
L->per[L->size].age = age;
cout << "请输入电话:" << endl;
string phone;
cin >> phone;
L->per[L->size].phone = phone;
cout << "请输入住址:" << endl;
string addr;
cin >> addr;
L->per[L->size].addr = addr;
L->size++;
cout << "添加成功!" << endl;
}
else {
cout << "通讯录已满,无法添加!" << endl;
}
system("pause");
system("cls");
}
//显示联系人
void show_person(struct List L) {
if(L.size)
for (int i = 0; i < L.size; i++) {
cout << "姓名:" << L.per[i].name << '\t';
cout << "性别:" << (L.per[i].sex == 1 ? "男" : "女") << '\t';
cout << "年龄:" << L.per[i].age << '\t';
cout << "电话:" << L.per[i].phone << '\t';
cout << "住址:" << L.per[i].addr << '\t';
cout << endl;
}
else {
cout << "通讯录为空!" << endl;
}
system("pause");
system("cls");
}
//查找联系人
int seek_person(struct List L) {
string name;
cout << "请输入要查找的联系人姓名:" ;
cin >> name;
for (int i = 0; i < L.size; i++) {
if (L.per[i].name == name) {
return i;
}
}
return -1;
}
//修改联系人
void amend_person(struct List* L) {
string name;
int n = -1;
cout << "请输入要修改的联系人姓名:";
cin >> name;
for (int i = 0; i < L->size; i++) {
if (L->per[i].name == name) {
n = i;
break;
}
}
if (n == -1) {
cout << "查无此人,无法修改!" << endl;
}
else {
cout << "请输入修改的姓名:" << endl;
string name;
cin >> name;
L->per[n].name = name;
cout << "请输入修改的性别1--男 2--女:" << endl;
int sex;
cin >> sex;
while (1) {
if (sex == 1 || sex == 2) {
L->per[n].sex = sex;
break;
}
else {
cout << "输入错误!请重新输入:" << endl;
cin >> sex;
}
}
cout << "请输入修改的年龄:" << endl;
int age;
cin >> age;
L->per[n].age = age;
cout << "请输入修改的电话:" << endl;
string phone;
cin >> phone;
L->per[n].phone = phone;
cout << "请输入修改的住址:" << endl;
string addr;
cin >> addr;
L->per[n].addr = addr;
cout << "修改成功!" << endl;
}
system("pause");
system("cls");
}
//删除联系人
void delete_person(struct List* L) {
string name;
int n = -1;
cout << "请输入要删除的联系人姓名:";
cin >> name;
for (int i = 0; i < L->size; i++) {
if (L->per[i].name == name) {
n = i;
break;
}
}
if(n == -1)
cout << "查无此人,无法删除!" << endl;
else {
for (int i = n; i < L->size; i++) {
L->per[i] = L->per[i + 1];
}
cout << "删除成功!" << endl;
L->size--;
}
system("pause");
system("cls");
}
//清空通讯录
void clear_List(struct List* L) {
L->size = 0;
cout << "通讯录已清空!" << endl;
system("pause");
system("cls");
}
int main(int argc, char** argv) {
char select;
struct Person p;
struct List L;
L.size = 0;
while (true) {
showMenu();
cin >> select;
switch (select) {
case '0':
cout << "欢迎下次使用!" << endl;
return 0;
case '1':
Add_person(&L);
break;
case '2':
show_person(L);
break;
case '3': {
int flag = 0;
flag = seek_person(L);
if (flag != -1) {
cout << "找到了:" << "姓名:" << L.per[flag].name << '\t';
cout << "性别:" << (L.per[flag].sex == 1 ? "男" : "女") << '\t';
cout << "年龄:" << L.per[flag].age << '\t';
cout << "电话:" << L.per[flag].phone << '\t';
cout << "住址:" << L.per[flag].addr << '\t';
cout << endl;
system("pause");
system("cls");
}
else {
cout << "查无此人!" << endl;
system("pause");
system("cls");
}
break;
}
case '4':
amend_person(&L);
break;
case '5':
delete_person(&L);
break;
lable: case '6': {
cout << "您确定清空吗?Y/N" << endl;
char a;
cin >> a;
if(a == 'Y')
clear_List(&L);
else if (a == 'N') {
system("pause");
system("cls");
break;
}
else {
cout << "输入错误!请重新输入" << endl;
goto lable;
}
}
default:
system("pause");
system("cls");
}
}
return 0;
}