C语言简单通讯录模板
生活随笔
收集整理的這篇文章主要介紹了
C语言简单通讯录模板
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
頭文件:
#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语言简单通讯录模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab生成exe-在没有安装mat
- 下一篇: Python time localtim