C语言程序设计—通讯录实现

本篇文章主要是实现一个简易的通讯录:

功能如下:

  1. 添加用户
  2. 修改用户
  3. 删除用户
  4. 查找用户(可重名)
  5. 按名字或年龄排序
  6. 显示用户
  7. 保存通讯录
  8. 日志追加

 有如下知识点:

  1. 动态数组
  2. 结构体
  3. 枚举
  4. 自定义标识符和宏
  5. 文件打开与存储
  6. 函数
  7. 指针
  8. 循环 
  9. 排序

简述特点:

  1. 将人员信息放在一个PeoInf的结构体中,再创建一个结构体List,用于存放peoinf这个结构体的指针,和容量与目前通讯录人员数量。
  2. 再用realloc动态开辟以结构体peoinf为大小的内存,用于实现动态内存开辟。
  3. 程序运行后,初始化这段空间,并查询是否有“contact.txt”的文件存在,如果存在,则读取文件里的内容,并放到peoinf的结构体“数组”中,并实时监控是否需要扩容。如果不存在就创建文件。
  4. 随后就可以添加、修改、查找、删除用户,每一次增删改查都会被记录到一个“contact_log.txt”的文件里,这里使用了时间戳。
  5. 用qsort进行名字或年龄进行排序
  6. 程序会知道本次是否进行修改,如果修改后就退出会提示是否需要保存,当然也可以自己手动保存。
  7. 程序以“rb”和“wb”进行文件的读写。
  8. 程序实现了重名查找,在有重名的情况下会进行选择。
  9. (代码可直接运行,复制到编译器vs2019即可)

代码部分

  1. contact.h

    #pragma once
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <assert.h>
    #include <time.h>
    #include <sys/stat.h>#define MAX_10 10
    #define MAX_20 20
    //#define LOG_PRINT(x) fprintf(pf, "In %s %s ,user %s name calling %s\n",__DATE__,__TIME__,(x),(list->pl+num)->name)
    typedef struct PeoInf
    {char name[MAX_20];int age;char sex[5];char tel[12];char addr[30];
    }PeoInf;
    //定义通讯录结构体,嵌套PeoInf
    typedef struct List
    {//动态内存分配PeoInf* pl;int count;int capacity;//容量
    }List;
    //定义一个枚举变量,用于存储菜单所有选择
    enum select_all
    {EXIT,//0ADD,DEL,SELECT,MODIFY,SORT,SHOW,SAVE
    };
    //菜单函数
    void menu();
    //定义初始化list函数
    void initialise_list(List* list);
    //定义添加信息函数
    void add_peoinf(List* list);
    //定义显示函数
    void show_list(List* list);
    //定义删除函数
    void del_peoinf(List* list);
    //定义通过找名字查找人的函数(已实现重名查找)
    int find_byname(const List* list);
    //定义查找人
    void sele_peoinf(const List* list);
    //定义修改人信息函数
    void modify_peoinf(List* list);
    //定义删除和修改已找到下标的信息函数
    void del_havefond(List* list, int position);
    void modify_havefond(List* list, int position);
    //定义排序函数
    void sort_peoinf(List* list);
    //定义文件保存函数
    void file_save(List* list);
    //定义扩容函数
    void expand_list(List* list);
  2. actualize.c

    #include "contact.h"
    //实现菜单函数
    void menu(){printf("----------------------------------------\n");printf("--------1. add      2. del--------------\n");printf("--------3. select   4. modify-----------\n");printf("--------5. sort     6. show-------------\n");printf("--------------7. save-------------------\n");printf("--------------0. exit-------------------\n");printf("----------------------------------------\n");
    }
    //实现初始化list函数
    void initialise_list(List* list){PeoInf* ptr = (PeoInf*)calloc(3, sizeof(PeoInf));//默认开辟三个人的存储空间if (ptr == NULL) {printf("%s", strerror(errno));return ;}list->pl = ptr;list->count = 0;list->capacity = 3;FILE* pf = NULL;struct stat buffer;//判断文件是否存在if (stat("contact.txt", &buffer) != 0) {//不存在就创建文件pf = fopen("contact.txt", "wb");fclose(pf);pf = NULL;return;}pf = fopen("contact.txt", "rb");if (pf != NULL) {for (int i = 0; fread(list->pl + i, sizeof(PeoInf), 1, pf) != 0; i++) {if (list->count == list->capacity - 1) {expand_list(list);}list->count++;}fclose(pf);}pf = NULL;
    }
    //扩容函数
    void expand_list(List* list) {PeoInf* ptr =(PeoInf*) realloc(list->pl, (list->capacity + 2)*sizeof(PeoInf));//每次增加两个if (ptr == NULL) {printf("%s", strerror(errno));return;}list->pl = ptr;list->capacity += 2;
    }
    //实现添加日志功能
    /*
    * return 0 失败
    * return 1 成功
    */
    int add_log(List* list,char* moving,int num) {//打开文件FILE* pf=NULL;//判断写入模式是否要追加if (list->count == 0) {pf = fopen("contact_log.txt", "w");}else {pf = fopen("contact_log.txt", "a");}//如果打开失败,报错if (pf == NULL) {perror("fopen:");return 0;}//获取时间戳time_t rawtime;struct tm* timeinfo;time(&rawtime);timeinfo = localtime(&rawtime);fprintf(pf, "In %s \tuser %s name calling %s.\n", asctime(timeinfo), moving, (list->pl + num)->name);fclose(pf);pf = NULL;return 1;
    }
    //实现添加信息功能
    void add_peoinf(List* list) {assert(list);//断言//判断是否需要扩容if (list->count == list->capacity) {expand_list(list);//内部函数,不必去头文件里定义printf("Automatic capacity expansion is successful,\n and the current address book capacity is %d\n", list->capacity);}printf("Please enter the name\n->");scanf("%s", (list->pl+list->count)->name);printf("Please enter age\n->");scanf("%d", &(list->pl + list->count)->age);printf("Please enter sex\n->");scanf("%s", (list->pl + list->count)->sex);printf("Please enter the telephone\n->");scanf("%s", (list->pl + list->count)->tel);printf("Please enter the address\n->");scanf("%s", (list->pl + list->count)->addr);//添加日志log功能if (!add_log(list,"add",list->count)) {printf("log fail,please find excause.\n");}list->count++;printf("succeed!\n");
    }
    //实现显示函数
    void show_list(List* list) {assert(list);printf("name\tage\tsex\ttelephone\taddr\n");for (int i = 0; i < (list->count); i++) {printf("%s\t%d\t%s\t%s\t\t%s\n", (list->pl + i)->name,(list->pl + i)->age, (list->pl + i)->sex, (list->pl + i)->tel, (list->pl + i)->addr);}
    }
    //实现通过寻找名字,找到这个人
    //重名默认存放数组为10,如需变大可改为动态扩容实现
    int find_byname(const List* list) {char s_name[MAX_20] = { 0 };int count = 0;int find_result[MAX_10] = { 0 };printf("Please enter the name that you want \n->");scanf("%s", s_name);for (int i = 0; i < list->count; i++) {if (strcmp((list->pl + i)->name, s_name)==0) {//找到了if (count == 0) {printf("Find the information, please confirm\n");printf("number\tname\tage\tsex\ttelephone\taddr\n");}printf("%d\t%s\t%d\t%s\t%s\t\t%s\n", count+1,(list->pl + i)->name,(list->pl + i)->age, (list->pl + i)->sex, (list->pl + i)->tel, (list->pl + i)->addr);find_result[count] = i;//将找到的坐标存入数组中count++;//判断是否有重复}}if (count == 0) {//找不到printf("Check no such person, please confirm after the input!\n");return -1;}else if (count == 1) {return find_result[0];}else {//两个以上int select_num = 0;while (1) {printf("Please select the object serial number that you want to operate on\n->");scanf("%d", &select_num);if (select_num >= 1 && select_num <= count) {//输入正确序号,方可返回return find_result[select_num - 1];}else {printf("error,please reenter\n");}}}
    }
    //实现删除函数
    void del_peoinf(List* list) {assert(list);int del_num = find_byname(list);if (del_num < 0) return;//查找失败for (int i = 0; i < list->count - del_num - 1; i++) {*(list->pl + del_num + i) = *(list->pl + del_num + 1 + i);}list->count--;printf("delet successfully\n");if (!add_log(list, "delet", del_num)) {printf("log fail,please find excause.\n");}
    }
    void del_havefond(List* list,int position) {assert(list);for (int i = 0; i < list->count - position - 1; i++) {*(list->pl + position + i) = *(list->pl + position + 1 + i);}list->count--;printf("delet successfully\n");if (!add_log(list, "delet", position)) {printf("log fail,please find excause.\n");}
    }
    //实现查找信息功能
    void sele_peoinf(const List* list) {assert(list);int find_num = find_byname(list);if (find_num < 0) return;//查找失败printf("The information is as follows\n");printf("name\tage\tsex\ttelephone\taddr\n");printf("%s\t%d\t%s\t%s\t\t%s\n", (list->pl + find_num)->name,(list->pl + find_num)->age, (list->pl + find_num)->sex, (list->pl + find_num)->tel, (list->pl + find_num)->addr);int input_find = 0;do {printf("--------------------------------\n");printf("--------------------------------\n");printf("-----1. del-------2. modif------\n");printf("------------0. exit-------------\n");printf("--------------------------------\n");printf("--------------------------------\n");printf("please enter what you want\n->");scanf("%d", &input_find);if (input_find == 1) {del_havefond(list, find_num);return;}else if (input_find == 2) {modify_havefond(list, find_num);return;}else if (input_find != 0) {printf("Input is wrong, please reagain\n");}} while (input_find);}
    void modify_havefond(List* list,int position) {assert(list);printf("Please enter the new name\n->");scanf("%s", (list->pl + position)->name);printf("Please enter new age\n->");scanf("%d", &(list->pl + position)->age);printf("Please enter new sex\n->");scanf("%s", (list->pl + position)->sex);printf("Please enter the new telephone\n->");scanf("%s", (list->pl + position)->tel);printf("Please enter the new address\n->");scanf("%s", (list->pl + position)->addr);printf("Modified successfully\n");if (!add_log(list, "modify", position)) {printf("log fail,please find excause.\n");}
    }
    //实现修改信息
    void modify_peoinf(List* list) {assert(list);int mod_num = find_byname(list);if (mod_num < 0) return;//查找失败printf("Please enter the new name\n->");scanf("%s", (list->pl + mod_num)->name);printf("Please enter new age\n->");scanf("%d", &(list->pl + mod_num)->age);printf("Please enter new sex\n->");scanf("%s", (list->pl + mod_num)->sex);printf("Please enter the new telephone\n->");scanf("%s", (list->pl + mod_num)->tel);printf("Please enter the new address\n->");scanf("%s", (list->pl + mod_num)->addr);printf("Modified successfully\n");if (!add_log(list, "modify",mod_num)) {printf("log fail,please find excause.\n");}
    }
    //qsort的比较函数
    int compare_name(const void* e1,const void* e2) {return strcmp(((PeoInf*)e1)->name, ((PeoInf*)e2)->name);}
    int compare_age(const void* e1, const void* e2) {return ((PeoInf*)e1)->age - ((PeoInf*)e2)->age;}
    //实现排序函数
    void sort_peoinf(List* list) {int sort_input = 0;do {printf("--------------------------------\n");printf("--------------------------------\n");printf("-----1. name-------2. age------\n");printf("------------0. exit-------------\n");printf("--------------------------------\n");printf("--------------------------------\n");printf("please enter you want sort by\n->");scanf("%d", &sort_input);if (sort_input == 1) {qsort(list->pl, list->count, sizeof(PeoInf), compare_name);printf("sort by name successfully\n");return;}else if (sort_input == 2) {qsort(list->pl, list->count, sizeof(PeoInf), compare_age);printf("sort by age successfully\n");return;}} while (sort_input);}
    void file_save(List *list) {FILE* pf = fopen("contact.txt", "wb");//二进制写入if (pf == NULL) {perror("fopen:");return;}//写数据for (int i = 0; i < list->count; i++) {fwrite(list->pl+i, sizeof(PeoInf), 1, pf);}fclose(pf);pf = NULL;
    }
  3. test.c

    #include "contact.h"
    int main()
    {int input = 0;List list;initialise_list(&list);//动态内存开辟,记得用完销毁int modify_num = 0;//修改数量记录do{menu();printf("Please choose!\n->");scanf("%d", &input);switch (input){case ADD: {add_peoinf(&list);modify_num++;}break;case DEL:{del_peoinf(&list);modify_num++;}break;case SELECT:{sele_peoinf(&list);}break;case MODIFY:{modify_peoinf(&list);modify_num++;}break;case SORT:{sort_peoinf(&list);}break;case SHOW:{show_list(&list);}break;case SAVE:{file_save(&list);printf("save in file sucessfully\n");modify_num = 0;}break;case EXIT:{if (modify_num != 0) {int save_select = 0;printf("=========1.save   2.no=============\n");printf("The modified data is not saved, whether it needs to be saved\n->");scanf("%d", &save_select);if (save_select == 1) {file_save(&list);printf("save in file sucessfully\n");}}printf("Ok,have a nice day! Bye~");}break;default:printf("Input is wrong,please reagain!\n");break;}} while (input);//销毁动态内存free(list.pl);list.pl = NULL;
    }

希望对大家有帮助!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/74604.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

matlab相机标定求得相机内参

素材下载 可以去官网下载标定板,然后使用我们的相机进行拍照(10~15张即可): 下载请点击这里:here 在拍摄照片之前,先量取对应的每个方格的长度: 如下: 使用MATLAB标定 打开MATLAB的命令行: 输入 cameraCalibrator #调用标定的工具箱 添加的是刚刚我们拍照标定板的…

微信小程序使用editor 富文本编辑器

.wxml <editor id"editor" class"item-content ql-container" placeholder"请输入新闻内容" bindstatuschange"onStatusChange" bindready"onEditorReady" bindinput"onInput" bindblur"onBlur">…

商城系统优化

1、DB、模板的渲染速度&#xff08;thymeleaf&#xff09;、静态资源、日志、JVM 数据库的优化&#xff08;参照数据库优化课程&#xff09;使用索引&#xff0c;减少数据库的交互次数、缓存 thymeleaf使用缓存 静态资源&#xff1a;放到nginx中&#xff0c;实现动静分离 2、…

2023-09-08力扣每日一题

链接&#xff1a; 2651. 计算列车到站时间 题意&#xff1a; 不看日期只看时间 解&#xff1a; &#xff1f; 实际代码&#xff1a; 还看&#xff01;你怎么肥四&#xff1f;int findDelayedArrivalTime(int arrivalTime, int delayedTime) {return (arrivalTimedelayed…

华为数通方向HCIP-DataCom H12-821题库(单选题:301-320)

第301题 某台路由器运行 IS-IS,其输出信息如图所示,下列说法错误的是? [R1]display isis sdb local verboseDatabase information for ISIS(1) Level-1 Link State Database LSPID Seq Num Checksum Holdtime…

电脑同时连接有线和无线网络怎么设置网络的优先级

电脑同时连接有线和无线网络怎么设置网络的优先级&#xff1a; 我们知道在 笔记本电脑系统 中&#xff0c;可以通过有线或无线网络进行联网。如果电脑在有线网络和无线网络同时存在的情况&#xff0c;应该怎么设置有线网络优先连接呢?对此我们提供下面的方法可以让电脑在有Wi…

Spring框架入门:构建你的第一个Web应用

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

python 编程应用:使用 Python 实现数据可视化

Python编程应用是一种非常流行的编程语言&#xff0c;它可以用于开发各种软件和应用程序。Python可以用于创建Web应用程序&#xff0c;桌面应用程序&#xff0c;移动应用程序&#xff0c;数据科学&#xff0c;机器学习&#xff0c;自然语言处理&#xff0c;系统管理&#xff0c…

Qt之postEvent

基本介绍 postEvent方法所属类为QCoreApplication&#xff0c;完整声明如下&#xff1a; [static] void QCoreApplication::postEvent(QObject *receiver, QEvent *event, int priority Qt::NormalEventPriority) 该方法的作用是将要发送的事件推送到对应线程的事件队列中&…

基于SpringBoot的在线教育平台系统

基于SpringBootVue的线教育平台系统&#xff0c;前后端分离 开发语言&#xff1a;Java数据库&#xff1a;MySQL技术&#xff1a;SpringBoot、Vue、Mybaits Plus、ELementUI工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 【主要功能】 角色&#xff1a;管理员、学生、老师 …

【网络编程】学习成果day7:用sqlite3和fgetc将字典写入数据库中的数据表。

1.将字典写入数据库中的数据表 代码&#xff1a; linuxlinux:~/study/NETbc$ cat 03_dictsqlite3.c #include<myhead.h> #define MAX 50int do_insert(sqlite3* db);int main(int argc, const char *argv[]) {//打开数据库sqlite3 *dbNULL;if(sqlite3_open("./dic…

修改Docker的运行时数据存储位置

Docker的运行时数据&#xff0c;包括镜像、容器、网络和存储卷等默认存储在/var/lib/docker目录下&#xff0c;如果硬盘容量有限我们则需要修改这个存储位置 修改步骤如下&#xff1a; 停止Docker服务&#xff1a;sudo systemctl stop docker 创建一个新的目录来存储Docker运…

【C++基础】5. 常量

文章目录 【 1. 常量的分类 】1.1 整型常量1.2 浮点常量1.3 字符常量1.4 字符串常量1.5 布尔常量 【 2. 常量的定义 】2.1 #define 预处理器2.2 const 关键字 常量 是固定值&#xff0c;在程序执行期间不会改变。这些固定的值&#xff0c;又叫做字面量。常量可以是任何的基本数…

企业架构LNMP学习笔记22

防盗链原理和实现。 域名A的资源文件&#xff0c;经常被域名B直接调用访问。 而用户经常访问域名B&#xff0c;看到的资源&#xff08;图片等&#xff09;以为是域名B的&#xff0c;实际则是域名A的。 但是域名A没有获得任何收益&#xff0c;却要给域名B来源的访问消耗服务器…

总结 - 组件通用封装思路(组件封装)

组件封装&#xff1a; 1. 不变&#xff1b; 2. 变&#xff1a;①prop ②slot插槽 详细总结在文末。 --------------------------------------------------------------------------------------------------- 问题&#xff1a;结构相似&#xff0c;内容不同 --》 可以用组…

在Kubernetes环境中有关Nginx Ingress与API Gateway的连接问题

文章目录 小结问题解决参考 小结 在Kubernetes环境中是通过Nginx Ingress来从外部访问Kubernetes内部的环境&#xff0c;并用API Gateway来分发请求&#xff0c;碰到了 502 Bad gateway.的问题&#xff0c;并尝试解决。 问题 从外部通过Nginx Ingress访问Kubernetes内部的环…

Shell命令管理进程

Shell命令管理进程 列出进程 ps命令 top命令 管理后台进程 启动后台进程 查看后台进程 jobs和ps的区别 停止进程 Linux除了是一种多用户操作系统之外&#xff0c;还是一种多任务系统。多任务意味着可以同时运行多个程序。Linux 提供了相关的工具来列出运行中的进程,监视…

ClickHouse进阶(七):Clickhouse数据查询-1

进入正文前&#xff0c;感谢宝子们订阅专题、点赞、评论、收藏&#xff01;关注IT贫道&#xff0c;获取高质量博客内容&#xff01; &#x1f3e1;个人主页&#xff1a;含各种IT体系技术,IT贫道_Apache Doris,大数据OLAP体系技术栈,Kerberos安全认证-CSDN博客 &#x1f4cc;订阅…

SpringMVC应用

文章目录 一、常用注解二、参数传递2.1 基础类型String2.2 复杂类型2.3 RequestParam2.4.路径传参 PathVariable2.4 Json数据传参 RequestBody2.5 RequestHeader 三、方法返回值3.1 void3.2 Stringmodel3.3 ModelAndView 一、常用注解 SpringMVC是一个基于Java的Web框架&#…

11.Redis的慢操作之rehash

Redis为什么快 它接收到一个键值对操作后&#xff0c;能以微秒级别的速度找到数据&#xff0c;并快速完成操作。 数据库这么多&#xff0c;为啥 Redis 能有这么突出的表现呢&#xff1f; 内存数据结构 一方面&#xff0c;这是因为它是内存数据库&#xff0c;所有操作都在内存上…