#include <iostream>
#include <string>
using namespace std;
#define max 1000
struct person {
string names;
int sex;
int age;
string phone;
string address;
};
struct book {
struct person a[max];
int size;
};
void menu()
{
cout << "1.添加" << endl;
cout << "2.显示" << endl;
cout << "3.删除" << endl;
cout << "4.查找" << endl;
cout << "5.修改" << endl;
cout << "6.清空" << endl;
cout << "0.退出" << endl;
}
void addperson(struct book* abs) {
if (abs->size == max) {
cout << "通讯录已满,无法添加" << endl;
return;
}
else {
string name;
cout << "输入姓名" << endl;
cin >> name;
abs->a[abs->size].names = name;
int gender = 0;
cout << "输入性别(1代表男,2代表女)" << endl;
while (1) {
cin >> gender;
if (gender == 1 || gender == 2) {
abs->a[abs->size].sex = gender;
break;
}
cout << "输入有误,重新输入" << endl;
}
int age;
cout << "输入年龄" << endl;
cin >> age;
abs->a[abs->size].age = age;
string phone;
cout << "输入电话" << endl;
cin >> phone;
abs->a[abs->size].phone = phone;
string address;
cout << "输入地址" << endl;
cin >> address;
abs->a[abs->size].address = address;
abs->size++;
cout << "添加成功" << endl;
system("pause");
system("cls");
}
}
void showperson(struct book* abs) {
if (abs->size == 0) {
cout << "当前记录为空" << endl;
}
else {
for (int i = 0; i < abs->size; i++) {
cout << "姓名:" << abs->a[i].names << "\t";
cout << "年龄:" << abs->a[i].age << "\t";
cout << "地址:" << abs->a[i].address << "\t";
cout << "电话:" << abs->a[i].phone << "\t";
cout << "性别:" << (abs->a[i].sex==1?"男":"女" )<< endl;
}
}
system("pause");
system("cls");
}
int isexist(struct book* abs, string name) {
for (int i = 0; i < abs->size; i++) {
if (name == abs->a[i].names) {
return i;
}
else {
return -1;
}
}
}
void deleteperson(struct book* abs) {
string name;
cout << "请输入要删除的联系人姓名" << endl;
cin >> name;
int ret = isexist(abs, name);
if (ret == -1) {
cout << "查无此人" << endl;
}
else {
for (int i = ret; i < abs->size; i++) {
abs->a[i] = abs->a[i + 1];
}
abs->size--;
cout << "删除成功" << endl;
}
system("pause");
system("cls");
}
void findperson(struct book* abs) {
cout << "输入查找人姓名" << endl;
string name;
cin >> name;
int ret = isexist(abs, name);
if (ret == -1) {
cout << "查无此人" << endl;
}
else {
cout << "姓名:" << abs->a[ret].names << "\t";
cout << "年龄:" << abs->a[ret].age << "\t";
cout << "地址:" << abs->a[ret].address << "\t";
cout << "电话:" << abs->a[ret].phone << "\t";
cout << "性别:" << (abs->a[ret].sex == 1 ? "男" : "女") << endl;
}
system("pause");
system("cls");
}
void modifyperson(struct book* abs) {
cout << "输入要修改的姓名" << endl;
string name;
cin >> name;
int ret = isexist(abs, name);
if (ret == -1) {
cout << "查无此人" << endl;
}
else {
string name;
cout << "输入姓名" << endl;
cin >> name;
abs->a[ret].names = name;
int gender = 0;
cout << "输入性别(1代表男,2代表女)" << endl;
while (1) {
cin >> gender;
if (gender == 1 || gender == 2) {
abs->a[ret].sex = gender;
break;
}
cout << "输入有误,重新输入" << endl;
}
int age;
cout << "输入年龄" << endl;
cin >> age;
abs->a[ret].age = age;
string phone;
cout << "输入电话" << endl;
cin >> phone;
abs->a[ret].phone = phone;
string address;
cout << "输入地址" << endl;
cin >> address;
abs->a[ret].address = address;
}
system("pause");
system("cls");
}
void cleanperson(struct book* abs) {
int select = 0;
cout << "你是否确定要清空?" << endl;
cout << "确认清空请按1" << endl;
cin >> select;
if (select == 1) {
abs->size = 0;
cout << "已清空" << endl;
}
system("pause");
system("cls");
}
int main()
{
struct book abs;
abs.size = 0;
while (1) {
menu();
int select=0;
cin >> select;
switch (select) {
case 1:
addperson(&abs);
break;
case 2:
showperson(&abs);
break;
case 3:
deleteperson(&abs);
break;
case 4:
findperson(&abs);
break;
case 5:
modifyperson(&abs);
break;
case 6:
cleanperson(&abs);
break;
case 0:
cout << "welcome the next use" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
通讯录系统全部代码
最新推荐文章于 2023-10-01 10:43:21 发布