C語言開發(fā)實現(xiàn)通訊錄管理系統(tǒng)
本文實例為大家分享了C語言通訊錄管理系統(tǒng)開發(fā)的具體代碼,供大家參考,具體內(nèi)容如下
程序介紹
通訊錄管理系統(tǒng)主要是實現(xiàn)對聯(lián)系人的增、刪、查以及顯示的基本操作。用戶可以根據(jù)自己的需要在功能菜單中選擇相應(yīng)的操作,實現(xiàn)對聯(lián)系人的快速管理。
操作流程
用戶在編譯完成后會產(chǎn)生一個系統(tǒng)的可執(zhí)行文件,用戶只要雙擊可執(zhí)行文件就可以進入系統(tǒng),進入系統(tǒng)的功能選擇菜單,如圖所示,用戶根據(jù)自己的需要選擇相應(yīng)的操作。
代碼
#include<stdio.h> #include<stdlib.h> #include<dos.h> #include <conio.h> #include<string.h> struct Info { ?? ?char name[15];/*姓名*/ ?? ?char city[10];/*城市*/ ?? ?char province[10];/*省*/ ?? ?char state[10];/*國家*/ ?? ?char tel[15];/*電話*/ }; typedef struct node/*定義通訊錄鏈表的結(jié)點結(jié)構(gòu)*/ { ?? ?struct Info data; ?? ?struct node *next; }Node,*link; void stringinput(char *t,int lens,char *notice) { ? ?char n[50]; ? ?do{ ? ? ? printf("%s",notice); /*顯示提示信息*/ ? ? ? scanf("%s",&n); /*輸入字符串*/ ? ? ? if(strlen(n)>lens) ?? ? ??? ?printf("\n exceed the required length! \n"); /*超過lens值重新輸入*/ ? ? ?}while(strlen(n)>lens); ? ?strcpy(t,n); /*將輸入的字符串拷貝到字符串t中*/ } void enter(link l)/*輸入記錄*/ { ?? ?Node *p,*q; ?? ?q=l; ?? ?while(1) ?? ?{ ?? ??? ?p=(Node*)malloc(sizeof(Node));/*申請結(jié)點空間*/ ?? ??? ?if(!p)/*未申請成功輸出提示信息*/ ?? ??? ?{ ?? ??? ??? ?printf("memory malloc fail\n"); ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?stringinput(p->data.name,15,"enter name:");/*輸入姓名*/ ?? ??? ?if(strcmp(p->data.name,"0")==0)/*檢測輸入的姓名是否為0*/ ?? ??? ??? ?break; ?? ??? ?stringinput(p->data.city,10,"enter city:");/*輸入城市*/ ?? ??? ?stringinput(p->data.province,10,"enter province:");/*輸入省*/ ?? ??? ?stringinput(p->data.state,10,"enter status:");/*輸入國家*/ ?? ??? ?stringinput(p->data.tel,15,"enter telephone:");/*輸入電話號碼*/ ?? ??? ?p->next=NULL; ?? ??? ?q->next=p; ?? ??? ?q=p; ?? ?} } void del(link l) { ?? ?Node *p,*q; ?? ?char s[20]; ?? ?q=l; ?? ?p=q->next; ?? ?printf("enter name:"); ?? ?scanf("%s",s);/*輸入要刪除的姓名*/ ?? ?while(p) ?? ?{ ?? ??? ?if(strcmp(s,p->data.name)==0)/*查找記錄中與輸入名字匹配的記錄*/ ?? ??? ?{ ?? ??? ??? ?q->next=p->next;/*刪除p結(jié)點*/ ?? ??? ??? ?free(p);/*將p結(jié)點空間釋放*/ ?? ??? ??? ?printf("delete successfully!"); ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?q=p; ?? ??? ??? ?p=q->next; ?? ??? ?} ?? ?} ?? ?getch(); } void display(Node *p) { ?? ?printf("MESSAGE \n"); ?? ?printf("name:%15s\n",p->data.name); ?? ?printf("city: ? ?%10s\n",p->data.city); ?? ?printf("province:%10s\n",p->data.province); ?? ?printf("state: ? %10s\n",p->data.state); ?? ?printf("telphone:%15s\n",p->data.tel); ?? ? } void search(link l) { ?? ?char name[20]; ?? ?Node *p; ?? ?p=l->next; ?? ?printf("enter name to find:"); ?? ?scanf("%s",name);/*輸入要查找的名字*/ ?? ?while(p) ?? ?{ ?? ??? ?if(strcmp(p->data.name,name)==0)/*查找與輸入的名字相匹配的記錄*/ ?? ??? ?{ ?? ??? ??? ?display(p);/*調(diào)用函數(shù)顯示信息*/ ?? ??? ??? ?getch(); ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?else ?? ??? ?p=p->next; ?? ?} } void list(link l) { ?? ?Node *p; ?? ?p=l->next; ?? ?while(p!=NULL)/*從首節(jié)點一直遍歷到鏈表最后*/ ?? ?{ ?? ??? ?display(p); ?? ??? ?p=p->next; ?? ?} ?? ?getch(); } void save(link l) { ?? ?Node *p; ?? ?FILE *fp; ?? ?p=l->next; ?? ?if((fp=fopen("f:\\adresslist","wb"))==NULL) ?? ?{ ?? ??? ?printf("can not open file\n"); ?? ??? ?exit(1); ?? ?} ?? ?printf("\nSaving file\n"); ?? ?while(p)/*將節(jié)點內(nèi)容逐個寫入磁盤文件中*/ ?? ?{ ?? ??? ?fwrite(p,sizeof(Node),1,fp); ?? ??? ?p=p->next; ?? ?} ?? ?fclose(fp); ?? ?getch(); } void load(link l) { ?? ?Node *p,*r; ?? ?FILE *fp; ?? ?l->next=NULL; ?? ?r=l; ?? ?if((fp=fopen("f:\\adresslist","rb"))==NULL) ?? ?{ ?? ??? ?printf("can not open file\n"); ?? ??? ?exit(1); ?? ?}; ?? ?printf("\nLoading file\n"); ?? ?while(!feof(fp)) ?? ?{ ?? ??? ?p=(Node*)malloc(sizeof(Node));/*申請節(jié)點空間*/ ?? ??? ?if(!p) ?? ??? ?{ ?? ??? ??? ?printf("memory malloc fail!"); ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?if(fread(p,sizeof(Node),1,fp)!=1)/*讀記錄到節(jié)點p中*/ ?? ??? ?break; ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?p->next=NULL; ?? ??? ??? ?r->next=p;/*插入鏈表中*/ ?? ??? ??? ?r=p; ?? ??? ?} ?? ?} ?? ?fclose(fp); ?? ?getch(); } int menu_select() { ?? ?int i; ?? ?printf("\n\n\t *************************ADDRESS LIST*************************\n"); ?? ?printf("\t|*?? ??? ??? ?1.input record?? ??? ??? ? ? ? ?*|\n"); ?? ?printf("\t|*?? ??? ??? ?2.delete record?? ??? ??? ? ? ? ?*|\n"); ?? ?printf("\t|*?? ??? ??? ?3.list record?? ??? ??? ? ? ? ?*|\n"); ?? ?printf("\t|*?? ??? ??? ?4.search record?? ??? ??? ? ? ? ?*|\n"); ?? ?printf("\t|*?? ??? ??? ?5.save record?? ??? ??? ? ? ? ?*|\n"); ?? ?printf("\t|*?? ??? ??? ?6.load record?? ??? ??? ? ? ? ?*|\n"); ?? ?printf("\t|*?? ??? ??? ?7.Quit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?*|\n"); ?? ?printf("\t **************************************************************\n"); ?? ?do ?? ?{ ?? ??? ?printf("\n\tEnter your choice:"); ?? ??? ?scanf("%d",&i); ?? ?}while(i<0||i>7); ?? ?return i; } main() { ?? ?link l; ?? ?l=(Node*)malloc(sizeof(Node)); ?? ?if(!l) ?? ?{ ?? ??? ?printf("\n allocate memory failure "); /*如沒有申請到,輸出提示信息*/ ?? ? ? ?return 0; ? ? ? ? ? ? /*返回主界面*/ ?? ?} ?? ?l->next=NULL; ?? ?system("cls"); ?? ?while(1) ?? ?{ ?? ??? ?system("cls"); ?? ??? ?switch(menu_select()) ?? ??? ?{ ?? ??? ??? ?case 1: ?? ??? ??? ??? ?enter(l); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 2: ?? ??? ??? ??? ?del(l); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 3: ?? ??? ??? ??? ?list(l); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 4: ?? ??? ??? ??? ?search(l); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 5: ?? ??? ??? ??? ?save(l); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 6: ?? ??? ??? ??? ?load(l); ?? ??? ??? ??? ?break; ?? ??? ??? ?case 7: ?? ??? ??? ??? ?exit(0); ?? ??? ?} ?? ?} }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Visual?Studio下Eigen庫環(huán)境配置方式
這篇文章主要介紹了Visual?Studio下Eigen庫環(huán)境配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12C語言 動態(tài)內(nèi)存開辟常見問題解決與分析流程
動態(tài)內(nèi)存是相對靜態(tài)內(nèi)存而言的。所謂動態(tài)和靜態(tài)就是指內(nèi)存的分配方式。動態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存2022-03-03