头文件:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>typedef struct NODE{int bh;char *name;char *phone;struct NODE *pNext;
}
Node;typedef struct PAGE
{int currentPage;int totalPage;int onePageItem;int totalItem;
} Page;int Getbh();
char *GetPhone();
char *GetName();
Node* GetNode();void InitInfo(Node**top,Node** end,int nLength);
void AddNode(Node** ppTop,Node** ppEnd,Node* node);
void InsertNode(Node** ppTop,Node** ppEnd,int bh,Node* node);
void DelNode(Node** ppTop,Node** ppEnd,int bh);Page*   GetPage(Node* top,int onePageItem);
Page* InitPage(Node* top,int onePageItem);
void ShowMenu(Page* page);
void Show(Node* top,Page* page);
char GetKey();
int g_menu_type = 0;
char g_key;
void LookContacts(Node* top);
void OperatePage(Node* top,Page* page);
Node* GetNodeIn();
char* Getstring();
void FindContacts(Node* top);
void DelContacts(Node** top,Node** end);
void UpContacts(Node* top);

主代码:

#include"m.h"int main()
{Node *top = NULL;Node *end = NULL;char key;InitInfo(&top,&end,120);while(1){printf("1.查看通讯录\n");printf("2.添加联系人\n");printf("3.查询联系人\n");printf("4.删除联系人\n");printf("5.修改联系人\n");printf("6.退出\n");key = GetKey();switch(key){case '1':g_menu_type = 1;LookContacts(top);break;case '2':AddNode(&top,&end,GetNodeIn());break;case '3':g_menu_type = 3;FindContacts(top);break;case '4':g_menu_type = 4;DelContacts(&top,&end);break;case '5':g_menu_type = 5;UpContacts(top);break;case '6':return 0;break;  }}return 0;
}int Getbh()
{static int a=0;a++;return a;
}char *GetPhone()
{char *phone =(char*)malloc(12);char c[2];int i;switch (rand()%4){case 0:strcpy_s(phone,12,"131");break;case 1:strcpy_s(phone,12,"132");break;case 2:strcpy_s(phone,12,"133");break;case 3:strcpy_s(phone,12,"131");break;}for(i=0;i<8;i++){itoa (rand()%10,c,10);strcat_s(phone,12,c);}return phone;
}char *GetName()
{char *name=(char*)malloc(9);int i;for(i=0;i<8;i++){name[i]=rand()%26+97;}name[8]=0;return name;
}Node* GetNode()
{Node *node=(Node*)malloc(sizeof(Node));node->bh=Getbh();node->name=GetName();node->phone=GetPhone();node->pNext=NULL;return node;
}void InitInfo(Node**top,Node** end,int nLength)
{int i;for(i=0;i<nLength;i++){AddNode(top,end,GetNode());}
}void AddNode(Node** ppTop,Node**ppEnd,Node* node)
{if(*ppTop==NULL){*ppTop=node;}else(*ppEnd)->pNext=node;*ppEnd=node;
}void InsertNode(Node** ppTop,Node** ppEnd,int bh,Node* node)
{Node* bj=*ppTop;if(bh==(*ppTop)->bh){node->pNext=*ppTop;*ppTop=node;return;}while(bj->pNext!=NULL){if( bj->pNext->bh==bh){node->pNext=bj->pNext;bj->pNext=node;return;}bj=bj->pNext;}(*ppEnd)->pNext=node;*ppEnd=node;
}void DelNode(Node** ppTop,Node** ppEnd,int bh)
{Node* pDel = NULL;Node* bj = *ppTop;//头删除if((*ppTop)->bh == bh){pDel = *ppTop;*ppTop = (*ppTop)->pNext;free(pDel);return ;}//中间删除while(bj->pNext){if(bj->pNext->bh == bh){//先让pDel指向要删除的节点pDel = bj->pNext;bj->pNext = bj->pNext->pNext;free(pDel);if(bj->pNext == NULL){*ppEnd = bj;}return ;}bj = bj->pNext;}
}Page*   GetPage(Node* top,int onePageItem)
{Page *page = (Page*)malloc(sizeof(Page));page->onePageItem = onePageItem;page->currentPage = 0;page->totalItem = 0;while(top!=NULL){page->totalItem++;top = top->pNext;}page->totalPage = page->totalItem%page->onePageItem == 0? page->totalItem/page->onePageItem  :  page->totalItem/page->onePageItem+1  ;return page;
}void ShowMenu(Page* page)
{switch(g_menu_type){case 1:printf("共%d条 共%d页 当前第%d页 w上一页,s下一页  b返回主菜单\n",page->totalItem,page->totalPage,page->currentPage);break;case 3:printf("共%d条 共%d页 当前第%d页 w上一页,s下一页 c重新查询  b返回主菜单\n",page->totalItem,page->totalPage,page->currentPage);break;case 4:printf("共%d条 共%d页 当前第%d页 w上一页,s下一页 c重新查询 d删除信息 b返回主菜单\n",page->totalItem,page->totalPage,page->currentPage);break;}
}void Show(Node* top,Page* page)
{int begin=(page->currentPage-1)*page->onePageItem+1;int end =page->currentPage*page->onePageItem;int count=0;while(top){count++;if(count>=begin&&count<=end)printf("%d %s %s\n",top->bh,top->name,top->phone);top = top->pNext;}}char GetKey()
{char c;char v=-1;int a = -1;while((c =getchar()) != '\n' || a == -1){a = 1;v = c;}return v;
}Page* InitPage(Node* top,int onePageItem)
{Page* page = (Page*)malloc(sizeof(Page));page->currentPage = 0;page->onePageItem = onePageItem;page->totalItem = 0;while(top){page->totalItem++;top = top->pNext;}page->totalPage = page->totalItem%page->onePageItem == 0? page->totalItem/page->onePageItem:page->totalItem/page->onePageItem+1;return page;
}void LookContacts(Node* top)
{Page* page = InitPage(top,10);OperatePage(top,page);}void OperatePage(Node* top,Page* page)
{char key = 's';int d;while( key != 'b'){switch(key){case 'w'://上一页if(page->currentPage == 1){printf("已经是第一页了\n");}else{page->currentPage--;Show(top,page);ShowMenu(page);}break;case 's'://下一页if(page->currentPage == page->totalPage){printf("已经是最后一页了\n");}else{page->currentPage++;Show(top,page);ShowMenu(page);}break;case 'c':  //重新查询return;case 'd': //删除return;default:printf("按错了,再来一次\n");}g_key = key = GetKey();}
}Node* GetNodeIn()
{Node* node = (Node*)malloc(sizeof(Node));node->bh = Getbh();printf("请输入名字:\n");node->name = Getstring();printf("请输入电话号码:\n");node->phone = Getstring();node->pNext = NULL;return node;}char* Getstring()
{int size = 5;char* str = (char*)malloc(size);char c;int count = 0;char* jstr = str;char* newstr = NULL;while((c=getchar()) != '\n'){//1.取字符 放到申请的空间里*str = c;str++;count++;//2.判断,如果空间不够了,申请更大的空间if(size == count+1 ){//把旧空间存的字符 变成字符串*str = 0;size += 5;//3.把旧空间的字符串拷贝到新的空间newstr = (char*)malloc(size);strcpy(newstr,jstr);//4.把旧的空间释放掉free(jstr);jstr = newstr;str = newstr + count;}}*str = 0;return jstr;
};void FindContacts(Node* top)
{//1.输入关键Node* node;char* keyword = NULL;Node* newtop = NULL;Node* newend = NULL;Node* bj = top;while(1){top = bj;newtop = NULL;newend = NULL;while(1){//输入关键字printf("请输入要查询的关键字:\n");keyword = Getstring();//a建确定 其他件重新输入printf("按a确定,其他键重新输入\n");if(GetKey() == 'a'){break;}}//2.根据关键字 查找链表中的节点while(top){if(strncmp(keyword,top->name,strlen(keyword)) == 0|| strncmp(keyword,top->phone,strlen(keyword)) == 0){//如果找到了,申请新的节点,把节点放到新的链表中node = (Node*)malloc(sizeof(Node));node->bh = top->bh;node->name = top->name;node->phone = top->phone;node->pNext = NULL;AddNode(&newtop,&newend,node);}top = top->pNext;}//3.找到 或者没找到;如果找到了,对新的链表进行分页显示if(newtop){LookContacts(newtop);}else{printf("没找到\n");}if(g_key == 'b'|| g_key == 'd'){return ;}}}void DelContacts(Node** top,Node** end)
{//1.调用查询 int bh;while(1){FindContacts(*top);if(g_key == 'b'){return ;}//2.删除信息printf("请输入要删除的编号:\n");bh = atoi(Getstring());DelNode(top,end,bh);}//3.y继续删除printf("y继续删除,其他键返回\n");if(GetKey() != 'y') {return ;}
}void UpContacts(Node* top)
{int bh;Node* bj = top;while(1) {FindContacts(top);if(g_key == 'b'){return ;}printf("请输入要修改的编号\n");bh = atoi(Getstring());bj = top;while(bj){if(bj->bh == bh){printf("请输入新的名字:\n");bj->name = Getstring();printf("请输入新的电话号码:\n");bj->phone = Getstring();break;}bj = bj->pNext;}if(bj == NULL){printf("没有此编号\n");}printf("y继续修改,其他键返回\n");if(g_key == 'y'){}else{return ;}}
}

C语言简单通讯录模板相关推荐

  1. C语言实现简单通讯录

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.分析问题 二.函数具体功能的实现 1.结构体的创建 2.对结构体进行初始化 3.增加联系人 4.find_by_n ...

  2. c语言实现简单通讯录(仅代码)

    C语言实现简单通讯录(仅代码) 头文件部分 #pragma once //头文件区域 #include<stdio.h> #include<Windows.h> #includ ...

  3. 用c++语言编写通讯录,C++实现简单通讯录

    本文实例为大家分享了C++实现简单通讯录的具体代码,供大家参考,具体内容如下 说明: 1 程序中运用到两个类,一个是Person类,另一个是List类.前者存储用户信息,后者主要用于操作,如增删改查等 ...

  4. 主流WEB开发语言简单对比(转)

    主流WEB开发语言简单对比  原文链接:http://www.uml.org.cn/site/201401091.asp   随着时间的飞逝,随着岁月的流失.从世界上第一台计算机ENIAC诞生至今,已 ...

  5. C语言中比较大小的函数模板,C语言中实现模板函数小结 : 不敢流泪

    --by boluor 2009/5/20 如果要写个函数支持多种数据类型,首先想到的就是C++的模板了,但是有时候只能用C语言,比如在linux内核开发中,为了减少代码量,或者是某面试官的要求- 考 ...

  6. C语言简单五子棋实现

    |版权声明:本文为博主原创文章,未经博主允许不得转载.博客地址:https://blog.csdn.net/sgsgy5 今天我们来看一下用C语言简单实现五子棋的实现,这里面只实现了普通功能,如果有需 ...

  7. 第四次c语言实验报告模板,C语言实验报告模板完成版

    C语言实验报告模板完成版 <高级语言程序设计> 学 生 实 验 报 告 专业:计算机科学与技术(非师范) 学号:12600120 姓名: 李奕 实验一 C程序的运行环境和使用方法 1. 实 ...

  8. 通用型简单latex模板&表格制作

    转一个很好的模板,说他好是因为他包含了在latex下创建一个文件所需要用到的几乎所有的基本文档设置:页面,正文,字体, 发信人: ceo (神), 信区: MathTools 标 题: 我的通用型简单 ...

  9. 【C语言】- 通讯录实现详解

    今天向大家介绍一个利用C语言实现通讯录的过程. 通讯录相信大家都听说过,存放一个人的信息,用来方便联系.下图为我们即将实现通讯录的功能: 图中已经描述的很清楚了,这个通讯录的容量为1000,所录入的信 ...

  10. 东北大学C语言实验报告,东北大学C语言实验报告模板.doc

    东北大学C语言实验报告模板 C语言程序设计实验报告 实验名称顺序和条件控制语句学 院资源与土木工程学院专业班级采矿1201姓 名学 号任课教师柳秀梅实验时间2013年4月9日 实验目的 熟练掌握顺序结 ...

最新文章

  1. ASP.NET的Cookie跨域问题
  2. vue生成包报错error from UglifyJs
  3. iPhone开发过程中调试多次Release问题 message sent to deallocated
  4. 小波相干wtc matlab,实现时间序列的小波相干性分析,并画出图谱
  5. 面向对象2:类和对象
  6. math: 雅可比矩阵 黑塞矩阵
  7. IDEA告警:Field can be converted to a local varible
  8. python函数之plot函数(一)
  9. 戴尔电脑风扇声音大的解决方法
  10. dell电脑如何安装ubuntu系统_Dell电脑 U盘启动盘 安装ubuntu
  11. 手机的开发者选项怎么找(真机调试)
  12. day02 快速上手
  13. NXP RT1064学习笔记(六)— RTC
  14. ul(有序列表)可以排序?
  15. JS中的NaN和isNaN
  16. 网上购车平台蛋蛋订车上私户,汽车之家青少年嘉年华正式开幕
  17. 【论文笔记】Learning What Not to Segment: A New Perspective on Few-Shot Segmentation
  18. 使用python进行数据预处理--主成分分析
  19. 市场规模到底应该如何测算?
  20. 笔记本更换SSD和光驱硬盘架

热门文章

  1. 编程精华资源(ITeye优秀专栏)大汇总
  2. 关于DoIP 协议的理解
  3. 【Adobe美术基础】字体安装
  4. python脚本操作excel
  5. CleanMyMac X4.11.1中文正式版 系统优化 垃圾清理 程序卸载工具
  6. 在linux中查看服务,linux中怎么查看服务状态
  7. lorawan服务器通信协议,LoRaWAN协议(三)–Server端数据协议
  8. 网络视频传输的服务质量(QoS)
  9. android textwatcher 获取当前控件,TextWatcher如何找到调用它的EditText
  10. 精美绝伦的KShong GHOST Windows7-Pro 2010幸福版