这次给大家讲的程序是一个电话簿,具有基本的信息输入,显示,查询和排序功能,为了程序结构的清晰与后期维护和更新的便利,将把程序分为六个部分。 telbook.h包含所有用到的头文件、全局类型、宏定义和函数原型声明, 定义了电话信息的结构体,用来存储电话号码,机主姓名与地址。 telbook1.c定义函数入口,根据功能菜单输入字符选择相应操作,即链接到各个函数,使用switch-case选择结构。 telbook2.c提供系统功能菜单、电话信息的输入与显示功能。 telbook3.c提供保存和读取电话信息的功能,涉及文件操作。 telbook4.h提供按电话号码或机主姓名进行查询的功能,只需简单遍历即可实现。 telbook5.h提供按电话号码与机主姓名进行排序的功能,本程序中使用冒泡排序法,没有学习过的也可以顺带学习一下。 另外,所有函数的功能、参数的含义以及不常见的标准库函数,小编都在程序中进行了详细的标注。 程序的源代码如下: 一、telbook.h #include #include #include #include #include /*以上给定了所有需要用到的头文件*/ #define MAX_ITEM 700 /*定义了系统最大容量*/ struct phone_item { char phone[20]; char name[20]; char address[50]; }; /*定义电话信息结构体*/ /*所有用户自定义函数的函数原型*/ void service_info(); int add_one_item(struct phone_item pe[], int cur_count); void display_all_info(struct phone_item pe[], int cur_count); int save_info(struct phone_item pe[], int cur_count); int load_info(struct phone_item pe[]); void Query_by_phone(struct phone_item pe[], int cur_count); void Query_by_name(struct phone_item pe[], int cur_count); void sort_by_phone(struct phone_item pe[], int cur_count); void sort_by_name(struct phone_item pe[], int cur_count); 二、telbook1.c #include 'telbook.h' /*main函数为主控程序,输入数字字符选择相应的操作模块*/ int main() { char choice; int count = 0; struct phone_item phone_entry[MAX_ITEM]; while (1) { service_info(); scanf('%c', &choice); getchar(); switch (choice) { case '1':count = add_one_item(phone_entry, count);/*添加一个电话信息*/ break; case '2':save_info(phone_entry, count);/*保存电话信息*/ break; case '3':count = load_info(phone_entry);/*读取电话信息*/ break; case '4':display_all_info(phone_entry, count);/*显示所有电话信息*/ break; case '5':Query_by_phone(phone_entry, count);/*根据电话号码查询*/ break; case '6':Query_by_name(phone_entry, count);/*根据姓名查询*/ break; case '7':sort_by_phone(phone_entry, count);/*根据电话号码排序*/ break; case '8':sort_by_name(phone_entry, count);/*根据姓名排序*/ break; case '9':printf('Thanks for using our YellowPage Service!\n');/*退出*/ exit(0); default:break; } printf('Press Enter to continue...\n'); /*使用户能够看到操作的结果信息*/ getch(); } } 三、telbook2.c #include'telbook.h' /*add_one_item函数,用于输入电话信息,pe是存储电话信息的结构体的首地址, cur_count是目前系统中存储的电话总数; 返回值:如果由于存储空间不够添加失败,则返回;否则返回了添加了电话信息后, 目前系统中存储的电话的总数。*/ int add_one_item(struct phone_item pe[], int cur_count) { if (cur_count >= MAX_ITEM) { printf('disk full!\n'); return 0; } printf('Please input phone_number:'); gets_s(pe[cur_count].phone); printf('Please input name:'); gets_s(pe[cur_count].name); printf('Please input address:'); gets_s(pe[cur_count].address); return (cur_count + 1); } /*service_info函数用于显示系统功能选项信息*/ void service_info() { printf('*********************************************\n'); printf('* welcome to YellowPage Service *\n'); printf('* 1.Add an phone entry. *\n'); printf('* 2.Save all phone info. *\n'); printf('* 3.Load phone info. *\n'); printf('* 4.Display all phone info. *\n'); printf('* 5.Query by phone number. *\n'); printf('* 6.Query by name. *\n'); printf('* 7.Sort all phone info by phone number*\n'); printf('* 8.Sort all phone info by name. *\n'); printf('* 9.Quit. *\n'); printf('*********************************************\n'); } /*display_all_info函数,用于显示所有电话信息项, 每行显示一个电话信息。*/ void display_all_info(struct phone_item pe[], int cur_count) { int i; printf('----------------------------------------------\n'); printf('%13s%20s%30s\n', 'phone number', 'owner name', 'owner address'); for (i = 0; i < cur_count;=""> printf('%13s%20s%30s\n', pe[i].phone, pe[i].name, pe[i].address); printf('----------------------------------------------\n'); } 四、telbook3.c #include'telbook.h' /*save_info函数用于将当前的信息保存到文件telbook.dat中。 参数pe和cur_count给定了当前的电话信息; 返回值:如果保存失败返回0;如果保存成功则返回当前电话信息项的个数*/ int save_info(struct phone_item pe[], int cur_count) { FILE *fp; char a; printf('Are you sure to save,this will overwrite exist information!\n'); printf('Yes(Y) or No(N)?\n'); scanf('%c', &a); if (toupper(a) != 'Y')/*toupper函数将小写字母转化为大写字母*/ return 0; fp = fopen('.\\telbook.dat', 'wb'); if (fp == NULL) { printf('Can't creat telbook.dat\n'); return 0; } if (fwrite(pe, sizeof(struct phone_item), cur_count, fp) != cur_count) { printf('save failed!\n'); return 0; } else { printf('All info saved successfully!\n'); return cur_count; } } /*load_info函数用于将保存在文件telbook.dat中的电话信息读取到内存中。 参数pe给定了保存电话信息的结构体数组的首地址; 返回值:如果读取失败,返回0;如果读取成功,则返回当前电话信息项的个数。*/ int load_info(struct phone_item pe[]) { FILE *fp; int count = 0; fp = fopen('.\\telbook.dat', 'rb'); if (fp == NULL) { printf('Cnt't open telbook.dat!\n'); return 0; } /*feof函数检测文件是否结束; fread函数原型:int fread(void *buf,int size,int count,FILE *fp); 功能为从fp指向的文件中读取长度为size的count个数据项并输入到以 buf为首地址的缓冲区中。*/ while (!feof(fp)) { if (fread(&pe[count++], sizeof(struct phone_item), 1, fp) != 1) break; } printf('Load phone information successfully!'); return count - 1; } 五、telbook4.c #include'telbook.h' /*Query_by_phone函数功能为根据电话号码进行线性查询。 参数pe和count给定了当前所有的电话信息。*/ void Query_by_phone(struct phone_item pe[], int cur_count) { int i; char num[20]; printf('Please input the phone number:'); gets_s(num); for (i = 0; i < cur_count;="" i++)=""> if (strcmp(pe[i].phone, num) == 0) { printf('Info locked:'); printf('%13s%20s%30s\n', pe[i].phone, pe[i].name, pe[i].address); break;/*已找到,退出循环*/ } } if (i >= cur_count) printf('Can't find the number you input!\n'); } /*Query_by_name函数,根据用户输入的机主姓名进行线性查询。 参数pe和count给定了当前所有电话信息。*/ void Query_by_name(struct phone_item pe[], int cur_count) { int i; char name[20]; printf('Please input the owner's name:'); gets_s(name); for (i = 0; i < cur_count;="" i++)=""> if (strcmp(pe[i].name, name) == 0) { printf('Info locked:'); printf('%13s%20s%30s\n', pe[i].phone, pe[i].name, pe[i].address); break; } } if (i >= cur_count) printf('Can't find the name you input!\n'); } 六、telbook5.c #include'telbook.h' /*sort_by_phone函数使用冒泡排序将电话信息按照电话号码升序排序。 参数pe和cur_count给定了当前所有的电话信息。*/ void sort_by_phone(struct phone_item pe[], int cur_count) { int i, j; struct phone_item tmp; for (i = 0; i < cur_count="" -="" 1;=""> for (j = 0; j < cur_count="" -="" 1="" -="" i;="" j++)=""> if (strcmp(pe[j].phone, pe[j + 1].phone)>0) { tmp = pe[j]; pe[j] = pe[j + 1]; pe[j + 1] = tmp; } } printf('Sort by phone number successfully!\n'); } /*sort_by_name函数使用冒泡排序将电话信息按照机主姓名升序排序。 参数pe和cur_count给定了当前所有的电话信息。*/ void sort_by_name(struct phone_item pe[], int cur_count) { int i, j; struct phone_item tmp; for (i = 0; i < cur_count="" -="" 1;=""> for (j = 0; j < cur_count="" -="" 1="" -="" i;="" j++)=""> if (strcmp(pe[j].name, pe[j + 1].name)>0) { tmp = pe[j]; pe[j] = pe[j + 1]; pe[j + 1] = tmp; } } printf('Sort by phone name successfully!\n'); } 能看到这里的读者都是很好学的了,每一个字母都是小编亲手打出来的,大家能看到这小编也觉得很欣慰了。 |
|