动态通讯录和静态的差别就是contact结构体中不用数组来存放人的信息,而是改用一个指针指向存放数据的空间,然后在初始化时用这个指针指向malloc开辟的空间。同时contact结构体中还多了一个参数capacity来表示容量大小,如果成员个数sz和capacity相等,那么就可以用realloc扩容。
#include
#include
#include
#include
#define NAME 10
#define SEX 5
#define TELE 12
#define ADDR 30
#define CAP 3
#define EXPAN 2
typedef struct PeoInfo
{
char name[NAME];
char sex[SEX];
int age;
char tele[TELE];
char addr[ADDR];
}PeoInfo;
typedef struct Contact
{
PeoInfo *data;
int sz;
int capacity;
}Contact, * Pcontact;
void InitContact(Contact* pc);
void DestroyContact(Contact* pc);
void Print(Contact* pc);
void AddContact(Pcontact pc);
void DelContact(Pcontact pc);
void SearchContact(Contact* pc);
void ModifyContact(Contact* pc);
void SortContact(Contact* pc);
void AllDelContact(Contact* pc);
#include"contact.h"
void menu()
{
printf("******************************\n");
printf("***** 1.add 2.del ******\n");
printf("**** 3.search 4.modify ****\n");
printf("***** 5.print 6.sort *****\n");
printf("***** 7.AllDel 0.exit *****\n");
printf("******************************\n");
}
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
PRINT,
SORT,
ALLDEL
};
int main()
{
int input = 0;
menu();
Contact con = { 0 };
InitContact(&con);
do
{
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case DEL:
DelContact(&con);
break;
case SEARCH:
SearchContact(&con);
break;
case MODIFY:
ModifyContact(&con);
break;
case PRINT:
Print(&con);
break;
case SORT:
SortContact(&con);
break;
case ALLDEL:
AllDelContact(&con);
break;
case EXIT:
DestroyContact(&con);
printf("退出通讯录\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
void InitContact(Contact* pc)
{
PeoInfo*ptr = (PeoInfo*)malloc(CAP * sizeof(PeoInfo));
if (ptr == NULL)
{
perror("malloc:");
return;
}
else
{
pc->data = ptr;
pc->sz = 0;
pc->capacity = CAP;
}
}
void DestroyContact(Contact* pc)
{
free(pc->data);
pc->data = NULL;
pc->capacity = 0;
pc->sz = 0;
}
static int CheckCapacity(Contact* pc)
{
if (pc->sz == pc->capacity)
{
PeoInfo* ptr = (PeoInfo*)realloc(pc->data, (pc->capacity + EXPAN)*sizeof(PeoInfo));
if (ptr == NULL)
{
printf("扩容失败:%s\n", strerror(errno));
return 0;
}
else
{
pc->capacity += EXPAN;
pc->data = ptr;
printf("扩容成功,当前容量为%d\n",pc->capacity);
}
}
return 1;
}
void AddContact(Pcontact pc)
{
int ret = CheckCapacity(pc);
if (ret == 0)
{
printf("空间不足,扩容失败\n");
return;
}
else
{
printf("请输入要添加人的姓名\n");
scanf("%s", pc->data[pc->sz].name);
printf("请输入要添加人的性别\n");
scanf("%s", pc->data[pc->sz].sex);
printf("请输入要添加人的年龄\n");
scanf("%d", &pc->data[pc->sz].age);
printf("请输入要添加人的电话\n");
scanf("%s", pc->data[pc->sz].tele);
printf("请输入要添加人的地址\n");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
printf("添加成功\n");
}
}
void Print(Contact* pc)
{
printf("%-10s %-8s %-5s %-12s %-30s\n",
"姓名", "性别", "年龄", "电话", "地址");
for (int i = 0; i < pc->sz; i++)
{
printf("%-10s %-8s %-5d %-12s %-30s\n",
pc->data[i].name,
pc->data[i].sex,
pc->data[i].age,
pc->data[i].tele,
pc->data[i].addr);
}
printf("打印成功\n");
}
void DelContact(Pcontact pc)
{
char name[NAME] = { 0 };
if (pc->sz == 0)
{
printf("通讯录为空,无法删除\n");
return;
}
printf("请输入要删除人的名字\n");
scanf("%s", name);
int pos = -1;
for (int i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
pos = i;
}
}
if (pos == -1)
{
printf("要删除的人不存在\n");
return;
}
int i = 0;
for (i = pos; i < pc->sz - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
//i+1的位置不用管,因为sz访问不到
pc->sz--;
printf("删除成功\n");
}
static int Find_by_name(char name[], Contact* pc)
{
for (int i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
void SearchContact(Contact* pc)
{
char name[NAME] = { 0 };
if (pc->sz == 0)
{
printf("通讯录为空\n");
return;
}
printf("请输入要查找人的名字\n");
scanf("%s", name);
int ret = Find_by_name(name, pc);
if (ret == -1)
{
printf("查无此人\n");
return;
}
printf("找到了\n");
printf("%-10s %-8s %-5s %-12s %-30s\n",
"姓名", "性别", "年龄", "电话", "地址");
printf("%-10s %-8s %-5d %-12s %-30s\n",
pc->data[ret].name,
pc->data[ret].sex,
pc->data[ret].age,
pc->data[ret].tele,
pc->data[ret].addr);
}
void ModifyContact(Contact* pc)
{
char name[NAME] = { 0 };
if (pc->sz == 0)
{
printf("通讯录为空\n");
return;
}
printf("请输入要修改人的名字\n");
scanf("%s", name);
int ret = Find_by_name(name, pc);
if (ret == -1)
{
printf("要修改的人的信息不存在\n");
return;
}
printf("请输入修改后人的姓名\n");
scanf("%s", pc->data[ret].name);
printf("请输入修改后人的性别\n");
scanf("%s", pc->data[ret].sex);
printf("请输入修改后人的年龄\n");
scanf("%d", &pc->data[ret].age);
printf("请输入修改后人的电话\n");
scanf("%s", pc->data[ret].tele);
printf("请输入修改后人的地址\n");
scanf("%s", pc->data[ret].addr);
printf("修改成功\n");
}
int cmp_by_name(const void* p1, const void* p2)
{
return strcmp(((PeoInfo*)p1)->name, ((PeoInfo*)p2)->name);
}
void SortContact(Contact* pc)
{
qsort(pc->data, pc->sz, sizeof(PeoInfo), cmp_by_name);
printf("排序成功\n");
}
void AllDelContact(Contact* pc)
{
pc->sz = 0;
memset(pc->data, 0, sizeof(pc->data));
printf("已删除全部联系人\n");
}
#include
#include
#include
#include
#define NAME 10
#define SEX 5
#define TELE 12
#define ADDR 30
#define CAP 3
#define EXPAN 2
typedef struct PeoInfo
{
char name[NAME];
char sex[SEX];
int age;
char tele[TELE];
char addr[ADDR];
}PeoInfo;
typedef struct Contact
{
PeoInfo *data;
int sz;
int capacity;
}Contact, * Pcontact;
void InitContact(Contact* pc);
void DestroyContact(Contact* pc);
void Print(Contact* pc);
void AddContact(Pcontact pc);
void DelContact(Pcontact pc);
void SearchContact(Contact* pc);
void ModifyContact(Contact* pc);
void SortContact(Contact* pc);
void AllDelContact(Contact* pc);
static int CheckCapacity(Contact* pc)
{
if (pc->sz == pc->capacity)
{
PeoInfo* ptr = (PeoInfo*)realloc(pc->data, (pc->capacity + EXPAN)*sizeof(PeoInfo));
if (ptr == NULL)
{
printf("扩容失败:%s\n", strerror(errno));
return 0;
}
else
{
pc->capacity += EXPAN;
pc->data = ptr;
printf("扩容成功,当前容量为%d\n",pc->capacity);
}
}
return 1;
}
void InitContact(Contact* pc)
{
PeoInfo*ptr = (PeoInfo*)malloc(CAP * sizeof(PeoInfo));
if (ptr == NULL)
{
perror("malloc:");
return;
}
else
{
pc->data = ptr;
pc->sz = 0;
pc->capacity = CAP;
}
}
void DestroyContact(Contact* pc)
{
free(pc->data);
pc->data = NULL;
pc->capacity = 0;
pc->sz = 0;
}
void Print(Contact* pc)
{
printf("%-10s %-8s %-5s %-12s %-30s\n",
"姓名", "性别", "年龄", "电话", "地址");
for (int i = 0; i < pc->sz; i++)
{
printf("%-10s %-8s %-5d %-12s %-30s\n",
pc->data[i].name,
pc->data[i].sex,
pc->data[i].age,
pc->data[i].tele,
pc->data[i].addr);
}
printf("打印成功\n");
}
void AddContact(Pcontact pc)
{
int ret = CheckCapacity(pc);
if (ret == 0)
{
printf("空间不足,扩容失败\n");
return;
}
else
{
printf("请输入要添加人的姓名\n");
scanf("%s", pc->data[pc->sz].name);
printf("请输入要添加人的性别\n");
scanf("%s", pc->data[pc->sz].sex);
printf("请输入要添加人的年龄\n");
scanf("%d", &pc->data[pc->sz].age);
printf("请输入要添加人的电话\n");
scanf("%s", pc->data[pc->sz].tele);
printf("请输入要添加人的地址\n");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
printf("添加成功\n");
}
}
static int Find_by_name(char name[], Contact* pc)
{
for (int i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
void DelContact(Pcontact pc)
{
char name[NAME] = { 0 };
if (pc->sz == 0)
{
printf("通讯录为空,无法删除\n");
return;
}
printf("请输入要删除人的名字\n");
scanf("%s", name);
int pos = -1;
for (int i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
pos = i;
}
}
if (pos == -1)
{
printf("要删除的人不存在\n");
return;
}
int i = 0;
for (i = pos; i < pc->sz - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
//i+1的位置不用管,因为sz访问不到
pc->sz--;
printf("删除成功\n");
}
void SearchContact(Contact* pc)
{
char name[NAME] = { 0 };
if (pc->sz == 0)
{
printf("通讯录为空\n");
return;
}
printf("请输入要查找人的名字\n");
scanf("%s", name);
int ret = Find_by_name(name, pc);
if (ret == -1)
{
printf("查无此人\n");
return;
}
printf("找到了\n");
printf("%-10s %-8s %-5s %-12s %-30s\n",
"姓名", "性别", "年龄", "电话", "地址");
printf("%-10s %-8s %-5d %-12s %-30s\n",
pc->data[ret].name,
pc->data[ret].sex,
pc->data[ret].age,
pc->data[ret].tele,
pc->data[ret].addr);
}
void ModifyContact(Contact* pc)
{
char name[NAME] = { 0 };
if (pc->sz == 0)
{
printf("通讯录为空\n");
return;
}
printf("请输入要修改人的名字\n");
scanf("%s", name);
int ret = Find_by_name(name, pc);
if (ret == -1)
{
printf("要修改的人的信息不存在\n");
return;
}
printf("请输入修改后人的姓名\n");
scanf("%s", pc->data[ret].name);
printf("请输入修改后人的性别\n");
scanf("%s", pc->data[ret].sex);
printf("请输入修改后人的年龄\n");
scanf("%d", &pc->data[ret].age);
printf("请输入修改后人的电话\n");
scanf("%s", pc->data[ret].tele);
printf("请输入修改后人的地址\n");
scanf("%s", pc->data[ret].addr);
printf("修改成功\n");
}
int cmp_by_name(const void* p1, const void* p2)
{
return strcmp(((PeoInfo*)p1)->name, ((PeoInfo*)p2)->name);
}
void SortContact(Contact* pc)
{
qsort(pc->data, pc->sz, sizeof(PeoInfo), cmp_by_name);
printf("排序成功\n");
}
void AllDelContact(Contact* pc)
{
pc->sz = 0;
memset(pc->data, 0, sizeof(pc->data));
printf("已删除全部联系人\n");
}
void menu()
{
printf("******************************\n");
printf("***** 1.add 2.del ******\n");
printf("**** 3.search 4.modify ****\n");
printf("***** 5.print 6.sort *****\n");
printf("***** 7.AllDel 0.exit *****\n");
printf("******************************\n");
}
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
PRINT,
SORT,
ALLDEL
};
int main()
{
int input = 0;
menu();
Contact con = { 0 };
InitContact(&con);
do
{
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case DEL:
DelContact(&con);
break;
case SEARCH:
SearchContact(&con);
break;
case MODIFY:
ModifyContact(&con);
break;
case PRINT:
Print(&con);
break;
case SORT:
SortContact(&con);
break;
case ALLDEL:
AllDelContact(&con);
break;
case EXIT:
DestroyContact(&con);
printf("退出通讯录\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
typedef struct PeoInfo
{
char name[NAME];
char sex[SEX];
int age;
char tele[TELE];
char addr[ADDR];
}PeoInfo;
typedef struct Contact
{
int sz;
int capacity;
PeoInfo data[0];
}Contact, * Pcontact;
Contact* InitContact()
{
Contact* ptr = (Contact*)malloc(sizeof(Contact) + CAP * sizeof(PeoInfo));
if (ptr == NULL)
{
perror("malloc:");
return NULL;
}
ptr->sz = 0;
ptr->capacity = CAP;
return ptr;
}
int CheckCapacity(Contact** pc)
{
if ((*pc)->sz == (*pc)->capacity)
{
Contact* ptr = (Contact*)realloc((*pc), sizeof(Contact) + (((*pc)->capacity + EXPAN) * sizeof(PeoInfo)));
if (ptr == NULL)
{
printf("扩容失败:%s\n", strerror(errno));
return 0;
}
else
{
(*pc) = ptr;
(*pc)->capacity += EXPAN;
printf("扩容成功,当前容量为%d\n", (*pc)->capacity);
}
}
return 1;
}
void AddContact(Pcontact* pc)
{
int ret = CheckCapacity(pc);
if (ret == 0)
{
printf("空间不足,扩容失败\n");
return;
}
else
{
printf("请输入要添加人的姓名\n");
scanf("%s", (*pc)->data[(*pc)->sz].name);
printf("请输入要添加人的性别\n");
scanf("%s", (*pc)->data[(*pc)->sz].sex);
printf("请输入要添加人的年龄\n");
scanf("%d", &(*pc)->data[(*pc)->sz].age);
printf("请输入要添加人的电话\n");
scanf("%s", (*pc)->data[(*pc)->sz].tele);
printf("请输入要添加人的地址\n");
scanf("%s", (*pc)->data[(*pc)->sz].addr);
(*pc)->sz++;
printf("添加成功\n");
}
}
void menu()
{
printf("******************************\n");
printf("***** 1.add 2.del ******\n");
printf("**** 3.search 4.modify ****\n");
printf("***** 5.print 6.sort *****\n");
printf("***** 7.AllDel 0.exit *****\n");
printf("******************************\n");
}
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
PRINT,
SORT,
ALLDEL
};
int main()
{
int input = 0;
menu();
Contact*ptr=InitContact();
do
{
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&ptr);
break;
case DEL:
DelContact(ptr);
break;
case SEARCH:
SearchContact(ptr);
break;
case MODIFY:
ModifyContact(ptr);
break;
case PRINT:
Print(ptr);
break;
case SORT:
SortContact(ptr);
break;
case ALLDEL:
AllDelContact(ptr);
break;
case EXIT:
DestroyContact(ptr);
printf("退出通讯录\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
在初始化函数中,用ptr拿到malloc开辟出的空间并将ptr返回。
在添加函数中,传&ptr是因为:
如果传的是ptr:
错误示范:
int CheckCapacity(Contact* pc)
{
if ((pc)->sz == (pc)->capacity)
{
Contact* p = (Contact*)realloc((pc), sizeof(Contact) + (((pc)->capacity + EXPAN) * sizeof(PeoInfo)));
if (p == NULL)
{
printf("扩容失败:%s\n", strerror(errno));
return 0;
}
else
{
(pc) = p;
注意:pc=p,但是ptr的指向并没有改变,也就是ptr指向的空间没有增容,
(p指向的)增容的那块空间在函数结束后就可能找不到了
(pc)->capacity += EXPAN;
printf("扩容成功,当前容量为%d\n", (pc)->capacity);
}
}
return 1;
}
void AddContact(Pcontact pc)
{
int ret = CheckCapacity(pc);
if (ret == 0)
{
printf("空间不足,扩容失败\n");
return;
}
else
{
printf("请输入要添加人的姓名\n");
scanf("%s", (pc)->data[(pc)->sz].name);
printf("请输入要添加人的性别\n");
scanf("%s", (pc)->data[(pc)->sz].sex);
printf("请输入要添加人的年龄\n");
scanf("%d", &(pc)->data[(pc)->sz].age);
printf("请输入要添加人的电话\n");
scanf("%s", (pc)->data[(pc)->sz].tele);
printf("请输入要添加人的地址\n");
scanf("%s", (pc)->data[(pc)->sz].addr);
(pc)->sz++;
printf("添加成功\n");
}
}
#include
#include
#include
#include
#define NAME 10
#define SEX 5
#define TELE 12
#define ADDR 30
#define CAP 2
#define EXPAN 1
typedef struct PeoInfo
{
char name[NAME];
char sex[SEX];
int age;
char tele[TELE];
char addr[ADDR];
}PeoInfo;
typedef struct Contact
{
int sz;
int capacity;
PeoInfo data[0];
}Contact, * Pcontact;
Contact* InitContact();
void DestroyContact(Contact* pc);
void Print(Contact* pc);
void AddContact(Pcontact* pc);
void DelContact(Pcontact pc);
void SearchContact(Contact* pc);
void ModifyContact(Contact* pc);
void SortContact(Contact* pc);
int CheckCapacity(Contact** pc)
{
if ((*pc)->sz == (*pc)->capacity)
{
Contact* ptr = (Contact*)realloc((*pc), sizeof(Contact) + (((*pc)->capacity + EXPAN) * sizeof(PeoInfo)));
if (ptr == NULL)
{
printf("扩容失败:%s\n", strerror(errno));
return 0;
}
else
{
(*pc) = ptr;
ptr->capacity += EXPAN;
printf("扩容成功,当前容量为%d\n", (*pc)->capacity);
}
}
return 1;
}
Contact* InitContact()
{
Contact* ptr = (Contact*)malloc(sizeof(Contact) + CAP * sizeof(PeoInfo));
if (ptr == NULL)
{
perror("malloc:");
return NULL;
}
ptr->sz = 0;
ptr->capacity = CAP;
return ptr;
}
void DestroyContact(Contact* pc)
{
pc->capacity = 0;
pc->sz = 0;
free(pc);
pc = NULL;
}
void Print(Contact* pc)
{
printf("%-10s %-8s %-5s %-12s %-30s\n",
"姓名", "性别", "年龄", "电话", "地址");
for (int i = 0; i < pc->sz; i++)
{
printf("%-10s %-8s %-5d %-12s %-30s\n",
pc->data[i].name,
pc->data[i].sex,
pc->data[i].age,
pc->data[i].tele,
pc->data[i].addr);
}
printf("打印成功\n");
}
void AddContact(Pcontact* pc)
{
int ret = CheckCapacity(pc);
if (ret == 0)
{
printf("空间不足,扩容失败\n");
return;
}
else
{
printf("请输入要添加人的姓名\n");
scanf("%s", (*pc)->data[(*pc)->sz].name);
printf("请输入要添加人的性别\n");
scanf("%s", (*pc)->data[(*pc)->sz].sex);
printf("请输入要添加人的年龄\n");
scanf("%d", &(*pc)->data[(*pc)->sz].age);
printf("请输入要添加人的电话\n");
scanf("%s", (*pc)->data[(*pc)->sz].tele);
printf("请输入要添加人的地址\n");
scanf("%s", (*pc)->data[(*pc)->sz].addr);
(*pc)->sz++;
printf("添加成功\n");
}
}
static int Find_by_name(char name[], Contact* pc)
{
for (int i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
void DelContact(Pcontact pc)
{
char name[NAME] = { 0 };
if (pc->sz == 0)
{
printf("通讯录为空,无法删除\n");
return;
}
printf("请输入要删除人的名字\n");
scanf("%s", name);
int pos = -1;
for (int i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
pos = i;
}
}
if (pos == -1)
{
printf("要删除的人不存在\n");
return;
}
int i = 0;
for (i = pos; i < pc->sz - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
//i+1的位置不用管,因为sz访问不到
pc->sz--;
printf("删除成功\n");
}
void SearchContact(Contact* pc)
{
char name[NAME] = { 0 };
if (pc->sz == 0)
{
printf("通讯录为空\n");
return;
}
printf("请输入要查找人的名字\n");
scanf("%s", name);
int ret = Find_by_name(name, pc);
if (ret == -1)
{
printf("查无此人\n");
return;
}
printf("找到了\n");
printf("%-10s %-8s %-5s %-12s %-30s\n",
"姓名", "性别", "年龄", "电话", "地址");
printf("%-10s %-8s %-5d %-12s %-30s\n",
pc->data[ret].name,
pc->data[ret].sex,
pc->data[ret].age,
pc->data[ret].tele,
pc->data[ret].addr);
}
void ModifyContact(Contact* pc)
{
char name[NAME] = { 0 };
if (pc->sz == 0)
{
printf("通讯录为空\n");
return;
}
printf("请输入要修改人的名字\n");
scanf("%s", name);
int ret = Find_by_name(name, pc);
if (ret == -1)
{
printf("要修改的人的信息不存在\n");
return;
}
printf("请输入修改后人的姓名\n");
scanf("%s", pc->data[ret].name);
printf("请输入修改后人的性别\n");
scanf("%s", pc->data[ret].sex);
printf("请输入修改后人的年龄\n");
scanf("%d", &pc->data[ret].age);
printf("请输入修改后人的电话\n");
scanf("%s", pc->data[ret].tele);
printf("请输入修改后人的地址\n");
scanf("%s", pc->data[ret].addr);
printf("修改成功\n");
}
int cmp_by_name(const void* p1, const void* p2)
{
return strcmp(((PeoInfo*)p1)->name, ((PeoInfo*)p2)->name);
}
void SortContact(Contact* pc)
{
qsort(pc->data, pc->sz, sizeof(PeoInfo), cmp_by_name);
printf("排序成功\n");
}
void menu()
{
printf("******************************\n");
printf("***** 1.add 2.del ******\n");
printf("**** 3.search 4.modify ****\n");
printf("***** 5.print 6.sort *****\n");
printf("***** 0.exit *****\n");
printf("******************************\n");
}
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
PRINT,
SORT,
};
int main()
{
int input = 0;
menu();
Contact*ptr=InitContact();
do
{
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&ptr);
break;
case DEL:
DelContact(ptr);
break;
case SEARCH:
SearchContact(ptr);
break;
case MODIFY:
ModifyContact(ptr);
break;
case PRINT:
Print(ptr);
break;
case SORT:
SortContact(ptr);
break;
case EXIT:
DestroyContact(ptr);
printf("退出通讯录\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}