动态实现实现通讯录,就需要使用malloc和realloc等这些函数,首先我们先来介绍一下这些函数。
(1)void *malloc(unsigned int size);
size是需要分配的字节数。
(2)void *calloc(unsigned int num_elements,unsigned int elements_size);
num_elements是分配的元素个数,elements_size是每个元素占的字节数。
(3)void *realloc(void *p,unsigned int new_size);
new_size是修改后的字节数,p是原先的内存首地址。
由于这些函数都是在堆(heap)里开辟的空间,使用完后需要释放,所以就需要free函数。
(4)void free(void *p);
p是需要释放的空间的首地址。
前边3个函数都需要free,如果不free就可能造成内存泄漏。free释放完成后需要置为NULL。我们在使用指针之前必须要有一个原则,那就是使用之前先判断是否为空、使用之后需要置为NULL。
下来我们就来实现动态增长通讯录。
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1 #ifndef __CONTACT_H__ #define __CONTACT_H__ #include <stdio.h> #include <string.h> enum OP { EXIT, ADD, DEL, MODIFY, SEARCH, DISPLAY, SORT, CLEAR }; #define NAME_MAX 20 #define SEX_MAX 5 #define TELE_MAX 12 #define ADDR_MAX 25 #define MAX 1000 #define MAX_INIT 2 #define MAX_RISE 2 typedef struct PerInfo { char name[NAME_MAX]; int age; char sex[SEX_MAX]; char tele[TELE_MAX]; char addr[ADDR_MAX]; }PeoInfo; typedef struct Contact { PeoInfo data[MAX]; int size; int i; }Contact,*Pcon; void init_contact(Pcon pcon); void _add_contact(Pcon pcon); void _display_contact(Pcon pcon); void _del_contact(Pcon pcon); void _search_contact(Pcon pcon); void _modify_contact(Pcon pcon); void _sort_contact(Pcon pcon); void _clear_contact(Pcon pcon); #endif //__CONTACT_H__
函数实现部分:
#define _CRT_SECURE_NO_WARNINGS 1 #include "Contact.h" // //git //svn // void init_contact(Pcon pcon) { //memset(pcon->data, 0, MAX*sizeof(PeoInfo)); //pcon->size = 0; pcon->data=(PerInfo *)malloc(MAX_INIT*sizeof(PerInfo)); if(pcon->data == NULL) { printf("out of menory"); exit(EXIT_FAILURE); } pcon->size=0; pcon->capacity=MAX_INIT; } void _add_contact(Pcon pcon) { /*if(pcon->size >= MAX) { printf("电话本满了\n"); return; }*/ if(pcon->size >= pcon->i) { PerInfo *tmp=(PerInfo *)realloc(pcon->data,(pcon->i+MAX_RISE)*sizeof(PerInfo)); //当实际联系人个数和初始化的容量相等时用realloc增容 if(tmp == NULL) { printf("out of menory\n"); exit(EXIT_FAILURE); } else { pcon->data=tmp; pcon->capacity+=MAX_RISE; } printf("请输入名字:>"); scanf("%s",pcon->data[pcon->size].name); printf("请输入年龄:>"); scanf("%d",&(pcon->data[pcon->size].age)); printf("请输入性别:>"); scanf("%s",pcon->data[pcon->size].sex); printf("请输入电话:>"); scanf("%s",pcon->data[pcon->size].tele); printf("请输入地址:>"); scanf("%s",pcon->data[pcon->size].addr); pcon->size++; printf("添加成功\n"); } void _display_contact(Pcon pcon) { int i = 0; printf("%9s\t%3s\t%4s\t%11s\t%10s\n","name","age","sex","tele","addr"); for(i = 0;i<pcon->size;i++) { printf("%9s\t%3d\t%4s\t%11s\t%10s\n", pcon->data[i].name, pcon->data[i].age, pcon->data[i].sex, pcon->data[i].tele, pcon->data[i].addr); } } static int find_entry(Pcon pcon, char *name) { int i = 0; for(i = 0;i<pcon->size; i++) { if(strcmp(pcon->data[i].name,name) == 0) { return i; } } return -1; } void _del_contact(Pcon pcon) { int pos = 0; int index = 0; char name[NAME_MAX]; if(pcon->size == 0) { printf("电话本空\n"); return; } printf("请输入要删除人的名字:>"); scanf("%s",name); pos = find_entry(pcon, name); if(pos == -1) { printf("找不到要删除的人\n"); return; } // for(index = pos; index < pcon->size; index++) { pcon->data[index] = pcon->data[index+1]; } pcon->size--; printf("删除成功\n"); } void _search_contact(Pcon pcon) { char name[NAME_MAX] = {0}; int pos = 0; printf("请输入要查找人的名字:>"); scanf("%s",name); pos = find_entry(pcon, name); if(pos == -1) { printf("指定联系人不存在\n"); return; } else { printf("%9s\t%3s\t%4s\t%11s\t%10s\n","name","age","sex","tele","addr"); printf("%9s\t%3d\t%4s\t%11s\t%10s\n", pcon->data[pos].name, pcon->data[pos].age, pcon->data[pos].sex, pcon->data[pos].tele, pcon->data[pos].addr); } } void _modify_contact(Pcon pcon) { char name[NAME_MAX] = {0}; int pos = 0; printf("请输入要修改人的名字:>"); scanf("%s",name); pos = find_entry(pcon, name); if(pos == -1) { printf("指定联系人不存在\n"); return; } else { printf("请输入名字:>"); scanf("%s",pcon->data[pos].name); printf("请输入年龄:>"); scanf("%d",&(pcon->data[pos].age)); printf("请输入性别:>"); scanf("%s",pcon->data[pos].sex); printf("请输入电话:>"); scanf("%s",pcon->data[pos].tele); printf("请输入地址:>"); scanf("%s",pcon->data[pos].addr); } } //volatile void _sort_contact(Pcon pcon) { int i = 0; int j = 0; for(i = 0;i<pcon->size-1; i++)//控制排序趟数 { for(j = 0; j<pcon->size-1-i; j++) { if(strcmp(pcon->data[j].name,pcon->data[j+1].name) > 0) { PeoInfo tmp = {0}; tmp = pcon->data[j]; pcon->data[j] = pcon->data[j+1]; pcon->data[j+1] = tmp; } } } } void _clear_contact(Pcon pcon) { pcon->size = 0; }
时间: 2024-11-05 22:35:00