通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
//game.h
#ifndef __GAME_H__ #define __GAME_H__ #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> struct student { char name[20]; char sex[10]; int age; char tel[12]; char dres[100]; }; int add(struct student *pdest, int *num); void display(struct student *pdest, int *num); int find(struct student *pdest, int *num, char *nam); void del(struct student stu[], char *nam, int ret, int *num); void set(struct student *pdest, int ret); int cmp(const void *x, const void *y); void sort(struct student *pdest, int * num); #endif //__GAME_H__
//test.c
#include "game.h" int main() { struct student *stu = (struct student *)malloc(sizeof(struct student) * 1); if (stu != NULL) { int count = 1; int input = 0; int ret = 0; char nam[20] = { 0 }; do { printf("*******************************************\n"); printf("****** 1.add 2.del ******\n"); printf("****** 3.find 4.modify ******\n"); printf("****** 5.display 6.clear ******\n"); printf("****** 7.sort 0.exit ******\n"); printf("*******************************************\n"); printf("请输入操作数:>"); scanf("%d", &input); if (input == 0) exit(1); if (input != 1 && count == 1) printf("还没有成员录入,请先输入成员\n"); else { int flag = 1; switch (input) { case 1: { while (flag) { int res = add(stu, &count); if (res == 0) flag = 0; else { flag = 1; count++; struct student *stu_1 = realloc(stu, sizeof(struct student) * count); //通过不断循环输入,来控制不断开辟动态内存空间 stu = stu_1; } } } break; case 2: printf("请输入需要删除人的姓名:>"); scanf("%s", nam); ret = find(stu, &count, nam); if (ret != -1) { del(stu, nam, ret, &count); count--; } break; case 3: //查找联系人(根据姓名查找) printf("请输入需要查找人的姓名:>"); scanf("%s", nam); find(stu, &count, nam); break; case 4: //修改指定联系人 printf("请输入所要修改人的名字:"); scanf("%s", nam); ret = find(stu, &count, nam); if (ret != -1) set(stu, ret); break; case 5: //显示所以人 display(stu, &count); break; case 6: //清空 memset(stu, 0, sizeof(stu)); count = 1; break; case 7: //按姓名排序 sort(stu, &count); break; default: printf("输入错误!\n"); } } } while (input); } free(stu); system("pause"); return 0; }
//game.c
#include "game.h" int add(struct student *pdest, int *num) { assert(pdest); int ret = 0; char ch = 0; int j = *num - 1; printf("继续(Y)/退出(N)\n"); getchar(); scanf("%c", &ch); while (j < *num) { if (ch == 'Y') { printf("请依次输入学生姓名、性别、年龄、电话号码、家庭住址:>\n"); scanf("%s%s%d%s%s", ((pdest + j)->name), ((pdest + j)->sex), &((pdest + j)->age), \ ((pdest + j)->tel), ((pdest + j)->dres)); j++; } else return 0; } return 1; } void display(struct student *pdest, int *num) { assert(pdest); int i = 0; while (i < *num - 1) //判断条件设置的非常合理,因为当没有成员的时候,他就不显示了 { printf(" %d: %8s %8s %8d %16s %25s\n", i + 1, ((pdest + i)->name), ((pdest + i)->sex), ((pdest + i)->age), \ ((pdest + i)->tel), ((pdest + i)->dres)); i++; } } int find(struct student *pdest, int *num, char *nam) { assert(pdest); int i = 0; while (i < *num - 1) { if (strcmp((pdest + i)->name, nam) == 0) { printf(" %d: %8s %4s %2d %11s %20s\n", i + 1, ((pdest + i)->name), ((pdest + i)->sex), ((pdest + i)->age), \ ((pdest + i)->tel), ((pdest + i)->dres)); return i; } i++; } printf("没有你要查找的人!\n"); return -1; } void del(struct student stu[], char *nam, int ret, int *num) //应用到了数组中删除一个元素的算法 { //memset(pdest + ret, 0, sizeof(pdest + ret)); if (ret < *num - 1) { while (ret < *num - 1) { stu[ret] = stu[ret + 1]; //后一项替代前一项 ret++; } } } void set(struct student *pdest, int ret) { printf("请输入新的信息!\n"); scanf("%s%s%d%s%s", ((pdest + ret)->name), ((pdest + ret)->sex), &((pdest + ret)->age), \ ((pdest + ret)->tel), ((pdest + ret)->dres)); } int cmp(const void *x, const void *y) { //根据name的大小进行排序 // return strcmp((*(struct student *)x).name, (*(struct student *)y).name); } void sort(struct student *pdest, int *num) { //整个结构体数组交换 qsort(pdest, *num - 1, sizeof(struct student), cmp); }