#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdlib>
using namespace std;
//全局变量
#define MAX_NAME 20
#define MAX_GENDER 10
#define MAX_ADDR 30
#define MAX_TEL 12
#define MAX_NUM 1000
#define SM 3
#define INC 2
typedef struct peoinfo {//定义联系人
char name[MAX_NAME];
int age;
char gender[MAX_GENDER];
char tel[MAX_TEL];
char addr[MAX_ADDR];
}peoinfo;
typedef struct contact {//定义通讯录
peoinfo* data;
int capacity;
int sz;
}contact;
void menu()//功能菜单
{
cout << "********************************" << endl;
cout << "********1.add 2.del********" << endl;
cout << "********3.search 4.modify*****" << endl;
cout << "********5.sort 6.print******" << endl;
cout << "********0.exit *************" << endl;
}
int search(contact* pc, char name[])//findbyname
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
enum option//枚举
{
EXIT, ADD, DEL, SEARCH, MODIFY, SORT, PRINT
};
void Initcontact(contact* pc)//初始化通讯录
{
pc->data = (peoinfo*)malloc(SM* sizeof(peoinfo));
if (pc->data == NULL)
{
perror("Initcontact");
return;
}
pc->sz = 0;
pc->capacity = SM;
}
void destorycontact(contact* pc)//释放调用空间
{
free(pc->data);
pc->data = NULL;
pc->sz = 0;
pc->capacity = 0;
}
void addcontact(contact* pc)//动态版增容
{
if (pc->sz == pc->capacity)
{
peoinfo*ptr=(peoinfo*)realloc(pc->data, (INC + pc->capacity) * sizeof(peoinfo));
if (ptr != NULL)
{
pc->data = ptr;
cout << "增容成功!" << endl;
pc->capacity += INC;
}
else
{
perror("addcontact");
return;
}
}
else
{
cout << "请输入名字:" << endl;
cin >> pc->data[pc->sz].name;
cout << "请输入年龄:" << endl;
cin >> pc->data[pc->sz].age;
cout << "请输入性别:" << endl;
cin >> pc->data[pc->sz].gender;
cout << "请输入电话:" << endl;
cin >> pc->data[pc->sz].tel;
cout << "请输入地址:" << endl;
cin >> pc->data[pc->sz].addr;
cout << "添加成功" << endl;
pc->sz++;
}
}
void print(const contact* pc)//PEINT
{
cout << left << setw(12) << setfill(' ') << "名字" << left << setw(12) << setfill(' ') << "年龄" << left << setw(12) << setfill(' ') << "性别" << left << setw(12) << setfill(' ') << "电话" << left << setw(12) << setfill(' ') << "地址" << endl;
for (int i = 0; i < pc->sz; i++)
{
cout << left << setw(12) << setfill(' ') << pc->data[i].name;
cout << left << setw(12) << setfill(' ') << pc->data[i].age;
cout << left << setw(12) << setfill(' ') << pc->data[i].gender;
cout << left << setw(12) << setfill(' ') << pc->data[i].tel;
cout << left << setw(12) << setfill(' ') << pc->data[i].addr << endl;
}
cout << "打印成功" << endl;
}
void delcontact(contact* pc)//DEL
{
char name[MAX_NAME];
if (pc->sz == 0)
{
cout << "通讯录为空,无需删除" << endl;
return;
}
cout << "请输入你要删除的联系人名字:";
cin >> name;
int pos = search(pc, name);
if (pos == -1)
{
cout << "要删除的联系人不存在" << endl;
return;
}
for (int i = pos; i < pc->sz - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->sz--;
cout << "删除成功" << endl;
}
void searchcontact(contact* pc)//SEARCH
{
char name[MAX_NAME];
cout << "请输入你要查找的联系人名字:";
cin >> name;
int pos = search(pc, name);
if (pos == -1)
{
cout << "要查找的联系人不存在" << endl;
return;
}
else
{
cout << left << setw(12) << setfill(' ') << "名字" << left << setw(12) << setfill(' ') << "年龄" << left << setw(12) << setfill(' ') << "性别" << left << setw(12) << setfill(' ') << "电话" << left << setw(12) << setfill(' ') << "地址" << endl;
cout << left << setw(12) << setfill(' ') << pc->data[pos].name;
cout << left << setw(12) << setfill(' ') << pc->data[pos].age;
cout << left << setw(12) << setfill(' ') << pc->data[pos].gender;
cout << left << setw(12) << setfill(' ') << pc->data[pos].tel;
cout << left << setw(12) << setfill(' ') << pc->data[pos].addr << endl;
cout << "查找成功" << endl;
}
}
void modifycontact(contact* pc)//MODOFY
{
char name[MAX_NAME];
cout << "请输入你要修改的联系人名字:";
cin >> name;
int pos = search(pc, name);
if (pos == -1)
{
cout << "要修改的联系人不存在" << endl;
return;
}
else
{
cout << "请输入名字:" << endl;
cin >> pc->data[pos].name;
cout << "请输入年龄:" << endl;
cin >> pc->data[pos].age;
cout << "请输入性别:" << endl;
cin >> pc->data[pos].gender;
cout << "请输入电话:" << endl;
cin >> pc->data[pos].tel;
cout << "请输入地址:" << endl;
cin >> pc->data[pos].addr;
cout << "修改成功" << endl;
}
}
void sortcontact(contact* pc) //SORT
{
for (int i = 0; i < pc->sz - 1; i++) {
for (int j = 0; j < pc->sz - i - 1; j++) {
if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0) {
peoinfo temp = pc->data[j];
pc->data[j] = pc->data[j + 1];
pc->data[j + 1] = temp;
}
}
}
cout << "排序完成" << endl;
}
int main()//主函数
{
int input = 0;
contact con;
Initcontact(&con);
do {
menu();
cout << "请选择:->" << endl;
cin >> input;
switch (input)
{
case ADD://增加联系人
addcontact(&con);
break;
case DEL://删除联系人
delcontact(&con);
break;
case SEARCH://查询联系人
searchcontact(&con);
break;
case MODIFY://修改联系人
modifycontact(&con);
break;
case SORT://排序联系人
sortcontact(&con);
break;
case PRINT://打印联系人
print(&con);
break;
case EXIT:
cout << "退出通讯录" << endl;
destorycontact(&con);
break;
default:
cout << "选择错误,请重新选择!" << endl;
break;
}
} while (input);
return 0;
}