简陋版C语言仿真通讯录
https://blog.csdn.net/csdn_kou/article/details/80287640
简陋版C语言仿真通讯录之动态内存开辟版本
给Contact结构体增加一个容量,来表示什么时候增容
#define MAX_NAME 20
typedef struct PeoInfo
{char name[MAX_NAME];int age;char sex[4];
}people;typedef struct Contact
{people * data;int count;int capacity;
}Contact, *pContact;
初始化容量大小,这里我们做小一点可以看到增容效果
/*初始化*/
void InitContact(pContact pc)
{pc->count = 0;pc->capacity = DEFAULT_SZ;pc->data = (people*)calloc(sizeof(people), pc->capacity);
}
检查是否需要增容放在了增加数据的第一句作为判断
/*增加数据*/
void check_cap(pContact pc)
{if (pc->count == pc->capacity){people* p = (people *) realloc(pc->data,(pc->capacity+2)*sizeof(people));if (p != NULL){pc->data = p;p = NULL;}pc->capacity += 2;printf("增容成功");}
}
最后别忘了,动态内存开辟空间要释放
void Free(pContact pc)
{free(pc->data);pc->capacity = 0;pc->count = 0;pc->data = NULL;printf("释放空间成功\n");
}
完整代码
main.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "contact.h"int main()
{int input = 0;Contact my_con;InitContact(&my_con);do {menu();printf("please chose");scanf("%d", &input);//运行第二遍是输入字母默认为1switch (input){case ADD:AddContact(&my_con);justice(&my_con);break;case SHOW:ShowContact(&my_con);break;case DEL:DelContact(&my_con);break;case EXIT:Free(&my_con);break;case SEARCH:Search(&my_con);break;case SORT:Sort(&my_con);break;default:fflush(stdin);fflush(stdout);//sync();//清除缓冲区//clear();//清除错误状态break;}} while (input);system("pause");return 0;
}
contact.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "contact.h"void menu()
{printf("*************************\n");printf("**1.add 2.show*******\n");printf("**3.search 4.delete*****\n");printf("**5.sort 0.exit*******\n");printf("*************************\n");
}/*初始化*/
void InitContact(pContact pc)
{pc->count = 0;pc->capacity = DEFAULT_SZ;pc->data = (people*)calloc(sizeof(people), pc->capacity);
}/*增加数据*/
void check_cap(pContact pc)
{if (pc->count == pc->capacity){people* p = (people *) realloc(pc->data,(pc->capacity+2)*sizeof(people));if (p != NULL){pc->data = p;p = NULL;}pc->capacity += 2;printf("增容成功");}
}void AddContact(pContact pc)
{check_cap(pc);if (pc->count < 1000){printf("请输入名字>");scanf("%s", pc->data[pc->count].name);printf("请输入年龄>");scanf("%d", &pc->data[pc->count].age);printf("请输入性别>");scanf("%s", pc->data[pc->count].sex);pc->count++;}else{printf("存不下了");return;}
}/*展示*/
void ShowContact(pContact pc)
{printf("%-11s %-11s %-11s \n","姓名","年龄","性别");if (pc->count != 0){for (int i = 0; i < pc->count; i++){if (pc->data[i].age != 0){printf("%d|%-11s %-11d %-11s \n", i,pc->data[i].name,pc->data[i].age,pc->data[i].sex);}}}else{printf("通讯没有号码\n");return;}
}/*删除*/
void DelContact(pContact pc)
{printf("请输入要删除的编号");int i = 0, num;scanf("%d", &num);for (i = num; i < pc->count; i++){pc->data[i] = pc->data[i + 1];}pc->count - 1;
}/*判断输入的名字是不是重复,如果重复就删除*/
void justice(pContact pc)
{for (int i = 0; i < pc->count - 1; i++){if (!strcmp(pc->data[pc->count - 1].name, pc->data[i].name)){printf("\n非法输入\n");pc->data[pc->count - 1] = pc->data[pc->count];}}
}/*比较*/
char compare(const void * a, const void * b)
{return (*(char*)a - *(char*)b);
}void Sort(pContact pc)
{int i;qsort(pc->data, pc->count, sizeof(people), compare);for (i = 0; i<pc->count; i++)printf("%d|%-11s %-11d %-11s \n", i, pc->data[i].name, pc->data[i].age, pc->data[i].sex);return;
}/*查找*/
int search_son(pContact pc, char *name)
{int i, j;for (i = 0; i < pc->count; i++){if (!strcmp(name, pc->data[i].name)){return i;}}return -1;
}void Search(pContact pc)
{int i = 0;char name[20];//要给一个大小,要不然一直读取位置时发生访问冲突printf("name>");scanf("%s", &name);i = search_son(pc, name);if (i >= 0){printf("找到了,呜啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦!\n");printf("%d|%-8s%8d%11s\n", i, pc->data[i].name, pc->data[i].age, pc->data[i].sex);printf("******************************************\n");}else{printf("Can't find it\n");}return;
}void Free(pContact pc)
{free(pc->data);pc->capacity = 0;pc->count = 0;pc->data = NULL;printf("释放空间成功\n");
}
contact.h
#ifndef __CONTACT_H__
#define __CONTACT_H__#define DEFAULT_SZ 3
/*
这个顺序就是switch()的顺序,注意逗号不是分号,最后一个不写符号
*/
enum OPTION
{EXIT,ADD,SHOW,SEARCH,DEL,SORT
};#define MAX_NAME 20
typedef struct PeoInfo
{char name[MAX_NAME];int age;char sex[4];
}people;typedef struct Contact
{people * data;int count;int capacity;
}Contact, *pContact;void AddContact(pContact pc);
void InitContact(pContact pc);
void ShowContact(pContact pc);
void DelContact(pContact pc);
void justice(pContact pc);
void Search(pContact pc);
void Sort(pContact pc);
void Free(pContact pc);
#endif // !__CONTACT_H__