這篇文章主要講解了C語(yǔ)言如何實(shí)現(xiàn)通訊錄管理系統(tǒng),內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
創(chuàng)新互聯(lián)服務(wù)緊隨時(shí)代發(fā)展步伐,進(jìn)行技術(shù)革新和技術(shù)進(jìn)步,經(jīng)過(guò)10余年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設(shè)計(jì)師、專(zhuān)業(yè)的網(wǎng)站實(shí)施團(tuán)隊(duì)以及高素質(zhì)售后服務(wù)人員,并且完全形成了一套成熟的業(yè)務(wù)流程,能夠完全依照客戶(hù)要求對(duì)網(wǎng)站進(jìn)行成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、建設(shè)、維護(hù)、更新和改版,實(shí)現(xiàn)客戶(hù)網(wǎng)站對(duì)外宣傳展示的首要目的,并為客戶(hù)企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。C語(yǔ)言實(shí)現(xiàn)了通訊錄的錄入信息、保存信息、插入、刪除、排序、查找、單個(gè)顯示等功能。。
完整的代碼如下:
#include#include //得到指向大小為Size的內(nèi)存區(qū)域的首字節(jié)的指針// #include #include //標(biāo)準(zhǔn)庫(kù)函數(shù)// #define NULL 0 #define LEN sizeof(struct address_list) //計(jì)算字節(jié)// int n; struct address_list { char name[30]; //名字 char work[30]; //職業(yè) char handset[30]; //手機(jī) char email[30]; //電子郵件 char address[30]; //通訊地址 struct address_list *next; }; struct address_list *shifang(struct address_list *head); // 釋放內(nèi)存函數(shù)聲明 //創(chuàng)建函數(shù),不帶頭結(jié)點(diǎn)的鏈表 struct address_list *creat(void) { struct address_list *head,*p1,*p2; char name[20]; n=0; p1=(struct address_list *)malloc(LEN); p2=p1; //強(qiáng)制內(nèi)存轉(zhuǎn)換 printf("請(qǐng)輸入通訊錄的內(nèi)容!\n姓名輸入為0時(shí)表示創(chuàng)建完畢!\n"); printf("請(qǐng)輸入姓名:"); gets(name); if(strcmp(name,"0")!=0) { strcpy(p1->name,name); printf("請(qǐng)輸入職業(yè):"); gets(p1->work); printf("請(qǐng)輸入手機(jī):"); gets(p1->handset); printf("請(qǐng)輸入電子郵件:"); gets(p1->email); printf("請(qǐng)輸入通訊地址:"); gets(p1->address); head=NULL; while(1) { n=n+1; //記錄通訊錄人數(shù)個(gè)數(shù) if(n==1) head=p1; else p2->next=p1; p2=p1; printf("請(qǐng)輸入姓名:"); gets(name); if(strcmp(name,"0")==0) { break; } else { p1=(struct address_list *)malloc(LEN); strcpy(p1->name,name); printf("請(qǐng)輸入職業(yè):"); gets(p1->work); printf("請(qǐng)輸入手機(jī):"); gets(p1->handset); printf("請(qǐng)輸入電子郵件:"); gets(p1->email); printf("請(qǐng)輸入通訊地址:"); gets(p1->address); } } p2->next=NULL; return head; } else return 0; } //輸出函數(shù) void print(struct address_list *head) { struct address_list *p; if(head!=NULL) { p=head; printf("本通訊錄現(xiàn)在共有%d人:\n",n); printf("---姓名-------職業(yè)--------手機(jī)-------Email-------通訊地址\n"); printf("==================================\n"); do { printf("== %s",p->name); printf(" "); printf("%s",p->work); printf(" "); printf("%s",p->handset); printf(" "); printf("%s",p->email); printf(" "); printf("%s",p->address); printf(" \n"); p=p->next; }while(p!=NULL); printf("==================================\n"); } else printf("通訊錄為空,無(wú)法輸出!\n"); } //增加函數(shù) struct address_list *insert(struct address_list *head) { struct address_list *p0,*p1,*p2; char name[20]; p1=head; printf("請(qǐng)輸入增加的內(nèi)容:\n"); printf("請(qǐng)輸入姓名:"); gets(name); if(strcmp(name,"0")==0) { printf("姓名不能為0,增加失敗!\n"); return(head); } else { p0=(struct address_list *)malloc(LEN); strcpy(p0->name,name); printf("請(qǐng)輸入職業(yè):"); gets(p0->work); printf("請(qǐng)輸入手機(jī):"); gets(p0->handset); printf("請(qǐng)輸入電子郵件:"); gets(p0->email); printf("請(qǐng)輸入通訊地址:"); gets(p0->address); n=n+1; if(head==NULL) { head=p0; p0->next=NULL; return head; } else { while(strcmp(p0->name,p1->name)>0&&(p1->next!=NULL)) { p2=p1; p1=p1->next; } if(strcmp(p0->name,p1->name)<0 || strcmp(p0->name,p1->name)==0) { if(head==p1) { head=p0; } else { p2->next=p0; } p0->next=p1; } else { p1->next=p0; p0->next=NULL; } return head; } } } struct address_list* delete_txl(struct address_list *head) { struct address_list *p,*q; char name[30]; if(head==NULL) { printf("通訊錄為空,無(wú)法顯示!\n"); return head; } p=head; printf("請(qǐng)輸入需要?jiǎng)h除的人的姓名:"); gets(name); if(strcmp(head->name,name)==0) { head=head->next; free(p); printf("刪除操作成功!\n"); return head; } else { q=head,p=head->next; while(p!=NULL) { if(strcmp(p->name,name)==0) { q->next=p->next; free(p); printf("刪除操作成功!\n"); return head; } p=p->next; q=q->next; } } } //顯示函數(shù) struct address_list *display(struct address_list *head) { struct address_list *p1,*p2; char name[30]; int m; if(head==NULL) { printf("通訊錄為空,無(wú)法顯示!\n"); return head; } p1=head; m=0; printf("請(qǐng)輸入需要顯示人的姓名:"); gets(name); while(p1!=NULL) { while((strcmp(p1->name,name))!=0 && p1->next!=NULL) { p2=p1; p1=p1->next; } if(strcmp(p1->name,name)==0) { m++; printf("%s的通訊內(nèi)容如下:\n",name); printf("---姓名--------職業(yè)--------手機(jī)-------Email------通訊地址\n"); printf("==================================\n"); printf("== %s",p1->name);printf(" "); printf("%s",p1->work);printf(" "); printf("%s",p1->handset);printf(" "); printf("%s",p1->email);printf(" "); printf("%s",p1->address); printf(" \n"); printf("==================================\n"); } p1=p1->next; } if(m==0) { printf("此人未在本通訊錄中!\n"); } return(head); } //排序函數(shù) struct address_list *paixu(struct address_list *head) { struct address_list *p1,*p2; int i,j; struct address_list1 { char name[30]; char work[30]; char handset[30]; char email[30]; char address[30]; }; struct address_list1 px[200]; struct address_list1 temp; if(head==NULL) { printf("通訊錄為空,無(wú)法排序!\n"); return(head); } p1=head; for(i=0;i name); strcpy(px[i].work,p1->work); strcpy(px[i].handset,p1->handset); strcpy(px[i].email,p1->email); strcpy(px[i].address,p1->address); p2=p1; p1=p1->next; } head=shifang(head); for(j=0;j name,px[0].name); strcpy(p1->work,px[0].work); strcpy(p1->handset,px[0].handset); strcpy(p1->email,px[0].email); strcpy(p1->address,px[0].address); head=p1; for(i=1;i name,px[i].name); strcpy(p1->work,px[i].work); strcpy(p1->handset,px[i].handset); strcpy(p1->email,px[i].email); strcpy(p1->address,px[i].address); p2->next=p1; p2=p1; } p2->next=NULL; printf("按姓名排序后為:\n"); print(head); return(head); } //姓名查找函數(shù) struct address_list *search(struct address_list *head) { struct address_list *p1,*p2; int m; char name[30]; if(head==NULL) { printf("通訊錄為空,無(wú)法分類(lèi)查找!\n"); return(head); } p1=head; printf("********************\n"); printf("** 請(qǐng)輸入需要查找的姓名 **\n"); printf("********************\n"); m=0; gets(name); while(p1!=NULL) { while(strcmp(p1->name,name)!=0&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(strcmp(p1->name,name)==0) { m++; printf("你查找的內(nèi)容是:\n"); printf("+++++++++++++++++++++++++++++++++++\n"); printf("++ %s %s %s %s %s\n",p1->name,p1->work,p1->handset,p1->email,p1->address); printf("+++++++++++++++++++++++++++++++++++\n"); } p1=p1->next; if(m==0) { printf("此人未在本通訊錄中!\n"); } break; } return(head); } //釋放內(nèi)存函數(shù) struct address_list *shifang(struct address_list *head) { struct address_list *p1; while(head!=NULL) { p1=head; head=head->next; free(p1); } return(head); } //文件寫(xiě)入函數(shù) void save(struct address_list *head) { FILE *fp; struct address_list *p1; char tong[30]; if(head==NULL) { printf("通訊錄為空,無(wú)法存儲(chǔ)!\n"); return; } printf("請(qǐng)輸入保存后的文件名:"); gets(tong); fp=fopen("(tong).txt","w"); if(fp==NULL) { printf("cannot open file\n"); return; } p1=head; fprintf(fp,"姓名 職業(yè) 手機(jī) Email 通訊地址\n"); for(;p1!=NULL;) { fprintf(fp,"%s %s %s %s %s\n",p1->name,p1->work,p1->handset,p1->email,p1->address); p1=p1->next; } printf("保存完畢!\n"); fclose(fp); } //文件讀出函數(shù) struct address_list *load(struct address_list *head) { FILE *fp; char tong[30]; struct address_list *p1,*p2; printf("請(qǐng)輸入要輸出的文件名:"); gets(tong); fp=fopen("(tong).txt","r"); if(fp==NULL) { printf("此通訊錄名不存在,無(wú)法輸出!\n"); return(head); } else { head=shifang(head); } p1=(struct address_list *)malloc(LEN); fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address); if(feof(fp)!=0) { printf("文件為空,無(wú)法打開(kāi)!\n"); return(head); } else { rewind(fp); p2=p1; head=p1; n=0; while(feof(fp)==0) { fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p1->address); if(feof(fp)!=0) break; p2->next=p1; p2=p1; p1=(struct address_list *)malloc(LEN); n=n+1; } p2->next=NULL; p1=head; head=head->next; n=n-1; free(p1); print(head); printf("打開(kāi)完畢!\n"); return(head); } fclose(fp); } //綜合操作函數(shù) struct address_list *menu(struct address_list *head) { char num[10]; while(1) { printf("*********************\n"); printf("*** 1 姓名查找 ****\n"); printf("*** 2 單個(gè)顯示 ****\n"); printf("*** 3 增加 ****\n"); printf("*** 4 退出 ****\n"); printf("*********************\n"); printf("請(qǐng)輸入您選擇的操作:"); gets(num); switch(*num) { case '1': { head=search(head); //姓名查找 print(head); } break; case '2': { head=display(head); //顯示 } break; case '3': { head=insert(head); //增加 print(head); } break; case '4': return head; default: printf("操作錯(cuò)誤,此項(xiàng)不存在!\n"); break; } if(strcmp(num,"6")==0) break; } return head; } //主函數(shù) void main() { struct address_list *head=NULL; char num[10]; printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n"); printf("*=* 程序說(shuō)明 *=*\n"); printf("*=* 請(qǐng)及時(shí)保存創(chuàng)建完畢的通訊錄內(nèi)容! *=*\n"); printf("*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n"); while(1) { printf("************************\n"); printf("*** 1 創(chuàng)建通訊錄 ****\n"); printf("*** 2 按名字排序 ****\n"); printf("*** 3 綜合操作 ****\n"); printf("*** 4 保存 ****\n"); printf("*** 5 打開(kāi) ****\n"); printf("*** 6 刪除 ****\n"); printf("*** 7 退出 ****\n"); printf("************************\n"); printf("請(qǐng)輸入您選擇的操作:"); gets(num); switch(*num) { case '1': { if(head==NULL) { head=creat(); //創(chuàng)建 print(head); } else { head=shifang(head); head=creat(); //重新創(chuàng)建 print(head); } } break; case '2': { head=paixu(head); //排序 } break; case '3': { head=menu(head); //綜合操作 } break; case '4': { save(head); //文件保存 print(head); } break; case '5': { head=load(head); //文件輸出 } break; case '6': { head=delete_txl(head); //刪除 print(head); } break; case '7': head=shifang(head); break; default: printf("操作錯(cuò)誤,此項(xiàng)不存在!\n"); break; } if(strcmp(num,"7")==0) break; } }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。