• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C语言实现简单电子通讯录(2)

c语言 搞代码 4年前 (2022-01-06) 44次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了C语言实现简单电子通讯录的第二部分,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言实现简单电子通讯录的具体代码,供大家参考,具体内容如下

这两天学完系统调用和标准IO,之前的通讯录可以进行一些改进,将数据保存到文件中(图我这里就不发了)。

原理:每次启动程序时先从预设文件中以只读的形式读取保存的通讯录信息,然后将读取到的信息导入到结构体中;每次执行退出时,先将预设文件以更新方式打开,并将文件中之前保存的信息清空,然后将结构体的数据保存到文件中去,再退出程序。

还有在文件写入时和读取时,先写入(读取)保存数据的总个数,让程序知道有多少数据要写入(读取),然后保存每个数据的字节数(程序会根据每个数据的字节数来进行保存),最后再保存数据。

与之前版本相比,改动的只有主函数中加入了读取文件和写入数据这两个步骤,还有的变动就是结构体从一个变成了两个,将指针从原来一个结构体中分离出来,方便数据从文件中的导入导出。下面是代码(免得你们往前翻,我把之前的代码都修改了考过来):

头文件 head.h 唯一修改的就是结构体

 #ifndef HEAD_H_ #define HEAD_H_ #include  #include  #include  #include        // sleep函数头文件 #include  #include  #include  #define uint unsigned int #define OK      0 #define ERROR     -1 #define MALLOC_ERROR -2 #define N       20 typedef int ElementType; typedef struct data { ElementType ID;       // ID号 char Name [N];       // 姓名 char Mobile_Phone [N];   // 手机号码 char Home_Address [N];   // 家庭住址 char Company_Tell [N];   // 公司电话 }Data; typedef struct _Node { Data data; struct _Node* next;     // 节点指针 }Node; typedef Node* PNode;      // 重命名节点指针类型 //显示操作界面 int Interface_Display (); //添加好友信息 (尾插法) int Add_Friend (PNode head, ElementType num); //显示所有好友信息 int Friend_Information (PNode head); //查找好友 int Search_Friend (PNode head, char* Name); //删除好友 void Delete_Friend (PNode head, char* Name); #endif

源文件 head.c 关于结构体的部分要进行修改

 #include "head.h" //显示操作界面 int Interface_Display () { system ("clear"); printf ("\t*****************************************\n"); printf ("\t~     欢迎使用通讯录        ~\n"); printf ("\t~                    ~\n"); printf ("\t~   1 >>>>>>>> 添加好友信息     ~\n"); printf ("\t~   2 >>>>>>>> 列表好友信息     ~\n"); printf ("\t~   3 >>>>>>>> 搜索好友       ~\n"); printf ("\t~   4 >>>>>>>> 删除好友       ~\n"); printf ("\t~   5 >>>>>>>> 退出         ~\n"); printf ("\t~                    ~\n"); printf ("\t~                    ~\n"); printf ("\t~            作者:believe ~\n"); printf ("\t*****************************************\n"); printf ("                      \n"); printf ("                      \n"); printf ("\t请输入对应数字选择相应功能:"); } //添加好友信息 (尾插法) int Add_Friend (PNode head, ElementType num) { if (NULL == head) { return ERROR; } //创建一个新的结点 PNode p = (PNode) malloc(sizeof(Node)/sizeof(char)); if (NULL == p) { return MALLOC_ERROR; } //将新数据赋给新结点 system("clear"); printf ("\t*************添加好友***************\n"); p->data.ID = num; printf ("\t好友的ID为:%d\n", p->data.ID); printf ("\n"); printf ("\t请输入好友的名字:"); scanf ("%s", p->data.Name); printf ("\n"); printf ("\t请输入好友的手机号:"); scanf ("%s", p->data.Mobile_Phone); printf ("\n"); printf ("\t请输入好友的家庭住址:"); scanf ("%s", p->data.Home_Address); printf ("\n"); printf ("\t请输入好友的公司电话:"); scanf ("%s", p->data.Company_Tell); printf ("\n"); p->next = NULL; //找到最后一个结点 PNode Ptmp;         //将头结点地址给临时指针Ptmp Ptmp = head; while (Ptmp->next) { Ptmp = Ptmp->next; } Ptmp->next = p; return OK; } //显示所有好友信息 int Friend_Information (PNode head) { if (NULL == head) { return ERROR; } PNode p = head->next; printf ("\tID\t姓名\t\t手机号\t\t住址\t\t\t公司电话\n"); while (p) { printf ("\t%d\t%s\t\t%s\t\t%s\t\t\t%s\n", p->data.ID, p->data.Name,p->data.Mobile_Phone, p->data.Home_Address, p->data.Company_Tell); p = p->next; } putchar('\n'); return OK; } //通过名字查找好友 int Search_Friend (PNode head, char* Name) { PNode p = head; PNode q = NULL; if ((NULL != p) && NULL != (p->next)) { while (p->next) { q = p->next; if ((NULL != q) && 0 == (strcmp(q->data.Name, Name))) { printf ("\t好友信息: \n\tID:%d\n\t姓名: %s\n\t手机号码: %s\n\t家庭地址:%s\n\t公司电话: %s\n", q->data.ID, q->data.Name, q->data.Mobile_Phone, q->data.Home_Address, q->data.Company_Tell); } else { printf ("\t对不起,您的通讯录没有该好友!\n"); } p = p->next; } } /* 另一种做法 if (NULL == head) { return ERROR; } PNode p; int flag = 1; for (p = head->next; p != NULL; p = p->next) { if (0 == strcmp(p->data.Name, Name)) { flag = 0; printf ("\t好友信息:\n\tID: %d\n\t姓名: %s\n\t手机号码: %s\n\t家庭地址: %s\n\t公司电话: %s\n", p->data.ID, p->data.Name, p->data.Mobile_Phone, p->data.Home_Address, p->data.Company_Tell); } } fi (flag) { printf ("\t对不起,您的通讯录没有该好友!\n"); } putchar('\n'); */ return OK; } //删除好友 void Delete_Friend (PNode head, char* Name) { PNode p = head; PNode q = NULL; while (NULL != p && NULL != (p->next)) { q = p->next; if (NULL != q && 0 == strcmp(q->data.Name, Name)) { p->next = q->next; free(q); int j; printf ("\t正在删除\n"); printf ("\t请稍候"); fflush (stdout);      //强制刷新缓存,输出显示 for (j = 0; j next && 0 != strcmp(q->data.Name, Name)) { printf ("\t您的通讯录没有该好友!\n"); } p = p->next; } }

主函数 main.c 加入了数据的读取和写入,已标明

 /******************************************************************* 需求:制作一个电子通讯录,通过该通讯录能录入好友ID号、姓名(英文)、手 机号码,家庭住址,公司电话。 模块: 主界面:主要显示软件功能,A)添加好友信息 B)列表好友信息。(包含排序 功能) C)搜索好友 D)删除好友 A)用户输入INSERT命令后,让用户输入好友信息。添加成功或失败都需提示 B)用户输入DISPLAY命令后,好友信息升序排列 C)用户输入SEARCH命令后,让用户输入将要搜索好友姓名查询。如果未搜索 到请友好提示。如果搜索到,显示处该好友信息 D)用户输入DELETE命令后,让用户输入将要删除好友姓名删除,如果存在同 名的多个好友,则列表出,所有同名的好友信息,让用户通过输入ID号删除 提示用户删除成功。 **********************************************************************/ #include "head.h" int main () { int Function; int i = 0; char Name[N]; int cho; // 创建头结点并为其分配空间 PNode head_node = (PNode) malloc(sizeof(Node)/sizeof(char)); if (NULL == head_node) { return MALLOC_ERROR; } head_node->next = NULL; /**************************************************************** 打开存放信息的文件并将里面的数据导入到链表中区 ****************************************************************/ // 以只读方式打开存放信息的文件, FILE *fp1 = fopen ("student.txt", "r+"); if (NULL == fp1) { printf ("fopen"); return -1; } PNode tmp = head_node; int count; int ret; // 保存读记录的个数并判断是否读到文件结尾,如果读到文件结尾, // 它返回一个非0 的值 ret = fread (&count, sizeof(int), 1, fp1); if(ret != 0) { for (i = 0; i data), len, 1, fp1);   // 读取数据 node->next = NULL; while (tmp->next)            // 向后遍历 { tmp = tmp->next; } tmp->next = node;            // 将结点导入链表中 } } if (ret == 0 && !feof(fp1))           // 读取失败 { perror ("fread"); return -1; } fclose (fp1);                  // 关闭刚刚打开的文件 /**************************************************************** 文件导入链表结束,关闭文件 ****************************************************************/ i = 1;               // i初始化(i既是ID编号) while (1) { Interface_Display ();      // 主界面 scanf ("%d", &Function); switch (Function)        // 功能选择 { case 1:           // 添加好友 { Function = 0; Add_Friend (head_node, i++); int j; printf ("\t正在添加\n"); printf ("\t请稍<div style="color:transparent">来源gaodai^.ma#com搞#代!码网</div>候"); fflush (stdout);    // 强制刷新缓存,输出显示 for (j = 0; j <2; j++) { sleep (1);     // Linux 使用sleep,参数为秒 printf ("."); fflush (stdout);  // 强制刷新缓存,输出显示 } printf ("\n"); printf ("\t添加成功!\n"); printf ("\t返回主菜单请输入1:"); scanf ("%d", &cho); if (1 == cho) { break; } else { printf ("\t对不起!您的输入有误!请重新输入:"); scanf ("%d", &cho); break; } break; } case 2:         // 显示好友信息 { system ("clear"); printf ("\t***********好友信息******************\n"); printf ("\n"); Friend_Information (head_node); Function = 0; printf ("\t返回主菜单请输入1:"); scanf ("%d", &cho); if (1 == cho) { break; } else { printf ("\t对不起!您的输入有误!请重新输入:"); scanf ("%d", &cho); break; } break; } case 3:         // 查找好友 { system ("clear"); printf ("\t*************查找好友*************\n"); printf ("\t请输入您要查找的好友姓名:"); scanf ("%s", Name); printf ("\n"); int j; printf ("\t正在查找\n"); printf ("\t请稍候"); fflush (stdout);    // 强制刷新缓存,输出显示 for (j = 0; j next;     // tmp为第一个结点 count = 0;           // 用来存放链表长度 while(tmp)           // 求链表长度 { count++; tmp = tmp->next; } // 要写入数据的个数 fwrite(&count, sizeof(int), 1, fp2); tmp = head_node;        // tmp初始化 while (tmp->next) { Node* p = tmp->next; tmp->next = p->next; // 写入数据的长度 int len = sizeof(p->data); fwrite (&len, sizeof(int), 1, fp2); //写入数据 fwrite (&(p->data), sizeof(Data), 1, fp2); free (p); } fclose (fp2); /**************************************************************** 数据保存结束,即将退出程序 ****************************************************************/ Function = 0; system ("clear"); exit (0); } default:          //输入有误 { Function = 0; printf ("\t对不起!您的输入有误!请重新输入:"); scanf ("%d", &Function); break; } } } return 0; }

以上就是C语言实现简单电子通讯录(2)的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C语言实现简单电子通讯录(2)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址