课程设计(文本格式化)_第1页
课程设计(文本格式化)_第2页
课程设计(文本格式化)_第3页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

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

文档简介

1、附件1:学 号:27课程设计题目文本格式化学院计算机科学与技术学院专业计算机科学与技术班级计算机1104班姓名成霖指导教师吴利军2013 年 6 月 17 日题目:文本格式化一,问题描述 :试写一个文本格式化程序可以处理一个输入文件, 它按照用户指定的下列版面规格参数重排版 面:实现页内调整、分段、分页等文本处理功能,排版结果存入输出文本文件中。(1)输出文件中字与字之间只留一个空格符,即实现多余空格符的压缩。( 2)输出文件中,任何完整的字不能分割在两行,行尾可以不齐,但行首要对齐。( 3)将页号印在页底空行中第2 行的中间位置上。( 4)版面要求的参数见题集p117。(5)测试用例自己设计

2、。二,设计 :1, 版面要求的参数要包含 :页长 (Page Length) 每页内文字 (不计页号 )的行数。页宽(Page Wedth)每行内文字所占最大字符数。左空白 (Left Margin) 每行文字前的固定格数。头长 (Heading Length) 每页页顶所空行数。脚长 (Footing Length) 每页页底所空行数 (含页号行 )起始页号(Starting Page Number)首页的页号。2,概要设计:?1: void printOut() 该函数的功能是:格式化文本文件后必要提示用户的信息说明2: void writeOut(FILE * fp2,char c1)

3、该函数的功能是:将字符输出到文本文件和屏幕3: int zuokongbai(FILE * fp2) 该函数的功能是:实现文本参数格式(左空白)4: void pageNumber(FILE * fp2,char * aIndex,int * page) 该函数的功能是:输出当前页码 5: void out(int * columns,char * array,int * linage,FILE * fp2,int * page,int * size) 该函数的功能是:判断是否输出到文本文件和屏幕 6: void linageFull(FILE * fp2,int * linage,int *

4、 columns,int * page) 该函数的功能是:输出页首的格式(头长+ 左空白)7: void readIn(FILE * fp1,FILE * fp2) 该函数是整个程序的核心,执行程序的主要逻辑判断 8: void main()3,实验设计程序代码:#include/页长(一页最大的行数) /页宽(一行最大的字符数) /左空白(一行开始输出的空格数) /头长(一页开始输出的空行数) / 脚长(每页最后空 5 行) /起始页号#include #include int (PageLength) = 56; int PageWedth = 60; int LeftMargin = 1

5、0; int HeadingLength = 5; int FootingLength = 5; int StaringPageNumber = 1;int page = 1; / 记录页数int i = 0;/循环变量char a5; /记录 page 的 char 类型 char * aIndex = a; /数组 a 的首地址/函数声明(后面的函数调用了它)void linageFull(FILE * fp2,int * linage,int * columns,int * page);/ 函数 1:void printOut()/ 输出头文件中的所以整形变量/这个函数中的参数全部来自头

6、文件 printf(n);printf( 格式化文本文件结束 n); printf( 版面的参数如下: n);printf( 页长: %dn,PageLength);printf( 页宽: %dn,PageWedth);printf( 左空白: %dn,LeftMargin);printf( 头长: %dn,HeadingLength);printf( 脚长: %dn,FootingLength);printf( 起始页号: %dn,StaringPageNumber);printf(n);printf( 特别说明:本程序只支持英文格式化,对于中文格式化存在乱码问题。 n); printf(n

7、);/ 函数 2:void writeOut(FILE * fp2,char c1) /输出到文本文件 2 和屏幕char ch1;ch1 = fputc(c1,fp2);/ 接收从文本文件 1 中读出的一个字符,并且保存到文本文件 2 中 printf(%c,ch1); /把这个字符输出到外设 (屏幕 )/ 函数 3:/ 左空白/循环 10 次,输出 10 个空格到文本文件 2中/ 返回值为 10.它赋给记录单行字节数的变量columnsint zuokongbai(FILE * fp2) for(i=0; i=0; i-)writeOut(fp2,*(alndex+i); /向文本文件 2

8、 中输出 page(*page)+;/指向下一页/函数 5:void out(int * columns,char * array,int * linage,FILE * fp2,int * page,int * size)/ 输出 if(*(columns)+1+strlen(array) 60) / 判断这一行是否还能够输出空格加一个单词 /这一行不能再输出了,开始换行 if(*(linage)50) / 判断这一页是否已经印满 linageFull(fp2,linage,columns,page);/ 转到下一页 for(i=0; i*(size); i+)writeOut(fp2,*(

9、array+i);/ 把保存在数组中的一个字输出到文本文件 2 中 (*(columns)+;/ 输出一个字符,这一行的字符数就增加一个(*(size) = 0;/ 输出之后就开始重新向数组 array 中读入字符,所以 size 要归 0 (*(array) =0;/ 如果是多个空格连在一起,那么就需要把 array 数组第一个设为结束字 符。else / 这一页还没印满,换行后开始输出 writeOut(fp2,n);/ 换行 (*(linage)+; /*(columns)=0;(*(columns) = zuokongbai(fp2); / 左空白 10for(i=0; i(*(siz

10、e); i+) writeOut(fp2,(*(array+i);/ 通过循环向文本文件 2 中输入字 (*(columns)+;(*(size) = 0; (*(array) =0;else /这一行还没有满,还可以再输出writeOut(fp2, );(*(columns)+;for(i=0;i*size; i+) writeOut(fp2,*(array+i); (*(columns)+;(*(size) = 0;(*(array) =0;开始下一页的打印/ 函数 6:void linageFull(FILE * fp2,int * linage,int * columns,int *

11、page)II完成页的尾部格式(空一行后居中输出页码page再空3 行)writeOut(fp2,n);/空一行for(i=0; i29; i+)II 居中输出页码writeOut(fp2, );pageNumber(fp2,aIndex,page);for(i=0; i3; i+)writeOut(fp2,n); II再空 3 行*linage=0;开始打印第二页。实现输出页首的格式(头长+左空白)for(i=0; i5; i+) II 头长 5writeOut(fp2,n);*linage+;II 已经占有 linage 行*columns = zuokongbai(fp2); II 左空

12、白 10II函数7:void readIn(FILE * fp1,FILE * fp2) II 读入文本文件 int blankNumber=0;int linage = 0;int columns = 0;II记录空格的数量II记录行数II记录单行的字节数char array61 = 0;II 存一个单词char * arrayIndex = array;int size = 0;II 初始化单词的长度for(i=0; i5; i+) II 头长 5writeOut(fp2,n); linage+; / 已经占有 linage 行columns = zuokongbai(fp2); / 左空

13、白 10for(i=0; i50) /判断这一页是否已经印满 /印满了要换页输出 linageFull(fp2,&linage,&columns,&page);for(i=0; i7; i+)/段首缩进 8 字节 writeOut(fp2, ); columns+;else/这一页没有印满 writeOut(fp2,n);/ 换行 linage+; columns=0;columns = zuokongbai(fp2); / 左空白 10for(i=0; i61)/ 程序中设计一个字不能超过 60 个字符,这里判断条件为 结束符 0 .printf( 字符格式错误 ! 哪有这么长的单词 !n)

14、;break; /结束 while 循环 /函数 8:/程序的入口void main()FILE * fp1,* fp2; char 100;/char 100;/ fp1 指向读入的文本文件, 保存要格式化文本文件的文件名 保存格式化之后输出文本文件的文件名fp2 指向输出的文本文件printf( 请输入要格式化文件的文件名 (包括后缀名 ):n); gets();if(fp1=fopen(,r)=NULL) printf(The file %s was not fopen!n,); exit(0);/ 得到要进行格式化的文本文件 1 的地址printf( 请输入输出文件的文件名 (包括后缀

15、名 ):n); gets();if(fp2=fopen(,a)=NULL)printf(The file %s was not fopen!n,); exit(0); /得到格式化后要保存到文本文件2 的地址readIn(fp1,fp2); / 读入文本文件开始格式化printOut(); / 参数说明fclose(fp1);fclose(fp2);/ 关闭文件 (把文件缓冲区的数据送入硬盘中 )/ 关闭文件 (把文件缓冲区的数据送入硬盘中 )3,调试过程:(1):开始我设计页码是整形,当格式化文本文件过大时,页码输出就出现了问题。 实现输出页码是用 fputc 这个函数,它是实现字符的输出,

16、就把整形的页码数转换为 ASCII 码形式输出了。改为:把整形的页码转换为字符型用一个字符数组来保存。 每次输出页码是就是输出这个数组中的数据。这样就实现了页码的完整输出。( 2)读一个字符判断是否为:空, 。(占不考虑其他条件)不是就直接输出。导 致不能实现任何完整的字都没有被分割在两行这个功能。改为:读一个字符判断是否 为:空字符, 。(占不考虑其他条件)不是就保存在数组中,这个数组就是保存一 个字的作用。判断输出就是一个一个字的输出,这样就实现了任何完整的字都没有被 分割在两行这个功能。4,运行结果:本人运行程序的到的结果: (测试数据 1.txt )The use of silence

17、 in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.In easterncultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.To

18、most people in the United States, silence The use of silence The use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.In easterncultures, silence has long been regarded as a virtue. While in we

19、stern cultures, silence has generally been considered socially disagreeable.To most people in the United States, silence The use of silence The use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect,sadness, or any number of meanin

20、gs.In easterncultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.To most people in the United States, silence The use of silence The use of silence in communication is different from culture to culture. Silence

21、can communicate agreement, confusion, respect, sadness, or any number of meanings.In easterncultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.To most people in the United States, silence The use of silence The

22、 use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.In easterncultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially d

23、isagreeable.To most people in the United States, silence The use of silence The use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.In easterncultures, silence has long been regarded as a virt

24、ue. While in western cultures, silence has generally been considered socially disagreeable.1To most people in the United States, silence The use of silence The use of silence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any

25、 number of meanings.In easterncultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.To most people in the United States, silence The use of silence The use of silence in communication is different from culture to

26、culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings.In easterncultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable.To most people in the United States, silence The u

27、se of silence附截图:ToInToInToInTonost people in the United States silence The use of silence The use of silence in connunication is different fron culture to culture. Silence can connunicate agreements confusion respects sadness* or any nunber of meanings.easterncultures, silence has long been regarde

28、d as a u ii*tue. Uhile xn western uultuueM. s ilence has generally been considered socially disagreeable.nost people in the United States, silence The use of silence The use of silence in conmunication is different fron culture to culture. Silence can connuiixcate ayjreeinent conf us ion, respect, s

29、adnessor any nunber of meanings.easterncultures silence has long been regarded as a virtue. While in western cultiiuts. silence has generally been considered socially disagreeable.most people in the United States, silence The use o silence The use of silence in connunication is different fron cultur

30、e to culture. Silence can connunicate agreenent, confus ion, respect, sadness or any nunber of meanings.easterncultures silence has long been regarded as a virtue. While in western cultures generally been considered sociallynost people in the United States silencesilence has disagreeable.Theuse of s xlcnce TJie use of s xlcnce in connun icat ion is different fron culture to culture. Silence can connunicate agreenent, confusion, respect,To most people xn use ofthe United States, silence silence格式化文本文件结束I lie特别说明:加呈序只支

温馨提示

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

评论

0/150

提交评论