本篇內(nèi)容介紹了“C語(yǔ)言怎么設(shè)計(jì)簡(jiǎn)易電話簿”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

代碼如下:
#include <stdio.h>#include <stdbool.h>#include <string.h>#include <stdlib.h>#include <getch.h>typedef struct Contact{ //定義聯(lián)系人結(jié)構(gòu)體 char name[20]; //姓名 char sex; //性別 char tel[12]; //電話}Contact;Contact contacts[100];void show_contact(Contact* conp){ //顯示聯(lián)系人信息 printf("姓名:%s\t性別:%s\t電話:%s\n",conp->name,'w'==conp->sex?"女":"男",conp->tel); }void scan_contact(Contact* conp){ //輸入聯(lián)系人信息 printf("請(qǐng)輸入姓名,性別(w:女m:男),電話:"); scanf("%s%s%s",conp->name,&conp->sex,conp->tel);}void add_contacts(void){ //添加聯(lián)系人 for(int i=0;i<100;i++){ if(0 == contacts[i].sex){ scan_contact(contacts+i); printf("添加成功!\n"); return; } } printf("添加人已滿\n");}void del_contacts(void){ //刪除聯(lián)系人 char str[20] = {}; printf("請(qǐng)輸入刪除人姓名:\n"); scanf("%s",str); for(int i=0;i<100;i++){ if(0 == strcmp(str,contacts[i].name)){ contacts[i].sex = 0; printf("刪除聯(lián)系人成功\n"); return; } } printf("聯(lián)系人不存在\n");}void find_contacts(void){ //查找聯(lián)系人 char str[20] = {}; printf("請(qǐng)輸入要查詢的手機(jī)號(hào)\n"); scanf("%s",str); getchar(); for(int i=0;i<100;i++){ if(strstr(contacts[i].tel,str)){ show_contact(contacts+i); } } printf("請(qǐng)輸入任意鍵繼續(xù)...\n"); getch();}void list_contacts(void){ //顯示聯(lián)系人信息 for(int i=0;i<100;i++){ if(contacts[i].sex){ show_contact(contacts+i); } } printf("請(qǐng)輸入任意鍵繼續(xù)...\n"); getch(); } void change_contacts(void){ //修改聯(lián)系人信息 char str[20] = {}; printf("請(qǐng)輸入要修改的聯(lián)系人姓名:\n"); scanf("%s",str); for(int i=0;i<100;i++){ if(0 == strcmp(str,contacts[i].name)){ show_contact(contacts+i); scan_contact(contacts+i); return; } } printf("沒(méi)有找到要修改的聯(lián)系人");}char menu(void){ system("clear"); printf("歡迎使用電話蒲\n"); printf("--------------\n"); printf("1、添加聯(lián)系人 \n"); printf("2、刪除聯(lián)系人\n"); printf("3、修改聯(lián)系人信息\n"); printf("4、查找聯(lián)系人\n"); printf("5、顯示所有聯(lián)系人\n"); printf("--------------\n"); printf("請(qǐng)輸入指令:"); char cmd = getch(); printf("%c\n",cmd); return cmd;}int main(){ while(true){ switch(menu()){ case '1':add_contacts(); break; case '2':del_contacts(); break; case '3':change_contacts(); break; case '4':find_contacts(); break; case '5':list_contacts(); break; //case '6':exit(); break; default: printf("cmd error!\n"); } } }//------------------------------------總結(jié)------------------------------------------//添加與刪除聯(lián)系人的突破口:可以選擇性別的返回值來(lái)實(shí)現(xiàn)添加與刪除。//查找聯(lián)系人 strstr()函數(shù)的作用://strstr(str1,str2) 函數(shù)用于判斷字符串str2是否是str1的子串。如果是,則該函數(shù)返回str2在str1中首次出現(xiàn)的地址;否則,返回NULL。//因此查找聯(lián)系人時(shí)便可只打出電話的一部分就能查找到聯(lián)系人。//該程序的弊端:不能每次打開(kāi)就有之前保存的聯(lián)系人。//優(yōu)化: 可以將聯(lián)系人保存到文件中,并且在程序打開(kāi)的時(shí)候打開(kāi)文件。“C語(yǔ)言怎么設(shè)計(jì)簡(jiǎn)易電話簿”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
網(wǎng)頁(yè)題目:C語(yǔ)言怎么設(shè)計(jì)簡(jiǎn)易電話簿-創(chuàng)新互聯(lián)
本文URL:http://www.chinadenli.net/article24/cccije.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站排名、商城網(wǎng)站、響應(yīng)式網(wǎng)站、標(biāo)簽優(yōu)化、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容