您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“C語言如何實現(xiàn)通訊錄系統(tǒng)程序”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當(dāng),希望這篇“C語言如何實現(xiàn)通訊錄系統(tǒng)程序”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
利用鏈表增、刪、改、查功能以及文件來完成通訊錄系統(tǒng)。通訊錄中包含聯(lián)系人的基本信息:姓名、聯(lián)系電話、家庭住址以及電子郵件。
以下是設(shè)計該系統(tǒng)的步驟:
1.導(dǎo)出通訊錄系統(tǒng)的功能:
(構(gòu)建一個通訊錄結(jié)構(gòu)體)
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> /*定義通訊錄結(jié)構(gòu)體*/ typedef struct record { char name[30]; char phonenumber[11]; char address[3][30]; //定義一維 0-省 1-市 2-街道 char email[6]; struct record *next; } record;
聲明用到的函數(shù),不妨設(shè)置個密碼,并設(shè)計出通訊錄功能的主頁面
void mainmenu();//聲明通訊錄主菜單 void alterstring(record *head); void browsemenu(record *head, const int *total);//瀏覽通訊錄主菜單 record *newrecord(record *head, int *total);//添加聯(lián)系人信息 record *deleterecord(record *head,int *total);//刪除聯(lián)系人信息 record *modifyrecord(record *head);//修改聯(lián)系人信息 record *searchrecord(record *head, int onlyonerecord) ;//查找聯(lián)系人信息 record *importrecords(record *head, int *total);//導(dǎo)入聯(lián)系人信息 record *exportrecords(record *head);//導(dǎo)出聯(lián)系人信息 /*定義主函數(shù)執(zhí)行通訊錄功能*/ int main() { char mima[10]={0}; int i=0; printf("請輸入密碼:\n"); for(i=0;i<3;i++) { scanf("%s",mima); if(strcmp(mima,"123456")==0) { printf("登錄成功,親愛的小豬崽!\n"); break; } else { printf("密碼錯誤,笨熊!請重新輸入密碼:\n"); } } if(3==i) { printf("登錄失敗!老笨熊?。?!\n"); exit(1); } system("pause"); int total = 0, choice; record *head = NULL; printf("\n\t\t\t 歡迎使用通訊錄系統(tǒng)!\n"); printf("\n\t\t\t********************************************\n"); do { mainmenu(); scanf("%d", &choice); system("cls"); switch (choice) { case 0: break; case 1: browsemenu(head, &total); break; case 2: head = newrecord(head, &total); break; case 3: head = deleterecord(head,&total); break; case 4: head = modifyrecord(head); break; case 5: searchrecord(head,0); break; case 6: head = importrecords(head,&total); break; case 7: exportrecords(head); break; default: printf("\n\n**對不起,輸入錯誤,請輸入0~7?。?!\n"); }; } while (choice != 0); return 0; } /*通訊錄界面*/ void mainmenu() { printf("\n"); printf("\n\t\t\t****************1.瀏覽通訊錄****************\n"); printf("\n\t\t\t**************2.增加聯(lián)系人信息**************\n"); printf("\n\t\t\t**************3.刪除聯(lián)系人信息**************\n"); printf("\n\t\t\t**************4.修改聯(lián)系人信息**************\n"); printf("\n\t\t\t**************5.查找聯(lián)系人信息**************\n"); printf("\n\t\t\t*************6.從文件中導(dǎo)入記錄*************\n"); printf("\n\t\t\t*************7.從記錄導(dǎo)出到文件*************\n"); printf("\n\t\t\t********************0.退出******************\n"); printf("\n\t\t\t********************************************\n"); printf("\n\t\t\t請輸入0~7選擇功能 :"); }
以下為本程序亮點,就是所謂的增、刪、改、查小牛功能:
/*定義鏈表首地址,遇到回車鍵跳轉(zhuǎn)下一個成員*/ void alterstring(record *head) { int m; record *p1 = head; while (p1 != NULL) { for (m = 0; m < 30; m++) { if (*((p1->name) + m) == '\n') { *((p1->name) + m) = '\0'; } } for (m = 0; m < 11; m++) { if (*((p1->phonenumber) + m) == '\n') { *((p1->phonenumber) + m) = '\0'; } } for (m = 0; m < 30; m++) { if (*((p1->address[0]) + m) == '\n') { *((p1->address[0]) + m) = '\0'; } } for (m = 0; m < 30; m++) { if (*((p1->address[1]) + m) == '\n') { *((p1->address[1]) + m) = '\0'; } } for (m = 0; m < 30; m++) { if (*((p1->address[2]) + m) == '\n') { *((p1->address[2]) + m) = '\0'; } } for (m = 0; m < 6; m++) { if (*((p1->email) + m) == '\n') { *((p1->email) + m) = '\0'; } } p1 = p1->next; } } /*添加聯(lián)系人信息*/ record *newrecord(record *head, int *total) //鏈表首地址,總數(shù)地址 { int i = *total; char inputchar; record *p = head, *input = (record *) malloc(sizeof(record)); printf("\n**請輸入聯(lián)系人信息\n"); /*如果已經(jīng)有聯(lián)系人信息,則輸出現(xiàn)有的所有聯(lián)系人信息 */ if (*total) { printf("**共有 %d 個聯(lián)系人信息\n\n", *total); } do { //輸入聯(lián)系人信息 printf("請輸入第%d個聯(lián)系人的名字:", i + 1); fflush(stdin);//清理標(biāo)準(zhǔn)輸入流,把多余未被保存的數(shù)據(jù)丟掉 fgets(input->name, 31, stdin);//輸入長度為31的字符串 printf("請輸入第%d個聯(lián)系人的聯(lián)系方式:", i + 1); fflush(stdin); fgets(input->phonenumber,31, stdin); printf("請輸入第%d個聯(lián)系人的家庭地址:\n", i + 1); printf("*請輸入第%d個聯(lián)系人所在省份:", i + 1); fflush(stdin); fgets(input->address[0], 31, stdin); printf("*請輸入第%d個聯(lián)系人所在城市:", i + 1); fflush(stdin); fgets(input->address[1], 31, stdin); printf("*請輸入第%d個聯(lián)系人所在街道:", i + 1); fflush(stdin); fgets(input->address[2], 31, stdin); printf("請輸入第%d個聯(lián)系人的電子郵件:", i + 1); fflush(stdin); fgets(input->email, 7, stdin); input->next = NULL; //插入時放至鏈表的最后 //插入數(shù)據(jù),分為首數(shù)據(jù)和非首數(shù)據(jù) if (head == NULL) { head = input; p = input; } else { while (p->next != NULL) { p = p->next; } p->next = input; } (*total)++;//計數(shù)-聯(lián)系人的總?cè)藬?shù) printf("\n**是否繼續(xù)?(Y/N):"); scanf(" %c", &inputchar); /*如果用getchar只能輸入大寫Y才可以繼續(xù)*/ if (inputchar=='Y' || inputchar=='y') { input = (record *) malloc(sizeof(record));//創(chuàng)建新的空間 i++; } else { break; } } while (1); //按回車鍵跳轉(zhuǎn) alterstring(head); return head; } /*瀏覽通訊錄主菜單*/ //打印全部聯(lián)系人信息 void browsemenu(record *head, const int *total) { int page = 1, firstindex = 0, i, pageamount = *total / 10 + 1;//定義聯(lián)系人為一頁 record *p = head; do { system("cls"); /*輸入頁面的頁數(shù),不能過大或過小*/ if (page > pageamount) { printf("**對不起,頁數(shù)的最大值為%d\n", pageamount); } else if (page < 0) { printf("**對不起,輸入的數(shù)字必須為正數(shù)\n"); } else { //處理分頁,十個聯(lián)系人一頁 firstindex = 10 * (page - 1); printf("NO.\t姓名\t聯(lián)系電話\t省\t市\(zhòng)t街道\t電子郵件\t\n"); //處理前置數(shù)據(jù) p = head; for (i = 0; i < firstindex; ++i) { p = p->next; } i = 0; //輸出數(shù)據(jù) while (p!=NULL && i<10) { i++; printf("NO.%d\t%s\t%s\t\t%s\t%s\t%s\t%s\t\n", i+firstindex,p->name, p->phonenumber, p->address[0], p->address[1], p->address[2], p->email); p = p->next; } printf("** Page %d (共 %d 頁)\n ", page, pageamount); } printf("** 請輸入跳轉(zhuǎn)頁面(按0返回通訊錄主菜單):"); scanf("%d", &page); } while (page); } /*刪除聯(lián)系人信息*/ record *deleterecord(record *head,int *total) { record *p1 = head, *p2,*searchrestlt; searchrestlt = searchrecord(head, 1); while (p1 != NULL && p1 != searchrestlt) { p2 = p1; //p2的上一個節(jié)點 p1 = p1->next; //p1的下一個節(jié)點 } if (p1 == head) { head = p1->next; free(p1); (*total)--; printf("\n**刪除成功!\n"); } else if (p1 != NULL) { p2->next = p1->next; free(p1); (*total)--; printf("\n* *刪除成功!\n"); } else { printf("\n**對不起,沒有找到該聯(lián)系人!\n"); } return head; } //輸出聯(lián)系人信息 void printonerecord(record *p) { printf("姓名:%s\t聯(lián)系電話:%s\t省:%s\t市:%s\t街道::%s\t電子郵件:%s\t\n", p->name, p->phonenumber, p->address[0], p->address[1], p->address[2], p->email); } /*修改聯(lián)系人信息*/ record *modifyrecord(record *head) { record *p1 = head, *p2,*searchrestlt,*input = (record *) malloc(sizeof(record)); //返回需要修改的數(shù)組地址 searchrestlt = searchrecord(head, 1); if (!searchrestlt) { return head; } //輸入聯(lián)系人信息 printf("\n請輸入修改的聯(lián)系人姓名:"); fflush(stdin); fgets(input->name, 30 + 1, stdin); printf("請輸入修改的聯(lián)系人的聯(lián)系電話:"); fflush(stdin); fgets(input->phonenumber,30 + 1, stdin); printf("請輸入修改的聯(lián)系人的地址:\n"); printf("請輸入修改的聯(lián)系人的省份:"); fflush(stdin); fgets(input->address[0], 30 + 1, stdin); printf("請輸入修改的聯(lián)系人的城市:"); fflush(stdin); fgets(input->address[1], 30 + 1, stdin); printf("請輸入修改的聯(lián)系人的街道:"); fflush(stdin); fgets(input->address[2], 30 + 1, stdin); printf("請輸入修改的聯(lián)系人的電子郵件:"); fflush(stdin); fgets(input->email, 7, stdin); //插入時放于鏈表的最后 input->next = NULL; while (p1 != NULL && p1 != searchrestlt) { p2 = p1; //p2上一個節(jié)點 p1 = p1->next; //p1下一個節(jié)點 } if (p1 == head) { head = input; input->next = p1->next; free(p1); printf("\n**修改成功!\n"); } else if (p1 != NULL) { p2->next = input; input->next = p1->next; free(p1); printf("\n**修改成功!\n"); } else { printf("\n-- Do not find this id!\n"); } alterstring(head); return head; } /*查找聯(lián)系人信息*/ record *searchrecord(record *head, int onlyonerecord) { int amount = 0, i = 0, choice = 0; //i,p1循環(huán)變量 char input[30]; record *p1 = head, *results[100] = {NULL}; //result是record類型的指針數(shù)組 printf("\n查找聯(lián)系人:"); setbuf(stdin, NULL);//關(guān)閉輸入緩沖區(qū) fgets(input, 30 + 1, stdin); for (i = 0; i < 30; ++i) { if (*((input) + i) == '\n') { *((input) + i) = '\0'; } } //遍歷搜索 while (p1 != NULL) { if (strstr(p1->name, input) || //strstr()判斷是否為子串 strstr(p1->phonenumber, input) || strstr(p1->address[0], input) || strstr(p1->address[1], input) || strstr(p1->address[2], input) || strstr(p1->email, input)) { results[amount] = p1; amount++; } p1 = p1->next; } //若有同名同信息,根據(jù)編號選擇聯(lián)系人 if (amount > 1) { printf("\n查找結(jié)果:\n"); for (i = 0; i < amount; i++) { printf("NO.%d\t", i + 1); printonerecord(results[i]); } if (!onlyonerecord) { return NULL; //如果不需要去重,則返回NULL } printf("\n**請輸入你要刪除的聯(lián)系人編號: "); scanf("%d", &choice); //若輸入聯(lián)系人編號不正確,默認(rèn)刪除第一位聯(lián)系人 if (choice-1>amount || choice<0) { printf("\n**輸入錯誤(默認(rèn)刪除第一位聯(lián)系人)"); return results[0]; } return results[choice - 1]; } else if (!amount) { printf("\n**對不起,沒有找到該聯(lián)系人!"); return NULL; } else { printf("\n** 查找結(jié)果:\n"); printonerecord(results[0]); return results[0]; } }
最后保存至文件,以便下次查看。
/*將數(shù)據(jù)信息導(dǎo)入文件*/ record *importrecords(record *head, int *total) { int i = *total,m=3; FILE *fpRead; record *p = head, *input; fpRead = fopen("stu.txt","r"); if(fpRead==NULL) { printf("can't open file\n"); return 0; } do { //輸入數(shù)據(jù)(聯(lián)系人信息) input = (record *) malloc(sizeof(record)); fread(input, sizeof(struct record),1,fpRead); input->next = NULL; //插入時放于鏈表的最后 //插入數(shù)據(jù),分為首數(shù)據(jù)和非首數(shù)據(jù) if (head == NULL) { head = input; p = input; } else { while (p->next != NULL) { p = p->next; } p->next = input; } (*total)++;//計數(shù) m--; } while (m); return head; } record *exportrecords(record *head) { FILE *fp; struct record *p=head; if((fp=fopen("stu.txt","wb"))==NULL) { printf("Fail to open the stu.txt!\n"); exit(0); } while(p != NULL) { fwrite(p, sizeof(struct record),1,fp); p=p->next; } fclose(fp); printf("您已成功保存!\n"); return 0; }
讀到這里,這篇“C語言如何實現(xiàn)通訊錄系統(tǒng)程序”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。