您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)電話簿的具體代碼,供大家參考,具體內(nèi)容如下
#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)文件。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。