contactlist.h
#ifndef _CONTACT_ #define _CONTACT_ #include <stdio.h> #include <windows.h> #include <assert.h> #include <malloc.h> #include <stdlib.h> #include <string.h> #define INIT_CAP 64 #define INC_SIZE 128 #pragma warning(disable:4996) typedef struct man{ char name[64]; int sex; //0->man ,1->woman int age; char tel[12]; char addr[128]; }man_t, *man_p, **man_pp; typedef struct _contact{ man_p listp; int size; int cap; }contact_t, *contact_p, **contact_pp; void init(contact_pp c, int cap);//初始化 void destroy(contact_p c); void myAdd(contact_p c, man_p m);//增加联系人 void myDelete(contact_p c, char *name);//删除指定联系人 void myModify(contact_p c, char *name);//修改指定联系人信息 void mySearch(contact_p c, char *name);//查询指定人 void myShow(contact_p c);//查看所有联系人 void myClear(contact_p c);//清除所有联系人 void mySort(contact_p c);//按名字排序所有联系人 #endif
contactlist.c
#include "contactList.h" //检测当前的通讯录是否已经满了 //如果满了,就自动增加空间 static int inc(contact_p c) { if (c->cap == c->size){ c->listp = realloc(c->listp, c->cap + INC_SIZE); if (c->listp == NULL){ perror("realloc"); exit(3); } c->cap += INC_SIZE; printf("inc space....\n"); } return 1; } int myFind(contact_p c, char *name) { man_p start = c->listp; int i = 0; while (i < c->size) { if (strcmp(start[i].name, name) == 0) { return i; } i++; } return -1; } void init(contact_pp c, int cap) { assert(c); assert(cap>0); *c = (contact_p)malloc(sizeof(contact_t)); if (NULL == *c){ perror("malloc"); exit(1); } (*c)->listp = (man_p)malloc(sizeof(man_t)*cap); if (NULL == (*c)->listp){ perror("malloc"); exit(2); } (*c)->cap = cap; (*c)->size = 0; } void destroy(contact_p c) { assert(c); if (c->listp){ free(c->listp); c->listp = NULL; } free(c); } void myAdd(contact_p c, man_p m) { assert(c); assert(m); if (inc(c)){ memmove(c->listp + c->size, m, sizeof(man_t)); c->size++; } } void myDelete(contact_p c, char *name) { assert(c); assert(name); man_p start = c->listp; int i = 0; while (i < c->size){ if (strcmp(start[i].name, name) == 0){ start[i] = c->listp[c->size - 1]; c->size--; printf("Delete succeed!\n"); break; } } } void myModify(contact_p c, char *name) { assert(c); assert(name); int ret = myFind(c, name); if (ret != -1) { printf("Modified name:"); scanf("%s", &c->listp[ret].name); printf("Modified sex:"); scanf("%d", &c->listp[ret].sex); printf("Modified age:"); scanf("%d", &c->listp[ret].age); printf("Modified telephone:"); scanf("%s", &c->listp[ret].tel); printf("Modified address:"); scanf("%s", c->listp[ret].addr); } else printf("Not found!\n"); } void mySearch(contact_p c, char *name) { int ret = myFind(c, name); if (ret != -1) { printf("%s ", c->listp[ret].name); printf("%d ", c->listp[ret].sex); printf("%d ", c->listp[ret].age); printf("%s ", c->listp[ret].tel); printf("%s\n", c->listp[ret].addr); } else printf("Not found!\n"); } void myShow(contact_p c) { int i = 0; for (i = 0; i < c->size;i++) { printf("%s ", c->listp[i].name); printf("%d ", c->listp[i].sex); printf("%d ", c->listp[i].age); printf("%s ", c->listp[i].tel); printf("%s\n", c->listp[i].addr); } } void myClear(contact_p c) { c->size = 0; } void mySort(contact_p c) { int i = 0; int j = 0; struct man tmp; for (i = 0; i < c->size-1; i++) { for (j = i; j < c->size; j++) { if (strcmp(c->listp[i].name, c->listp[j].name)>0) { tmp = c->listp[i]; c->listp[i] = c->listp[j]; c->listp[j] = tmp; } } } for (i = 0; i < c->size; i++) { printf("%s %d %d %s %s\n", c->listp[i].name, \ c->listp[i].sex, c->listp[i].age, c->listp[i].tel, c->listp[i].addr); } printf("\n"); }
test.c
#include "contactList.h" int main() { contact_p myContact = NULL; init(&myContact, INIT_CAP); int action = 0; while (1){ printf("#################################\n"); printf("## 1. add 2.delete ##\n"); printf("## 3. modify 4.search ##\n"); printf("## 5. clear 6.show ##\n"); printf("###7. sort 0.exit ##\n"); printf("#################################\n"); printf("Please Select> "); scanf("%d", &action); switch (action) { case 0: //keepContact(myContact); //destroy(myContact); exit(0); break; case 1: { man_t man; printf("Your name: "); scanf("%s", &man.name); printf("Your sex[0(man), 1(woman)]: "); scanf("%d", &man.sex); printf("Your age: "); scanf("%d", &man.age); printf("Your telphone: "); scanf("%s", &man.tel); printf("Your address: "); scanf("%s", &man.addr); myAdd(myContact, &man); } break; case 2: { char name[64]; printf("Please Enter name: "); scanf("%s", name); myDelete(myContact, name); } break; case 3: { char name[64]; man_t man; printf("Please enter the name:"); scanf("%s", &name); myModify(myContact, name); } break; case 4: { char name[64]; printf("Please enter the name:"); scanf("%s", &name); mySearch(myContact, name); } break; case 5: { myClear(myContact); } break; case 6: { myShow(myContact); } break; case 7: { mySort(myContact); } break; default: { printf("error!Please enter again!\n"); } break; } } system("pause"); return 0; }
模拟实现通讯录(可变长)
最新推荐文章于 2020-12-24 14:17:45 发布