版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、乔 林,计算机程序设计基础,Email: Tel: 62780973,清华大学计算机科学与技术系,第七章 库与接口设计,学习目标 掌握接口的概念,理解接口是用户和库之间的界面,是实现程序抽象的手段 掌握通过相关接口使用ANSI C标准库和Turbo C图形库的方法 了解接口设计的一般原则,能够进行自定义接口的设计和实现,7.1 用户、库与接口概述,库与接口 库的目的:重用 接口的目的:隐藏库的设计细节 接口的定义:影响库的设计质量 C 语言中库的接口,7.2 标准库,标准 I/O 库 数学库 数字与字符特征库 字符串库 辅助函数库,标准 I/O 库接口,头文件:stdio.h 常用标准 I/O
2、 库函数,数学库接口,头文件:math.h 常用数学库函数,数学库应用示例,打印0360度之间,增量为30度的所有角度的正弦、余弦和正切函数值,#include #include #define PI 3.14159 void main() int angle = 0; double radian, coef = PI / 180; for( ; angle = 360; angle += 30 ) radian = coef * angle; printf( “sin(%d) = %lf, “, angle, sin(radian) ); ,数字与字符特征库接口,头文件:ctype.h 常用
3、数字与字符特征库函数,数字与字符特征库应用示例,从键盘输入一行字符,统计其中字母、数字字符、空格个数,同时统计字母中大写、小写字母个数,#include #include void main() char c, str255; int alpha = 0, upper = 0, lower = 0, digit = 0, space = 0, i = 0; printf( “Input a string: “ ); gets(str); while( c = stri+ ) if( isalpha(c) ) alpha+; if(isupper(c) upper+; else lower+;
4、if( isdigit(c) ) digit+; if( isspace(c) ) space+; printf( “Letter : %d; Upper : %d; Lower : %dn“, alpha, upper, lower ); printf( “Digit : %d; Space : %dn“, digit, space ); ,字符串库接口,头文件:string.h 常用字符串库函数,字符串库应用示例,完成一个简单学生电话簿管理程序,要求在一个主菜单下用户可以有以下选择 添加新记录 删除一条记录 通过姓名查询电话 浏览全部记录 退出,字符串库应用示例,#include #inc
5、lude #include #define MAX 50 struct student char name30; char sex; char tel20; ; void Init( struct student stud ); void Menu( struct student stud ); void Add( struct student stud ); void Delete( struct student stud ); void Search( struct student stud ); void Browse( struct student stud ); int GetLen
6、gth( struct student stud ); void main() struct student studMAX; Init( stud ); Menu( stud ); ,字符串库应用示例,void Menu( struct student stud ) int choice = 0; while( choice != 5 ) printf(“n1.Addn2.Deln3.Searchn4.Browsen5.Exitn“); printf(“Input your choice( 1-5 ) :“); scanf(“%d“, ,字符串库应用示例,void Init( struct
7、student stud ) int i; for( i = 0; i MAX; i+ ) strcpy( , “ ); /* 获取记录的数目 */ int GetLength( struct student stud ) int length = 0; while( strcmp( ,“ ) != 0 ) length+; return length; ,字符串库应用示例,void Add( struct student stud ) int length = 0; char temp30; length = GetLength( stud
8、); do printf( “nname : “ ); gets( ); if( strcmp( ,“ ) = 0 ) break; printf( “nsex: “ ); gets( temp ); studlength.sex = temp0; printf( “ntel: “ ); gets( studlength.tel ); printf( “nAdd next one? (Y|N): “ ); gets( temp ); while( ( toupper( temp0 ) = Y ) ,字符串库应用示例,void Del
9、ete( struct student stud ) int length = 0, flag = 0, i; char name30; length = GetLength( stud ); printf( “Input the name to delete: “ ); gets( name ); for( i = 0; i length; i+ ) if( strcmp( , name ) = 0 ) flag = 1; break; if( flag ) printf( “%s : %c : %s will be deleted!n“, , stu
10、di.sex, studi.tel ); for( 0; i length 1; i+ ) studi = studi+1; strcpy( ,“ ); else printf( “Cant find %s!n“, name ); ,字符串库应用示例,void Search( struct student stud ) int length = 0, exact = 0, like = 0, i; char name30; length = GetLength( stud ); printf( “Input the name to search: “ ); gets( na
11、me ); for( i = 0; i length; i+ ) if( strcmp( , name ) = 0 ) printf(“%s:%c:%sn“, , studi.sex, studi.tel); exact = 1; if( !exact ) for( i = 0; i length; i+ ) if(strstr(,name)|strstr(name,) printf(“%s:%c:%sn“,,studi.sex,studi.tel); like = 1; if( !( exac
12、t | like ) ) printf( “Cant find %s!n“, name ); ,字符串库应用示例,void Browse( struct student stud ) int length = 0, i; length = GetLength( stud ); for( i = 0; i length; i+ ) printf( “No.%d: %s : %c : %sn“, i + 1, , studi.sex, studi.tel ); ,辅助函数库接口,头文件:stdlib.h 常用辅助函数库函数,辅助函数库应用示例,修改学生电话簿管理程序,使用动态内
13、存分配技术存储学生姓名,struct student char name30; char sex; char tel20; ;,原数据结构的缺点:姓名字段的长度固定,大多数姓名都不超过30字节,不过也可能存在超过30字节的情况,怎么办?,struct student char* name; char sex; char tel20; ;,使用指针技术,在需要存储姓名时再分配恰当数目的内存,辅助函数库应用示例,void Add( struct student stud ) int length = 0; char buffer255; length = GetLength( stud ); do
14、 printf( “nname : “ ); gets( buffer ); if( strcmp( buffer,“ ) = 0 ) break; = ( char* )malloc( strlen(buffer) + 1 ); strcpy(, buffer ); while( ( toupper( temp0 ) = Y ) ,辅助函数库应用示例,void Delete( struct student stud ) if( flag ) printf( “%s : %c : %s will be deleted!n“, stu
15、, studi.sex, studi.tel ); free( ); = NULL; for( ; i length 1; i+ ) studi = studi+1; else printf( “Cant find %s!n“, name ); ,7.3 图形库,Turbo C 图形库 图形系统初始化 函数原型:initgraph( /* 图形显示模式,如VGAHI等 */ 第三个参数表示驱动程序路径,如“c:tcbgi”等 基本绘图函数 点操作、线段操作、2D与3D操作、基本屏幕与绘图操作 预定义的线型、线宽、颜色、填充模式常数,图形
16、库应用示例,用Turbo C 图形库绘制公共汽车轮廓,图形库应用示例:主函数设计,void main() 进行必要的初始化设置工作; 绘制车身; 绘制两个车门; 绘制五个车窗; 绘制三个车轮; ,图形库应用示例:汽车库设计,void Initial(); void Body( int lbottom_x, int lbottom_y, int length, int height, int color ); void Door( int lbottom_x, int lbottom_y, int length, int height, int color ); void Window( int
17、 lbottom_x, int lbottom_y, int length, int height, int color ); void Wheel( int axle_x, int axle_y, int inner_r, int outer_r, int color );,汽车库代码的组织 函数实现:buspart.c 函数原型:buspart.h,图形库应用示例:主函数的实现,#include “Busparts.h” void main() Initial(); Body( 150, 200, 300, 100, RED ); Door( 200, 190, 35, 70, WHITE
18、 ); Door( 360, 190, 35, 70, WHITE ); Window( 155, 140, 30, 20, WHITE ); Window( 245, 140, 30, 20, WHITE ); Window( 280, 140, 30, 20, WHITE ); Window( 315, 140, 30, 20, WHITE ); Window( 410, 140, 30, 20, WHITE ); Wheel( 180, 200, 10, 15, YELLOW ); Wheel( 290, 200, 10, 15, YELLOW ); Wheel( 415, 200, 1
19、0, 15, YELLOW ); ,图形库应用示例:汽车库头文件,#ifndef _BUSPARTS_H #define _BUSPARTS_H void Initial(); void Body( int lbottom_x, int lbottom_y, int length, int height, int color ); void Door( int lbottom_x, int lbottom_y, int length, int height, int color ); void Window( int lbottom_x, int lbottom_y, int length,
20、int height, int color ); void Wheel( int axle_x, int axle_y, int inner_r, int outer_r, int color ); #endif /*_BUSPARTS_H */,图形库应用示例:汽车库源文件,#include #include #include #include void Wheel( int axle_x, int axle_y, int inner_r, int outer_r, int color ) int temp; if(inner_r outer_r) temp = inner_r; inner
21、_r = outer_r; outer_r = temp; setcolor( color ); circle( axle_x, axle_y, inner_r ); circle( axle_x, axle_y, outer_r ); circle( axle_x, axle_y, inner_r / 4 ); setfillstyle( HATCH_FILL, color ); floodfill( axle_x + inner_r + 1, axle_y + 1, color ); setfillstyle( SOLID_FILL, color ); floodfill( axle_x,
22、 axle_y, color ); line( axle_x + inner_r / 4, axle_y, axle_x + inner_r, axle_y ); line( axle_x inner_r / 4, axle_y, axle_x inner_r, axle_y ); line( axle_x, axle_y + inner_r / 4, axle_x, axle_y + inner_r ); line( axle_x, axle_y inner_r / 4, axle_x, axle_y inner_r ); ,图形库应用示例:汽车库源文件,void Body( int lbo
23、ttom_x, int lbottom_y, int length, int height, int color ) int left, top, right, bottom, radius; left = lbottom_x; bottom = lbottom_y; top = lbottom_y height; right = lbottom_x + length; if(length height) radius = height/4; else radius = length/4; setcolor( color ); line( left, bottom, left, top + r
24、adius ); line( left, bottom, right, bottom ); line( right, bottom, right, top + radius ); line( left + radius, top, right radius, top ); arc( left + radius, top + radius, 90, 180, radius ); arc( right radius, top + radius, 0, 90, radius ); ,图形库应用示例:汽车库源文件,void Door( int lbottom_x, int lbottom_y, int
25、 length, int height, int color ) int left, top, right, bottom, mid_length, mid_height, temp; if( length height ) temp = length; length = height; height = temp; mid_length = length / 2.0; mid_height = height * 2.0 / 3; left = lbottom_x; bottom = lbottom_y; top = lbottom_y height; right = lbottom_x +
26、length; setcolor( color ); rectangle( left, top, right mid_length, bottom ); rectangle( left + mid_length, top, right, bottom ); rectangle( left, top, right mid_length, bottom mid_height ); rectangle( left + mid_length, top, right, bottom mid_height ); ,图形库应用示例:汽车库源文件,void Window( int lbottom_x, int lbottom_y, int le
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024学年七年级下学期期末考前打靶卷01(中图版·北京)(全解全析)
- 医生转正个人工作总结
- 《 建筑工程设计BIM应用》 习题及解答
- 煤矿重大事故隐患2026版自查自改对照检查表
- 农业技术推广服务对农业面源污染治理的影响研究意义
- 雹灾救援受伤人员救治绿色通道不畅要执行畅通整改措施
- 家庭洗烘套装叠放支架安装指南
- 《Linux系统管理与服务配置》课件 第8章 文件共享服务
- 2026年SEO趋势报告 搜索引擎优化趋势 2026
- 2026年天津市南开区中考英语二模试卷(含详细答案解析)
- 2026二季度重庆巫山县事业单位公开考调25人笔试备考题库及答案解析
- 2026-2030中国电热合金行业发展分析及发展战略研究报告
- 2026年超声诊断仪行业分析报告及未来发展趋势报告
- 黑吉辽蒙2025年高考真题物理试卷【附答案】
- 2026中信证券总部暑期日常实习招聘笔试备考试题及答案解析
- 城镇供水长距离输水管(渠)道工程技术规程
- 2026春季学期国家开放大学专科《高等数学基础》一平台在线形考形考任务一试题及答案
- 《JBT 11733-2013熔模铸造用煅烧高岭土砂粉》专题研究报告
- GB/T 18926-2008包装容器木构件
- 助产技术操作技能考核评分标准Microsoft-Word-文档
- 智能家居ppt模板
评论
0/150
提交评论