1.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 #include<stdio.h> #include<string.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[MAX]; int count; }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_dhp(pDhb pdhb); void sort_dhp(pDhb pdhb); #endif// __CONTACT_H__
2。contact.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "contact.h" void menu() { printf("******1.add 2.del*******\n"); printf("******3.search 4.modify****\n"); printf("******5.show 6.clear*****\n"); printf("******7.sort 0.exit*******\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[pdhb->count].name)) { return i; } } return -1; } void init_dhb(pDhb pdhb) { //pdhp->pinfo = { 0 }; 数组不可以一次访问整个数组 pdhb->count = 0; } void add_dhb(pDhb pdhb) { if (pdhb->count > MAX) { printf("电话本已满"); return; } printf("名字:>"); scanf("%s", pdhb->pinfo[pdhb->count].name);//为什么是%s printf("性别:>"); scanf("%s", pdhb->pinfo[pdhb->count].age); printf("年龄:>"); scanf("%d", pdhb->pinfo[pdhb->count].name); 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 (-1 == ret) { printf("要删除的人不存在\n"); return; } else { int j = 0; for (j = ret; j < pdhb->count; j++) { pdhb->pinfo[j] = pdhb->pinfo[j + 1]; } } pdhb->count--; } void search_dhb(pDhb pdhb) { char name[MAX_NAME]; int ret = 0; scanf("%s", name); ret = find_entry(pdhb,name); if (-1 == ret) { printf("要查找的人不存在\n"); return; } else { printf("%10s\t%5s\t%5s\t%20s\t%20s\t", "name", "sex", "age", "tele", "addr"); printf("%10s\t%5s\t%5d\t%20s\t%20s\t", 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 (-1 == ret) { printf("要修改的人不存在"); return; } else { printf("名字:>"); scanf("%s", pdhb->pinfo[pdhb->count].name);//为什么是%s printf("性别:>"); scanf("%s", pdhb->pinfo[pdhb->count].age); printf("年龄:>"); scanf("%d", pdhb->pinfo[pdhb->count].name); printf("电话:>"); scanf("%s", pdhb->pinfo[pdhb->count].tele); printf("住址:>"); scanf("%s", pdhb->pinfo[pdhb->count].addr); printf("修改成功\n"); } } void show_dhb(pDhb pdhb) { int i = 0; for (i = 0; i < pdhb->count; i++) { printf("%10s\t%5s\t%5s\t%20s\t%20s\t", "name", "sex", "age", "tele", "addr"); printf("%10s\t%5s\t%5d\t%20s\t%20s\t", pdhb->pinfo[i].name, pdhb->pinfo[i].sex, pdhb->pinfo[i].age, pdhb->pinfo[i].tele, pdhb->pinfo[i].addr); } } void clr_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 - i - 1; j++) { if (1 == strcmp(pdhb->pinfo[j].name, pdhb->pinfo[j + 1].name)) { Peo_Info temp = pdhb->pinfo[j]; pdhb->pinfo[j] = pdhb->pinfo[j + 1]; pdhb->pinfo[j + 1] = temp; } } } }
3.test.c
#define _CRT_SECURE_NO_WARNINGS 1 #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:clr_dhb(&dhb); break; case SORT:sort_dhb(&dhb); break; case EXIT:exit(EXIT_SUCCESS); break; } } system("pause"); }