今天是国庆节第二天了,昨天小编奔波了一天终于赶到了家,在这里也祝大家国庆七天玩得开心。

这次给大家讲的程序是一个电话簿,具有基本的信息输入,显示,查询和排序功能,为了程序结构的清晰与后期维护和更新的便利,将把程序分为六个部分。

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');

}

能看到这里的读者都是很好学的了,每一个字母都是小编亲手打出来的,大家能看到这小编也觉得很欣慰了。

c语言电话本程序代码,C语言程序设计之电话簿相关推荐

  1. 输出姓名对应的电话号码C语言,C语言电话本程序(只是简单的姓名和电话号码增删改查),在线等...

    满意答案 528428a 2016.05.12 采纳率:52%    等级:7 已帮助:8人 #include #include #include #define OK 1 #define ERROR ...

  2. c语言万年历查询程序代码,C语言小程序实现万年历

    分享一个简单的小程序,看看如何打印万年历吧~ 程序打开时时这样的,输入一个年份: 比如输入2016再回车,效果如下: 下面我们来看看实现这个程序的代码吧: #include #include #def ...

  3. c语言万年历查询程序代码,C语言 万年历程序(示例代码)

    C语言 万年历程序 原代码:[email protected]:~/c++$ cat 123.c #include #define Mon   1 #define Tues  2 #define We ...

  4. c语言复杂的程序代码,C语言中复杂结构的序列化

    我正在尝试在C中序列化一组结构.这适用于除我的struct中包含的向量之外的所有数据.我可以将数据写入磁盘,然后将所有数据读回内存.唯一的问题是当我尝试访问向量的元素时,我得到了一个分段错误.我的代码 ...

  5. c语言常用的代码,初学C语言常用简单程序代码;

    <初学C语言常用简单程序代码;>由会员分享,可在线阅读,更多相关<初学C语言常用简单程序代码;(16页珍藏版)>请在人人文库网上搜索. 1.初学C语言常用简单程序代码素数的筛选 ...

  6. c语言编写数据存储的游戏,c语言经典小程序和c语言编写的小游戏带注释(自动保存的).doc...

    c语言经典小程序和c语言编写的小游戏带注释(自动保存的) 1.写一个定时?关机的小程?序,可以立即关?闭计算机,也可以一段?时间后关闭?计算机. #inclu?de #inclu?de #inclu? ...

  7. python语言能够整合各类程序代码-python语言概述

    python语言的发展 python语言诞生于1990年,由Guide van Rossum设计并领导开发. python语言是开源项目的优秀代表,其解释器的全部代码都是开源的. 编写Hello程序 ...

  8. c语言小程序作业,c语言小程序(c语言简单小程序代码)

    所以特此求经典C语言小程序.谢谢大家的关注!!! #include #include void function(int n){ int i,j,k,x=0; for(i=1;i<=n;i++) ...

  9. c语言对抗程序代码,C语言贪吃蛇源程序代码双人对抗

    C语言贪吃蛇源程序代码双人对抗 #include #include #include #include #include #include #include #define LEFT 100 #def ...

最新文章

  1. 力扣(LeetCode)刷题,简单题(第18期)
  2. 第一次全面揭示世界软件巨人微软致胜的技术奥秘
  3. 参考: 40个轻量级 JavaScript 库
  4. 《第一行代码》学习笔记16-碎片Fragment(1)
  5. PAT甲级1140 Look-and-say Sequence:[C++题解]统计连续个数
  6. Springboot整合xxl-job实现任务自定义定时任务
  7. 队列 句子分析 精辟的诠释 有图片
  8. 15000php等于多少人民币,b站多少硬币才相当于1人民币?你一定想不到!
  9. 基于python+django框架+Mysql数据库的新闻信息管理系统设计与实现
  10. 前端设备通过Ehome协议接入EasyCVR平台无法播放问题解决
  11. 【EI稳定检索】第二届计算机科学、电子信息工程和智能控制技术国际会议(CEI 2022)
  12. Python实现身份证号码合法性校验
  13. Excel+VBA 区域数据的去重
  14. 嵌入式Linux磁盘(硬盘、SD卡)读写性能测试
  15. 现在买5G手机你考虑好了吗?
  16. Python - 升级pip时提示拒绝访问
  17. 作为一位软件测试工程师,应当需要哪些能力?
  18. 笔记本电脑的键盘灯如何关
  19. 用MVP+OKHttp实现上传图片
  20. 电工基础笔记01 - 认识简单电路和画图方法

热门文章

  1. Apollo决策技术分享20190328
  2. 要在textarea文本框中粘贴图片怎么办?
  3. “东数西算”开启数据中心的新一轮“圈地运动”
  4. Html5 Egret游戏开发 成语大挑战(三)开始界面
  5. 《咸鱼分享》DNS轮询
  6. input type=file选择图片按钮样式修改与图片预览
  7. 归纳偏置 (Inductive Bias)
  8. uoj#311. 【UNR #2】积劳成疾(期望dp)
  9. Errors while compiling. Reload prevented
  10. 快速搞定开源框架Flowable