第十二章编写大程序PPT课件_第1页
第十二章编写大程序PPT课件_第2页
第十二章编写大程序PPT课件_第3页
第十二章编写大程序PPT课件_第4页
第十二章编写大程序PPT课件_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 编写大程序 2 C程序结构 常见的C程序由多个源文件组成 还有一些头文件 源文件 函数的定义和外部变量头文件 可以在源文件直接共享的信息 3 把程序分成多个源文件有许多显著的特点 把相关的函数和变量分组放在同一个文件中可以使程序的结构清晰可以分别对每一个源文件进行编译 单独修改 单独编译 把函数归类分组放在不同的源文件中更利于复用 4 把程序分成多个源文件引发的问题 某个文件中的函数如何调用定义在其他文件中的函数呢 函数如何访问其他文件的外部变量 2个文件如何共享一个宏定义或类型定义 答案 include指令 5 include指令的使用 include引用C语言自身库的头文件搜寻 系统头文件所在的目录 include 文件名 引用所有的头文件 包含自己编写的头文件搜寻 先搜寻当前目录 然后搜寻系统头文件所在的目录也可指定目录 include c h 6 共享类型定义 在程序中有2个文件 includeboolean h 7 共享函数原型 8 当prog c被编译时 file3 h将被编译2次 导致重定义错误 8 保护头文件 9 为了保护头文件 用 ifndef endif来封闭文件的内容 ifndefBOOLEAN H defineBOOLEAN H defineTRUE1 defineFALSE0typedefintBool endif 9 保护头文件 解决办法 10 完整案例分析 文本格式程序 这个程序分成3个文件word c 读单词的相关函数line c 行缓冲相关的函数justify c 主调函数对应3个头文件 word h word c中的函数原型line h line c中的函数原型 11 word h ifndefWORD H defineWORD H read word Readsthenextwordfromtheinputand storesitinword Makeswordemptyifno wordcouldbereadbecauseofend of file Truncatesthewordifitslengthexceeds len voidread word char word intlen endif 12 line h ifndefLINE H defineLINE H clear line Clearsthecurrentline voidclear line void add word Addswordtotheendofthecurrentline Ifthisisnotthefirstwordontheline putsonespacebeforeword voidadd word constchar word 13 space remaining Returnsthenumberofcharactersleft inthecurrentline intspace remaining void write line Writesthecurrentlinewith justification voidwrite line void flush line Writesthecurrentlinewithout justification Ifthelineisempty does nothing voidflush line void endif 14 justify c Formatsafileoftext include include line h include word h defineMAX WORD LEN20intmain void charword MAX WORD LEN 2 intword len 15 clear line for read word word MAX WORD LEN 1 word len strlen word if word len 0 flush line return0 if word len MAX WORD LEN word MAX WORD LEN if word len 1 space remaining write line clear line add word word 16 word c include include word h intread char void intch getchar if ch n ch t return returnch 17 voidread word char word intlen intch pos 0 while ch read char while ch 18 line c include include include line h defineMAX LINE LEN60charline MAX LINE LEN 1 intline len 0 intnum words 0 voidclear line void line 0 0 line len 0 num words 0 19 voidadd word constchar word if num words 0 line line len line line len 1 0 line len strcat line word line len strlen word num words intspace remaining void returnMAX LINE LEN line len 20 voidwrite line void intextra spaces spaces to insert i j extra spaces MAX LINE LEN line len for i 0 i0 puts line 21 构建包含多文件的大型程序的

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论