时间:2022-01-04 10:56:20 | 栏目:C代码 | 点击:次
实现一个通讯录,通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
没有开辟动态内存的方法:
头文件:test.h
#ifndef __CONTACT #define __CONTACT #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<assert.h> #define N 1000 typedef struct contact { char name[30]; char gender[10]; int age; int telephone; char address[100]; };//结构体没有定义变量属于声明一个结构体类型 void meau(); void show(struct contact *p, int len);//struct contact *p:结构体指针指向这个结构体, int len:结构体数组的长度 void Add_linkman(struct contact *p, int len,int flag); int Delete_linkman(struct contact *p, int d_number, int len);//int d_number选择要删除第几个结构体的内容 void Find_member(struct contact *p, int d_number, int len);//int d_number表示要查找的第几个结构体的内容(即联系人的信息) void empty(struct contact *p); void Modify(struct contact *p, int M_member);//int M_member:要修改的第几个联系人 int cmp(const void *a, const void *b);//qsort里面的比较函数定义任意类型 void sort(struct contact *p, int len);//根据联系人的名字进行排序 #endif//条件编译
contact.c文件
#include"test.h" void meau() { printf(" *******************************************\n"); printf(" *******************************************\n"); printf(" ##############My address book##############\n"); printf(" *1-Add 2-Delete 3-Find *\n"); printf(" *4-Empty 5-Modify 6-Sort *\n"); printf(" *0-Exit 7-Show *\n"); printf(" *******************************************\n"); printf(" *******************************************\n"); } void show(struct contact *p, int len) { assert(p); int i = 0; for (i = 0; i < len; i++) { printf("name:%s gender:%s age:%d telephone:%d address:%s", \ p[i].name, p[i].gender, p[i].age, p[i].telephone, p[i].address); printf("\n"); } } void Add_linkman(struct contact *p, int len,int flag) { assert(p); int i = 0; for (i = flag; i < len; i++) { printf("请输入姓名:"); scanf("%s", &p[i].name); printf("请输入姓别:"); scanf("%s", &p[i].gender); printf("请输入年龄:"); scanf("%d", &p[i].age); printf("请输入电话:"); scanf("%d", &p[i].telephone); printf("请输入地址:"); scanf("%s", &p[i].address); } } int Delete_linkman(struct contact *p, int d_number, int len) { assert(p); int i = 0; for (i = d_number - 1; i < len - 1; i++) { p[i] = p[i + 1]; } } void Find_member(struct contact *p, int d_number, int len) { assert(p); if (d_number - 1 >= 0 || d_number - 1<len) { printf("name:%s gender:%s age:%d telephone:%d address:%s", \ p[d_number].name, p[d_number].gender, p[d_number].age, p[d_number].telephone, p[d_number].address); printf("\n"); } else { printf("不存在该联系人:"); return; } } void empty(struct contact *p) { assert(p); int i = 0; for (i = 0; i <1000; i++) { memset(p+i, 0, sizeof(struct contact)); } } void Modify(struct contact *p, int M_member) { assert(p); printf("修改之前联系人的信息为:"); printf("\n"); printf("name:%s gender:%s age:%d telephone:%d address:%s", \ p[M_member - 1].name, p[M_member - 1].gender, p[M_member - 1].age, p[M_member - 1].telephone, p[M_member - 1].address); printf("\n"); printf("请输入要修改的信息:"); printf("请输入姓名:"); scanf("%s", &p[M_member - 1].name); printf("请输入姓别:"); scanf("%s", &p[M_member - 1].gender); printf("请输入年龄:"); scanf("%d", &p[M_member - 1].age); printf("请输入电话:"); scanf("%d", &p[M_member - 1].telephone); printf("请输入地址:"); scanf("%s", &p[M_member - 1].address); } int cmp(const void *a, const void *b) { struct contact *aa = (struct contact *)a; struct contact *bb = (struct contact *)b; if (aa->name != bb->name) return(strcmp((aa->name), (bb->name))); } void sort(struct contact *p, int len) { assert(p); qsort(p, len, sizeof(struct contact), cmp); }
test.c文件:
#include"test.h" int main() { int num = 0; struct contact student[N]; int len = 0; int flag = 0;//定义一个标志位来结构体数组中每个结构体的位置 int total = N; int delete_number = 0; meau(); while (1) { printf("请输入数字进行选择:"); scanf("%d", &num); switch (num) { case 1:{ printf("请添加len个学生的信息:"); scanf("%d", &len); Add_linkman(student,len+flag,flag); flag=flag+len; }break; case 2:{ printf("请输入要删除的第i个学生的信息:"); scanf("%d", &delete_number); Delete_linkman(student, delete_number, len+flag); flag = flag - 1; }break; case 3:{ int Find_number = 0; printf("请输入要查找的第i个学生的信息:"); scanf("%d", &Find_number); Find_member(student, delete_number, len+flag); }break; case 4:{ printf("清空所有联系人:"); empty(student); }break; case 5:{ printf("请输入要修改的的第i个学生的信息:"); int M_member = 0; scanf("%d", &M_member); Modify(student, M_member); }break; case 6:{ printf("根据名字排序所有联系人:\n"); sort(student, len+flag); }break; case 7:{ printf("打印所有联系人的信息:\n"); show(student, flag); }break; case 0:{ exit(1); } default:printf("enter error data!!!"); } } system("pause"); return 0; }