我们使用多文件进行编写实现 "contact.h" #ifndef __CONTACT_H__ #define __CONTACT_H__ #define MAX_NAME 20 #define MAX_SEX 3 #define MAX_TELE 12 #define MAX_ADDR 20 #define MAX 1000 #define INIT_SIZE 100 #define INC 100 #include <stdio.h> #include <string.h> #include <stdlib.h> enum OP { EXIT, ADD, DEL, SEARCH, MODIFY, SHOW, CLR, SORT }; typedef struct Peo_Info { char name[MAX_NAME]; char sex[MAX_SEX]; int age; char tele[MAX_TELE]; char addr[MAX_ADDR]; }Peo_Info; typedef struct Dhb { Peo_Info *pinfo; int count; int size; }Dhb,*pDhb; void menu(); void init_dhb(pDhb pdhb); void add_dhb(pDhb pdhb); void del_dhb(pDhb pdhb); void search_dhb(pDhb pdhb); void modify_dhb(pDhb pdhb); void show_dhb(pDhb pdhb); void clear_dhb(pDhb pdhb); void sort_dhb(pDhb pdhb); void save(pDhb pdhb); #endif //__CONTACT_H__ "contact.c" #include "contact.h" void menu() { printf("*************************\n"); printf("**** 1.add 2.del ****\n"); printf("**** 3.search 4.modify***\n"); printf("**** 5.show 6.clear ***\n"); printf("*****7.sort 0.exit ***\n"); printf("*************************\n"); } static int find_entry(pDhb pdhb,const char* name) { int i = 0; for (i = 0; i < pdhb->count; i++) { if (0 == strcmp(name, pdhb->pinfo[i].name)) { return i; } } return -1; } static void init_add(pDhb pdhb, Peo_Info *peo) { if ((pdhb->count >= pdhb->size) && ((pdhb->size < MAX))) { if (pdhb->size + INC > MAX) { pdhb->size = MAX - pdhb->size; } else if (pdhb->size + INC <= MAX) { pdhb->size = pdhb->size + INC; } pdhb->pinfo = realloc(pdhb->pinfo, pdhb->size*sizeof(Peo_Info)); if (pdhb->pinfo == NULL) { printf("out of memory\n"); exit(1); } } else if ((pdhb->size >= MAX)) { printf("电话本已满\n"); return; } pdhb->pinfo[pdhb->count] = *peo; pdhb->count++; } void init_dhb(pDhb pdhb) { Peo_Info buf; FILE *pfread = fopen("contact.dat","r"); if (pfread == NULL) { perror("open file for read"); exit(EXIT_FAILURE); } pdhb->count = 0; pdhb->size = INIT_SIZE; pdhb->pinfo = (Peo_Info *)malloc((pdhb->size) *sizeof(Peo_Info)); if (pdhb->pinfo == NULL) { printf("out of memory\n"); exit(1); } while (fread(&buf, sizeof(Peo_Info), 1, pfread)) { init_add(pdhb, &buf); } fclose(pfread); } void add_dhb(pDhb pdhb) { if ((pdhb->count >= pdhb->size) && ((pdhb->size < MAX))) { if (pdhb->size + INC > MAX) { pdhb->size = MAX - pdhb->size; } else if (pdhb->size + INC <= MAX) { pdhb->size = pdhb->size + INC; } pdhb->pinfo = realloc(pdhb->pinfo, pdhb->size*sizeof(Peo_Info)); if (pdhb->pinfo == NULL) { printf("out of memory\n"); exit(1); } } else if ((pdhb->size >= MAX)) { printf("电话本已满\n"); return; } printf("名字:>"); scanf("%s", pdhb->pinfo[pdhb->count].name); printf("性别:>"); scanf("%s", pdhb->pinfo[pdhb->count].sex); printf("年龄:>"); scanf("%d", &pdhb->pinfo[pdhb->count].age); printf("电话:>"); scanf("%s", pdhb->pinfo[pdhb->count].tele); printf("住址:>"); scanf("%s", pdhb->pinfo[pdhb->count].addr); pdhb->count++; printf("添加成功\n"); } void del_dhb(pDhb pdhb) { char name[MAX_NAME]; int ret = 0; printf("请输入要删除的人的名字:>"); scanf("%s", name); ret = find_entry(pdhb, name); if (ret == -1) { printf("要删除的人不存在\n"); return; } else { int j = 0; for (j = ret; j < pdhb->count-1; j++) { pdhb->pinfo[j] = pdhb->pinfo[j+1]; } pdhb->count--; printf("删除成功\n"); } } void search_dhb(pDhb pdhb) { char name[MAX_NAME]; int ret = 0; printf("请输入要查找的人的名字:>"); scanf("%s", name); ret = find_entry(pdhb, name); if (ret == -1) { printf("要查找的人不存在\n"); return; } else { printf("%10s\t%5s\t%4s\t%10s\t%10s\n", "name", "sex", "age", "tele", "addr"); printf("%10s\t%5s\t%3d\t%10s\t%10s\n", pdhb->pinfo[ret].name, pdhb->pinfo[ret].sex, pdhb->pinfo[ret].age, pdhb->pinfo[ret].tele, pdhb->pinfo[ret].addr); } } void modify_dhb(pDhb pdhb) { char name[MAX_NAME]; int ret = 0; printf("请输入要修改的人的名字:>"); scanf("%s", name); ret = find_entry(pdhb, name); if (ret == -1) { printf("要修改的人不存在\n"); return; } else { printf("名字:>"); scanf("%s", pdhb->pinfo[ret].name); printf("性别:>"); scanf("%s", pdhb->pinfo[ret].sex); printf("年龄:>"); scanf("%d", &pdhb->pinfo[ret].age); printf("电话:>"); scanf("%s", pdhb->pinfo[ret].tele); printf("住址:>"); scanf("%s", pdhb->pinfo[ret].addr); } } void show_dhb(pDhb pdhb) { int i = 0; printf("%10s\t%5s\t%4s\t%10s\t%10s\n", "name", "sex", "age", "tele", "addr"); for (i = 0; i < pdhb->count; i++) { printf("%10s\t%5s\t%3d\t%10s\t%10s\n", pdhb->pinfo[i].name, pdhb->pinfo[i].sex, pdhb->pinfo[i].age, pdhb->pinfo[i].tele, pdhb->pinfo[i].addr); } printf("sz = %d\n", pdhb->size); } void clear_dhb(pDhb pdhb) { pdhb->count = 0; } void sort_dhb(pDhb pdhb) { int i = 0; int j = 0; for (i = 0; i < pdhb->count - 1; i++) { for (j = 0; j < pdhb->count - 1 - i; j++) { if (strcmp(pdhb->pinfo[j].name, pdhb->pinfo[j + 1].name)>0) { Peo_Info tmp = pdhb->pinfo[j]; pdhb->pinfo[j] = pdhb->pinfo[j + 1]; pdhb->pinfo[j + 1] = tmp; } } } } void save(pDhb pdhb) { FILE* pfwrite = fopen("contact.dat", "w"); if (pfwrite == NULL) { perror("open file for write"); exit(EXIT_FAILURE); } int i = 0; for (i = 0; i < pdhb->count; i++) { fwrite(&pdhb->pinfo[i],sizeof(Peo_Info), 1, pfwrite); } fclose(pfwrite); } "test.c" #include <stdlib.h> #include "contact.h" int main() { Dhb dhb; int input = 1; init_dhb(&dhb); while (input) { menu(); printf("请选择>:"); scanf("%d", &input); switch (input) { case ADD: add_dhb(&dhb); break; case DEL: del_dhb(&dhb); break; case SEARCH: search_dhb(&dhb); break; case MODIFY: modify_dhb(&dhb); break; case SHOW: show_dhb(&dhb); break; case CLR: clear_dhb(&dhb); break; case SORT: sort_dhb(&dhb); break; case EXIT: { save(&dhb); free(dhb.pinfo); exit(EXIT_SUCCESS); break; } } } return 0; }
本文出自 “Vs吕小布” 博客,谢绝转载!
原文地址:http://survive.blog.51cto.com/10728490/1725971