#include#define MAXN 20
#define MAXS 5
#define MAXA 15
static count = 1;
static int peocnt = 1;
typedef struct
{
char name[MAXN];
char sex[MAXS];
char addr[MAXA];
char num[11];
int age;
}stu;
enum optin//枚举类型(用在main函数的switch分支语句中,免得写出挫挫的case 1之类的代码)
{
EXIT,//0
ADD,//1
DEL,//2
SEARCH,//3
MODIFY,//4
SHOW,//5
EMPTY,//6
};
void menu()
{
printf("*************************************************************\n");
printf("****** 1增加通讯录人数 2删除通讯录人数 *******\n");
printf("****** 3查找通讯录 4修改通讯录 *******\n");
printf("****** 5显示通讯录 6清空通讯录 *******\n");
printf("****** 0退出通讯录 *******\n");
printf("*************************************************************\n");
}
void Add(stu* p)
{
if (count > peocnt)
{
stu *p1 = realloc(p,count*sizeof(stu));
if (p1 != NULL)
p = p1;
printf("增容成功!\n");
}
printf("请输入姓名:\n");
scanf("%s", p[count-1].name);
printf("请输入性别:\n");
scanf("%s", p[count - 1].sex);
printf("请输入地址:\n");
scanf("%s", p[count - 1].addr);
printf("请输入电话:\n");
scanf("%s", p[count - 1].num);
printf("请输入年龄:\n");
scanf("%d", &p[count - 1].age);
count++;
}
void Del(stu* p)
{
int i = 0;
char tmp[MAXN] = { 0 };
printf("请输入你要删除的名字:\n");
scanf("%s", tmp);
for (i = 0; i < count-1; i++)
{
if (strcmp(tmp, p[i].name) == 0)
{
strcpy(p[i].name, p[count - 2].name);
strcpy(p[i].sex, p[count - 2].sex);
strcpy(p[i].addr, p[count - 2].addr);
strcpy(p[i].num, p[count - 2].num);
p[i].age = p[count - 2].age;
count--;
return;
}
}
if (i == count-1)
printf("查无此人\n");
}
void Search(stu* p)
{
char tmp[MAXN] = { 0 };
int i = 0;
printf("请输入你要查找的人:\n");
scanf("%s", tmp);
for (i = 0; i < count-1; i++)
{
if (strcmp(tmp, p[i].name) == 0)
{
printf("姓名 年龄 性别 地址 电话\n");
printf("%5s %5d %5s %5s %5s", p[i].name, p[i].age, p[i].sex, p[i].addr, p[i].num);
printf("\n");
break;
}
}
if (i == count-1)
printf("查无此人\n");
}
void Modify(stu* p)
{
int i = 0;
char tmp[MAXN] = { 0 };
printf("请输入你要修改的人名;\n");
scanf("%s", tmp);
for (i = 0; i < count-1; i++)
{
if (strcmp(tmp, p[i].name) == 0)
{
printf("请输入姓名:\n");
scanf("%s", p[i].name);
printf("请输入性别:\n");
scanf("%s", p[i].sex);
printf("请输入地址:\n");
scanf("%s", p[i].addr);
printf("请输入电话:\n");
scanf("%s", p[i].num);
printf("请输入年龄:\n");
scanf("%d", &p[i].age);
}
}
if (i == count-1)
printf("查无此人\n");
}
void Show(stu* p)
{
int i = 0;
printf("姓名 年龄 性别 地址 电话\n");
for (i = 0; i < count-1; i++)
{
printf("%5s %5d %5s %5s %5s", p[i].name, p[i].age, p[i].sex, p[i].addr, p[i].num);
printf("\n");
}
}
void Empty(stu* p)
{
count = 0;
}
int main()
{
int input = 1;
stu *p = NULL;
stu *p1 = malloc(peocnt*sizeof(stu));
if (p1 != NULL)
p = p1;
else
perror("malloc:\n");
memset(p, 0, peocnt*sizeof(stu));
while (input)
{
menu();
printf("请输入选项>");
scanf("%d", &input);
switch (input)
{
case ADD:
Add(p);
break;
case DEL:
Del(p);
break;
case SEARCH:
Search(p);
break;
case MODIFY:
Modify(p);
break;
case SHOW:
Show(p);
break;
case EMPTY:
Empty(p);
break;
case EXIT:
break;
default:
printf("选择有误!\n");
break;
}
}
system("pause");
return 0;
}