作者前言
🎂 ✨✨✨✨✨✨🍧🍧🍧🍧🍧🍧🍧🎂
🎂 作者介绍: 🎂🎂
🎂 🎉🎉🎉🎉🎉🎉🎉 🎂
🎂作者id:老秦包你会, 🎂
简单介绍:🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂🎂
喜欢学习C语言和python等编程语言,是一位爱分享的博主,有兴趣的小可爱可以来互讨 🎂🎂🎂🎂🎂🎂🎂🎂
🎂个人主页::小小页面🎂
🎂gitee页面:秦大大🎂
🎂🎂🎂🎂🎂🎂🎂🎂
🎂 一个爱分享的小博主 欢迎小可爱们前来借鉴🎂
通讯录的实现
- **作者前言**
- 通讯录的结构
- 通讯录的各各方向
- 通讯录的大致架构
- 菜单
- 人的信息和通讯录
- 结构体变量初始化
- 增加成员
- 显示
- 删除
- 查找
- 修改
- 排序
- 最终的结构
- 这个通讯录的问题
- 总结
通讯录的结构
我们要写一个通讯录首先要知道具体架构
- 可以保存100个人的信息
- 删除指定联系人的信息
- 查找指定联系人的信息
- 修改指定联系人的信息
- 排序通讯录的信息
我们还要知道联系人的信息有哪里
- 名字
- 年龄
- 性别
- 电话
- 住址
现在我们大概清楚了通讯录的大致架构下面我们一一解决
通讯录的各各方向
通讯录的大致架构
#include "通讯录声明.h"
int main()
{int input = 0;;printf("是否开始通讯录的操作(0(否)/1(是)):>");scanf("%d",&input);switch (input){case 0:printf("欢迎下次光临,大佬慢走\n");break;case 1:while (1){int select;emun();printf("你要进行的行为是:>");scanf("%d", &select);switch (select){case 0://结束break;case 1://增break;case 2://删break;case 3://查break;case 4://修改break;case 5://排序break;case 6://显示break;default:printf("你的操作有误,请重新操作\n");break;}if (select == 0)break;}printf("操作完成,请慢走\n");break;default:printf("输入错误,请重新输入\n");}return 0;
}
或者是这种
#include "通讯录声明.h"
enum Operate
{EXIT,ADD,DEL,SEEK,MODIFY,SORT,SHOW,};
int main()
{int input = 0;printf("是否开始通讯录的操作(0(否)/1(是)):>");scanf("%d", &input);do{if (input != 0 && input != 1){printf("输入错误,请重新输入\n");printf("是否开始通讯录的操作(0(否)/1(是)):>");scanf("%d", &input);continue;}int select;emun();printf("你要进行的行为是:>");scanf("%d", &select);switch (select){case EXIT://结束break;case ADD://增break;case DEL://删break;case SEEK://查break;case MODIFY://修改break;case SORT://排序break;case SHOW://显示break;default:printf("你的操作有误,请重新操作\n");break;}if (select == 0){break;}} while (input);printf("退出成功,欢迎下次光临,大佬慢走\n");return 0;
}
我以第二种架构为例
菜单
void emun()
{printf("********************************\n");printf("****** 0.Exit 1.Add ******\n");printf("****** 2.Del 3.Seek ******\n");printf("****** 4.modify 5.sort ******\n");printf("****** 6.short ******\n");printf("********************************\n");
}
人的信息和通讯录
//人的信息
struct People
{char name[NAME];int age;char sex[SEX];char phone[PHONE];char address[ADDRESS];};
//封装成通讯录的样子
struct address_book
{struct People peple_address_num[PEPLE_ADDRESS_NUM]; //人的信息集合int conut; //统计人数
};
结构体变量初始化
void initialization(struct address_book* table)
{assert(table);(*table).conut = 0;//peple_address_num数组的初始化 ---方法1int sz = sizeof(struct People) * PEPLE_ADDRESS_NUM;memset(table->peple_address_num, 0, sz);//peple_address_num数组的初始化 ---方法2/*int i = 0;for (i = 0; i < ADDRESS; i++){(*table).peple_address_num[i].age = 0;int j = 0;for (j = 0; j < NAME; j++){(*table).peple_address_num[i].name[j] = 0;}for (j = 0; j < SEX; j++){(*table).peple_address_num[i].sex[j] = 0;}for (j = 0; j < PHONE; j++){(*table).peple_address_num[i].phone[j] = 0;}for (j = 0; j < ADDRESS; j++){(*table).peple_address_num[i].address[j] = 0;}}*/
}
增加成员
void Add_address(struct address_book* table)
{assert(table);//判断count的大小if (table->conut == PEPLE_ADDRESS_NUM){printf("通讯录已经满了");return;}getchar();printf("输入名字:");gets(table->peple_address_num[table->conut].name);printf("输入年龄:");scanf("%d", &(table->peple_address_num[table->conut].age));getchar();printf("输入性别:");gets(table->peple_address_num[table->conut].sex);printf("输入电话号码:");gets(table->peple_address_num[table->conut].phone);printf("输入地址");gets(table->peple_address_num[table->conut].address);
}
显示
void Show_address(const struct address_book* table)
{assert(table);if (table->conut == 0){printf("内容为空,无需打印\n");return;}int i = 0;printf("显示如下:\n");printf("%-20s %-5s %-5s %-12s %-100s\n", "名字", "年龄", "性别", "电话", "地址");for (i = 0; i < table->conut; i++){printf("%-20s ", table->peple_address_num[i].name);printf("%-5d ", table->peple_address_num[i].age);printf("%-5s ", table->peple_address_num[i].sex);printf("%-12s ", table->peple_address_num[i].phone);printf("%-100s\n", table->peple_address_num[i].address);}
}
删除
void Del_adderss( struct address_book * table)
{assert(table);//判断通讯录是否为空if (table->conut == 0){printf("通讯录为空\n");return;}//开始删除printf("你要删除人的名字是:");char arr[NAME];scanf("%s", arr);int idx = Seek_adderss(table, arr);if ( idx != -1){//进行删除table->conut--;while (idx < table->conut){table->peple_address_num[idx] = table->peple_address_num[idx + 1];idx++;}printf("删除成功\n");}elseprintf("无这个人\n");}
查找
int Seek_adderss(struct address_book* table, char * arr)
{assert(arr && table);int i = 0;for (i = 0; i < table->conut; i++){if (strcmp((char*)(&(table->peple_address_num[i])), arr) == 0){return i;}}return -1;
}
修改
void Modify_adderss(struct address_book* table)
{assert(table);//判断通讯录是否为空if (table->conut == 0){printf("通讯录为空\n");return;}//开始修改printf("你要修改人的名字是:");char arr[NAME];scanf("%s", arr);int idx = Seek_adderss(table, arr);if (idx != -1){//进行修改printf("请开始修改\n");printf("修改名字:");scanf("%s", table->peple_address_num[idx].name);printf("修改年龄:");scanf("%d", &(table->peple_address_num[idx].age));printf("修改性别:");scanf("%s", table->peple_address_num[idx].sex);printf("修改电话:");scanf("%s", table->peple_address_num[idx].phone);printf("修改地址:");scanf("%s", table->peple_address_num[idx].address);printf("修改成功\n");}elseprintf("无这个人\n");
}
排序
//排序
void Sort_address(struct address_book* table)
{assert(table);int input;printf("输入你要排序的数字>1(名字),2(年龄),3(性别),4(电话),5(地址)<:");scanf("%d", &input);switch (input){//名字case 1:qsort(table->peple_address_num, table->conut, sizeof(table->peple_address_num[0]), strsort);printf("成功");break;//年龄case 2:qsort(table->peple_address_num, table->conut, sizeof(table->peple_address_num[0]), strsort1);printf("成功");break;// 性别case 3:qsort(table->peple_address_num, table->conut, sizeof(table->peple_address_num[0]), strsort2);printf("成功");break;// 电话case 4:qsort(table->peple_address_num, table->conut, sizeof(table->peple_address_num[0]), strsort3);printf("成功");break;//地址case 5:qsort(table->peple_address_num, table->conut, sizeof(table->peple_address_num[0]), strsort4);printf("成功\n");break;default:printf("输入错误");break;}
}//名字排序
int strsort(const void* e1, const void* e2)
{return strcmp(((struct People*)e1)->name, ((struct People*)e2)->name);
}
//年龄
int strsort1(const void* e1, const void* e2)
{return ((struct People*)e1)->age - ((struct People*)e2)->age;
}
//性别
int strsort2(const void* e1, const void* e2)
{return strcmp(((struct People*)e1)->sex, ((struct People*)e2)->sex);
}
//电话
int strsort3(const void* e1, const void* e2)
{return strcmp(((struct People*)e1)->phone, ((struct People*)e2)->phone);
}
//地址
int strsort4(const void* e1, const void* e2)
{return strcmp(((struct People*)e1)->address, ((struct People*)e2)->address);
}
最终的结构
#include "通讯录声明.h"
int main()
{//创建一个通讯表struct address_book table;//初始化通讯表initialization(&table);int input = 0;printf("是否开始通讯录的操作(0(否)/1(是)):>");scanf("%d", &input);do{if (input != 0 && input != 1){printf("输入错误,请重新输入\n");printf("是否开始通讯录的操作(0(否)/1(是)):>");scanf("%d", &input);continue;}int select;emun();printf("你要进行的行为是:>");scanf("%d", &select);switch (select){case EXIT://结束break;case ADD://增Add_address(&table);table.conut++;printf("增加成功\n");break;case DEL://删Del_adderss(&table);break;case SEEK://查printf("你要查找人的名字是:");char arr[NAME];scanf("%s", arr);int i = Seek_adderss(&table, arr);if (i == -1)printf("无这个人\n");else{printf("显示如下:\n");printf("%-20s %-5s %-5s %-12s %-100s\n", "名字", "年龄", "性别", "电话", "地址");printf("%-20s ", table.peple_address_num[i].name);printf("%-5d ", table.peple_address_num[i].age);printf("%-5s ", table.peple_address_num[i].sex);printf("%-12s ", table.peple_address_num[i].phone);printf("%-100s\n", table.peple_address_num[i].address);}break;case MODIFY://修改Modify_adderss(&table);break;case SORT://排序Sort_address(&table);break;case SHOW://显示Show_address(&table);break;default:printf("你的操作有误,请重新操作\n");break;}if (select == 0){break;}} while (input);printf("退出成功,欢迎下次光临,大佬慢走\n");return 0;
}
这个通讯录的问题
- 这里录入的信息是存放在内存中,只要程序或者掉电了就会丢失
- 这里的通讯录的大小是写死的,如果要实现通讯录满了要扩充就要使用到动态内存管理函数
- 函数调用主要是依赖查找函数,缺少这个不行
总结
这个通讯录大致来讲缺陷很多,如果要实现更加完美,我们就要使用后面的知识,代码我会上传到gitee里面有空可以看看,