今天是国庆节第二天了,昨天小编奔波了一天终于赶到了家,在这里也祝大家国庆七天玩得开心。 这次给大家讲的程序是一个电话簿,具有基本的信息输入,显示,查询和排序功能,为了程序结构的清晰与后期维护和更新的便利,将把程序分为六个部分。 telbook.h包含所有用到的头文件、全局类型、宏定义和函数原型声明, 定义了电话信息的结构体,用来存储电话号码,机主姓名与地址。 telbook1.c定义函数入口,根据功能菜单输入字符选择相应操作,即链接到各个函数,使用switch-case选择结构。 telbook2.c提供系统功能菜单、电话信息的输入与显示功能。 telbook3.c提供保存和读取电话信息的功能,涉及文件操作。 telbook4.h提供按电话号码或机主姓名进行查询的功能,只需简单遍历即可实现。 telbook5.h提供按电话号码与机主姓名进行排序的功能,本程序中使用冒泡排序法,没有学习过的也可以顺带学习一下。 另外,所有函数的功能、参数的含义以及不常见的标准库函数,小编都在程序中进行了详细的标注。 程序的源代码如下: 一、telbook.h #includestdio.h #includestring.h #includeconio.h #includestdlib.h #includectype.h /*以上给定了所有需要用到的头文件*/ #defineMAX_ITEM /*定义了系统最大容量*/ structphone_item{ charphone[20]; charname[20]; charaddress[50]; }; /*定义电话信息结构体*/ /*所有用户自定义函数的函数原型*/ voidservice_info(); intadd_one_item(structphone_itempe[],intcur_count); voiddisplay_all_info(structphone_itempe[],intcur_count); intsave_info(structphone_itempe[],intcur_count); intload_info(structphone_itempe[]); voidQuery_by_phone(structphone_itempe[],intcur_count); voidQuery_by_name(structphone_itempe[],intcur_count); voidsort_by_phone(structphone_itempe[],intcur_count); voidsort_by_name(structphone_itempe[],intcur_count); 二、telbook1.c #includetelbook.h /*main函数为主控程序,输入数字字符选择相应的操作模块*/ intmain() { charchoice; intcount=0; structphone_itemphone_entry[MAX_ITEM]; while(1){ service_info(); scanf(%c,choice); getchar(); switch(choice){ case1:count=add_one_item(phone_entry,count);/*添加一个电话信息*/ break; case2:save_info(phone_entry,count);/*保存电话信息*/ break; case3:count=load_info(phone_entry);/*读取电话信息*/ break; case4:display_all_info(phone_entry,count);/*显示所有电话信息*/ break; case5:Query_by_phone(phone_entry,count);/*根据电话号码查询*/ break; case6:Query_by_name(phone_entry,count);/*根据姓名查询*/ break; case7:sort_by_phone(phone_entry,count);/*根据电话号码排序*/ break; case8:sort_by_name(phone_entry,count);/*根据姓名排序*/ break; case9:printf(ThanksforusingourYellowPageService!\n);/*退出*/ exit(0); default:break; } printf(PressEntertocontinue...\n); /*使用户能够看到操作的结果信息*/ getch(); } } 三、telbook2.c #includetelbook.h /*add_one_item函数,用于输入电话信息,pe是存储电话信息的结构体的首地址, cur_count是目前系统中存储的电话总数; 返回值:如果由于存储空间不够添加失败,则返回;否则返回了添加了电话信息后, 目前系统中存储的电话的总数。*/ intadd_one_item(structphone_itempe[],intcur_count) { if(cur_count=MAX_ITEM){ printf(diskfull!\n); return0; } printf(Pleaseinputphone_number:); gets_s(pe[cur_count].phone); printf(Pleaseinputname:); gets_s(pe[cur_count].name); printf(Pleaseinputaddress:); gets_s(pe[cur_count].address); return(cur_count+1); } /*service_info函数用于显示系统功能选项信息*/ voidservice_info() { printf(*********************************************\n); printf(*wel北京哪里能治好白癜风北京的最好白癜风医院
|