xm.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#define MAX 1000
using namespace std;
struct people
{
string name;
string num;
int sex;
string addr;
};
struct addressbooks
{
struct people person[MAX];
int sz;
};
void add(addressbooks *abs);
void show(addressbooks* abs);
void del(addressbooks* abs);
int ex(addressbooks* abs,string name);
void fin(addressbooks* abs);
void rev(addressbooks* abs);
void emp(addressbooks* abs);
xm.cpp
#include"xm.h"
void add(addressbooks* abs)
{
if (abs->sz >= MAX)//判断通讯录是否满
{
cout << "通讯录已满,无法添加!" << endl;
return ;
}
else
{
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->person[abs->sz].name = name;
while (true)
{
cout << "请输入性别:" << endl;
cout << "1-----男" << endl;
cout << "2-----女" << endl;
int sex;
cin >> sex;
int 男=1;
int 女=0;
if (sex == 1)
{
abs->person[abs->sz].sex = 男;
break;
}
else if (sex == 2)
{
abs->person[abs->sz].sex = 女;
break;
}
else
{
cout << "重新输入:"<<endl;
}
}
while (true)
{
cout << "请输入电话号码:" << endl;
string num;
cin >> num;
if (size(num) > 11 || size(num) < 11)
{
cout << "请重新输入:"<<endl;
}
else
{
abs->person[abs->sz].num = num;
break;
}
}
cout << "请输入家庭地址:"<<endl;
string addr;
cin >> addr;
abs->person[abs->sz].addr = addr;
abs->sz++;
cout << "记录完毕!"<<endl;
}
system("pause");
system("cls");
}
void show(addressbooks* abs)
{
if (abs->sz == 0)
{
cout << "暂无联系人" << endl;
}
else
{
for (int i = 0; i < abs->sz; i++)
{
cout << "名字:" << abs->person[i].name<<endl;
cout << "性别:" <<( abs->person[i].sex==1?"男":"女") << endl;
cout << "号码:" << abs->person[i].num << endl;
cout << "地址:" << abs->person[i].addr << endl<<endl;
}
}
system("pause");
system("cls");
}
int ex(addressbooks* abs,string name)
{
for (int i = 0; i < abs->sz; i++)
{
if (abs->person[i].name== name)
{
return i;
}
else
{
return -1;
}
}
}
void del(addressbooks* abs)
{
while (true)
{
cout << "请输入删除联系人姓名:" << endl;
string name;
cin >> name;
if (int ret = ex(abs, name) == -1)
{
cout << "您所要删除的人不存在,请重新输入:";
}
else
{
for (int i = ret; i < abs->sz; i++)
{
abs->person[i] = abs->person[i + 1];
}
abs->sz--;
cout << "删除成功" << endl;
break;
}
system("pause");
system("cls");
}
}
void fin(addressbooks* abs)
{
while (true)
{
cout << "请输入您要查找的人";
string name;
cin >> name;
int ret = ex(abs, name);
if ( ret!= -1)
{
cout << "名字:" << abs->person[ret].name<<endl;
cout << "性别:" << (abs->person[ret].sex == 1 ? "男" : "女") << endl;
cout << "号码:" << abs->person[ret].num<<endl;
cout << "地址:" << abs->person[ret].addr<<endl<<endl;
break;
}
else
{
cout << "没有找到,请再试一次:" << endl;
}
}
system("pause");
system("cls");
}
void rev(addressbooks* abs)
{
while (true)
{
cout << "请输入要修改名字:" ;
string name;
cin >> name;
int i = ex(abs, name);
if (i != -1)
{
string name;
cout << "请输入新的姓名:" << endl;
cin >> name;
abs->person[i].name = name;
while (true)
{
cout << "请输入性别:" << endl;
cout << "1-----男" << endl;
cout << "2-----女" << endl;
int sex;
cin >> sex;
int 男 = 1;
int 女 = 0;
if (sex == 1)
{
abs->person[i].sex = 男;
break;
}
else if (sex == 2)
{
abs->person[i].sex = 女;
break;
}
else
{
cout << "重新输入:" << endl;
}
}
while (true)
{
cout << "请输入电话号码:" << endl;
string num;
cin >> num;
if (size(num) > 11 || size(num) < 11)
{
cout << "请重新输入:" << endl;
}
else
{
abs->person[i].num = num;
break;
}
}
cout << "请输入家庭地址:" << endl;
string addr;
cin >> addr;
abs->person[i].addr = addr;
cout << "修改完毕!" << endl;
break;
}
else
{
cout << "没有这个人,请重新输入:" << endl;
}
}
system("pause");
system("cls");
}
void emp(addressbooks* abs)
{
while (true)
{
cout << "确定清空联系人!?" << endl;
cout << "1----确定" << endl;
cout << "2----算了" << endl;
int a = 0;
cin >> a;
if (a == 1)
{
abs->sz = 0;
cout << "通讯录已清空"<<endl;
break;
}
else if (a == 2)
{
break;
}
else
{
cout << "请重新输入:" << endl;
}
}
system("pause");
system("cls");
}
test.cpp
#include<iostream>
using namespace std;
#include"xm.h"
void menu()
{
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;
}
int main()
{
addressbooks abs;
abs.sz =0;
while (true)
{
menu();
int a = 0;
cin >> a;
switch (a)
{
case(1):
add(&abs);
break;
case(2):
show(&abs);
break;
case(3):
del(&abs);
break;
case(4):
fin(&abs);
break;
case(5):
rev(&abs);
break;
case(6):
emp(&abs);
break;
case(0):
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
default:
cout << "输入错误,请重新输入:" << endl;
break;
}
}
return 0;
}