前言
? 要实现一个通讯录,通讯录可以用来存储人的信息,每个人的信息包括:姓名、性别、年龄、电话、住址,可能我们在实现时,首先想到的是将这些信息储存在一个结构体中,用一个结构体数组来存储每个人的信息。然而我们如果用静态的数组,会有很多不合适的地方。如果需要存放的人的信息很多,我们的数组小而不够了怎么办?如果存放的信息少我们数组太大了浪费空间怎么办?这里就需要用到动态内存开辟的方法讲数组进行灵活的扩容,本文代码分为Contact.h存放头文件宏定义结构体数组以及函数头,Contact.c存放函数详细内容,test.c构建通讯录以及对通讯录测试。通讯录中函数功能有:添加,删除,查找,修改联系人信息,显示清空联系人信息,以名字排序所有联系人,以下是代码。
Contact.h
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Name_Max 20
#define Sex_Max 6
#define Tele_Max 12
#define Address_Max 30
#pragma once
struct PeoInfo
{
char name[Name_Max];
char sex[Sex_Max];
int age;
char tele[Tele_Max];
char address[Address_Max];
};
typedef struct Contact
{
struct PeoInfo* data;
int capacity;
int size;
}Contact;
void InitContact(Contact*ps);
void AddPeoInfo(Contact* ps);
void DelPeoInfo(Contact* ps);
void SearchPeoInfo(Contact* ps);
void ModifyPeoInfo(Contact* ps);
void ShowPeoInfo(Contact* ps);
void DestoryContact(Contact* ps);
void SortPeoInfo(Contact* ps);
Contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
void InitContact(Contact* ps)
{
ps->data = (struct PeoInfo*)malloc(5 * sizeof(struct PeoInfo));
ps->size = 0;
ps->capacity = 5;
}
void AddPeoInfo(Contact* ps)
{
if (ps->size == ps->capacity)
{
struct PeoInfo* tmp = (struct PeoInfo*)realloc(ps->data, (ps->capacity + 5) * sizeof(struct PeoInfo));
if (tmp != NULL)
{
ps->data = tmp;
ps->capacity += 5;
printf("增容成功\n");
}
else
{
printf("malloc fail");
return;
}
}
printf("请输入姓名\n");
scanf("%s", ps->data[ps->size].name);
printf("请输入性别\n");
scanf("%s", ps->data[ps->size].sex);
printf("请输入年龄\n");
scanf("%d", &(ps->data[ps->size].age));
printf("请输入电话\n");
scanf("%s", ps->data[ps->size].tele);
printf("请输入住址\n");
scanf("%s", ps->data[ps->size].address);
printf("添加成功");
ps->size++;
}
int FindPeoInfo(Contact* ps,const char*name)
{
for (int i = 0; i < ps->size; i++)
{
if (strcmp(ps->data[i].name, name) == 0)
{
return i;
}
else
{
return -1;
}
}
}
void DelPeoInfo(Contact* ps)
{
if (ps->size == 0)
{
printf("已经没有联系人了");
return;
}
char name[Name_Max] = { 0 };
printf("请输入需要删除的人的名字");
scanf("%s", name);
int res=FindPeoInfo(ps, name);
if (res == -1)
{
printf("没找到");
return;
}
else
{
for (int i = res; i < ps->size - 1; i++)
{
ps->data[i] = ps->data[i + 1];
}
ps->size--;
printf("删除成功!\n");
}
}
void SearchPeoInfo(Contact* ps)
{
char name[Name_Max] = { 0 };
printf("输入要查找人的名字\n");
scanf("%s", name);
int res = FindPeoInfo(ps, name);
if (res == -1)
{
printf("搜索不到联系人\n");
return;
}
else
{
printf("%15s %15s %3d %15s %15s", ps->data[res].name, ps->data[res].sex, ps->data[res].age, ps->data[res].tele, ps->data[res].address);
}
}
void ModifyPeoInfo(Contact* ps)
{
char name[Name_Max] = { 0 };
printf("输入要修改人的名字\n");
scanf("%s", name);
int res = FindPeoInfo(ps, name);
if (res == -1)
{
printf("搜索不到联系人\n");
return;
}
else
{
printf("请输入姓名\n");
scanf("%s", ps->data[res].name);
printf("请输入性别\n");
scanf("%s", ps->data[res].sex);
printf("请输入年龄\n");
scanf("%d", &(ps->data[res].age));
printf("请输入电话\n");
scanf("%s", ps->data[res].tele);
printf("请输入住址\n");
scanf("%s", ps->data[res].address);
printf("修改成功");
}
}
void ShowPeoInfo(Contact* ps)
{
if (ps->size == 0)
{
printf("没有人啊还\n");
return;
}
for (int i = 0; i < ps->size; i++)
{
printf("%15s %15s %3d %15s %15s\n",
ps->data[i].name, ps->data[i].sex, ps->data[i].age, ps->data[i].tele, ps->data[i].address);
}
}
void DestoryContact(Contact* ps)
{
free(ps->data);
ps->data = NULL;
ps->capacity = 0;
ps->size = 0;
}
int cmp_name(const void* elem1,const void *elem2)
{
return strcmp(((struct PeoInfo*)elem1)->name, ((struct PeoInfo*)elem2)->name);
}
void SortPeoInfo(Contact* ps)
{
qsort(ps->data, ps->size, sizeof(ps->data[0]), cmp_name);
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
void menu()
{
printf("************************************\n");
printf("************************************\n");
printf("**** 0.Exit 1.Add ******\n");
printf("**** 2.Del 3.Search ***\n");
printf("**** 4.Modify 5.Show *****\n");
printf("**** 6.Destory 7.Sort *****\n");
printf("************************************\n");
printf("************************************\n");
}
enum Myoption
{
Exit,
Add,
Del,
Search,
Modify,
Show,
Destory,
Sort
};
int main()
{
int input = 0;
Contact con;
InitContact(&con);
do
{
menu();
printf("请选择");
scanf("%d", &input);
switch (input)
{
case Exit:
break;
case Add:
printf("添加联系人\n");
AddPeoInfo(&con);
break;
case Del:
printf("删除指定联系人\n");
DelPeoInfo(&con);
break;
case Search:
printf("寻找指定联系人\n");
SearchPeoInfo(&con);
break;
case Modify:
printf("修改指定联系人\n");
ModifyPeoInfo(&con);
break;
case Show:
printf("展示所有联系人\n");
ShowPeoInfo(&con);
break;
case Destory:
printf("清空通讯录\n");
DestoryContact(&con);
break;
case Sort:
printf("对联系人按名字排序\n");
SortPeoInfo(&con);
break;
default:
break;
}
} while (input);
return 0;
}
|