《课程设计(文本格式化)(共15页).doc》由会员分享,可在线阅读,更多相关《课程设计(文本格式化)(共15页).doc(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上附件1:学 号: 27课 程 设 计题 目文本格式化学 院计算机科学与技术学院专 业计算机科学与技术班 级计算机1104班姓 名成霖指导教师吴利军2013年6月17日题目:文本格式化一,问题描述:试写一个文本格式化程序可以处理一个输入文件,它按照用户指定的下列版面规格参数重排版面:实现页内调整、分段、分页等文本处理功能,排版结果存入输出文本文件中。(1)输出文件中字与字之间只留一个空格符,即实现多余空格符的压缩。(2)输出文件中,任何完整的字不能分割在两行,行尾可以不齐,但行首要对齐。 (3)将页号印在页底空行中第2行的中间位置上。(4)版面要求的参数见题集p117。
2、(5)测试用例自己设计。二,设计: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 * colu
4、mns,int * page)该函数的功能是:输出页首的格式(头长+左空白) 7:void readIn(FILE * fp1,FILE * fp2)该函数是整个程序的核心,执行程序的主要逻辑判断 8:void main()3,实验设计程序代码:#include#include#includeint (PageLength) = 56;/页长(一页最大的行数)int PageWedth = 60;/页宽(一行最大的字符数)int LeftMargin = 10;/左空白(一行开始输出的空格数)int HeadingLength = 5;/头长(一页开始输出的空行数)int FootingLen
5、gth = 5;/脚长(每页最后空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()/输出头文件中的所以整形变量/这个函数中的参数全部来自头文件printf(n);printf(格式化文本文件结束n);pr
6、intf(版面的参数如下: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);/函数2:void writeOut(FILE * fp2,char c1)/输出到文本文件2和屏幕ch
7、ar ch1;ch1 = fputc(c1,fp2);/接收从文本文件1中读出的一个字符,并且保存到文本文件2中printf(%c,ch1);/把这个字符输出到外设(屏幕)/函数3:int zuokongbai(FILE * fp2)/左空白for(i=0; i=0; i-)writeOut(fp2,*(aIndex+i);/向文本文件2中输出page(*page)+;/指向下一页/函数5:void out(int * columns,char * array,int * linage,FILE * fp2,int * page,int * size)/输出if(*(columns)+1+st
8、rlen(array) 60)/判断这一行是否还能够输出空格加一个单词/这一行不能再输出了,开始换行if(*(linage)50)/判断这一页是否已经印满linageFull(fp2,linage,columns,page);/转到下一页for(i=0; i*(size); i+)writeOut(fp2,*(array+i);/把保存在数组中的一个字输出到文本文件2中(*(columns)+;/输出一个字符,这一行的字符数就增加一个(*(size) = 0;/输出之后就开始重新向数组array中读入字符,所以size要归0(*(array) =0;/如果是多个空格连在一起,那么就需要把arr
9、ay数组第一个设为结束字符。else/这一页还没印满,换行后开始输出writeOut(fp2,n);/换行(*(linage)+;/*(columns)=0;(*(columns) = zuokongbai(fp2);/左空白10for(i=0; i(*(size); i+)writeOut(fp2,(*(array+i);/通过循环向文本文件2中输入字(*(columns)+;(*(size) = 0;(*(array) =0;else/这一行还没有满,还可以再输出writeOut(fp2, );(*(columns)+;for(i=0;i*size; i+)writeOut(fp2,*(a
10、rray+i);(*(columns)+;(*(size) = 0;(*(array) =0;/函数6:void linageFull(FILE * fp2,int * linage,int * columns,int * page)/开始下一页的打印/完成页的尾部格式(空一行后居中输出页码page再空3行)writeOut(fp2,n);/空一行for(i=0; i29; i+)/居中输出页码writeOut(fp2, );pageNumber(fp2,aIndex,page);for(i=0; i3; i+)writeOut(fp2,n);/再空3行*linage=0;/开始打印第二页。实
11、现输出页首的格式(头长+左空白)for(i=0; i5; i+)/头长5writeOut(fp2,n);*linage+;/已经占有linage行*columns = zuokongbai(fp2);/左空白10/函数7:void readIn(FILE * fp1,FILE * fp2)/读入文本文件int blankNumber=0;/记录空格的数量int linage = 0;/记录行数int columns = 0;/记录单行的字节数char array61 = 0;/存一个单词char * arrayIndex = array;int size = 0; /初始化单词的长度for(i
12、=0; i5; i+)/头长5writeOut(fp2,n);linage+;/已经占有linage行columns = zuokongbai(fp2);/左空白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)/
13、程序中设计一个字不能超过60个字符,这里判断条件为61是加了一个结束符0.printf(字符格式错误!哪有这么长的单词!n);break; /结束while循环/函数8:/程序的入口void main()FILE * fp1,* fp2;/fp1指向读入的文本文件,fp2指向输出的文本文件char fileName_one100;/保存要格式化文本文件的文件名char fileName_two100;/保存格式化之后输出文本文件的文件名printf(请输入要格式化文件的文件名(包括后缀名):n);gets(fileName_one);if(fp1=fopen(fileName_one,r)=N
14、ULL)printf(The file %s was not fopen!n,fileName_one);exit(0);/得到要进行格式化的文本文件1的地址printf(请输入输出文件的文件名(包括后缀名):n);gets(fileName_two);if(fp2=fopen(fileName_two,a)=NULL)printf(The file %s was not fopen!n,fileName_two);exit(0);/得到格式化后要保存到文本文件2的地址readIn(fp1,fp2);/读入文本文件开始格式化printOut();/参数说明fclose(fp1);/关闭文件(把
15、文件缓冲区的数据送入硬盘中)fclose(fp2);/关闭文件(把文件缓冲区的数据送入硬盘中)3,调试过程:l (1):开始我设计页码是整形,当格式化文本文件过大时,页码输出就出现了问题。实现输出页码是用fputc这个函数,它是实现字符的输出,就把整形的页码数转换为ASCII码形式输出了。改为:把整形的页码转换为字符型用一个字符数组来保存。每次输出页码是就是输出这个数组中的数据。这样就实现了页码的完整输出。l (2)读一个字符判断是否为:空,。(占不考虑其他条件)不是就直接输出。导致不能实现任何完整的字都没有被分割在两行这个功能。改为:读一个字符判断是否为:空字符,。(占不考虑其他条件)不是就
16、保存在数组中,这个数组就是保存一个字的作用。判断输出就是一个一个字的输出,这样就实现了任何完整的字都没有被分割在两行这个功能。4,运行结果:本人运行程序的到的结果:(测试数据1.txt) 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 eastern cultures, silence has long bee
17、n 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 can communicate agreement, confusion, resp
18、ect, sadness, or any number of meanings. In eastern cultures, 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 dif
19、ferent from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings. In eastern cultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable. To most people in the Un
20、ited 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 eastern cultures, silence has long been regarded as a virtue. While in western cultures, sil
21、ence 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 meanings. In eastern cu
22、ltures, 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 can communicat
23、e agreement, confusion, respect, sadness, or any number of meanings. In eastern cultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagreeable. 1 To most people in the United States, silence The use of silence The use of s
24、ilence in communication is different from culture to culture. Silence can communicate agreement, confusion, respect, sadness, or any number of meanings. In eastern cultures, silence has long been regarded as a virtue. While in western cultures, silence has generally been considered socially disagree
25、able. 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 eastern cultures, silence has long been regarded as a virtue.
26、While in western cultures, silence has generally been considered socially disagreeable. To most people in the United States, silence The use of silence附截图:5,经验和体会:本次试验,花费时间有点长,但是在我多方面查找资料和询问同学下终于顺利完成了。本次试验也很成功,让自己也很有成就感。经过本次试验,让我熟悉了程序设计中应该注意的事项和容易出错的地方,但让我看到了很多自己算法设计上的不足,以后还需改进和提高的地方。学无止境,通过本次试验,也同样让我明白了细节决定成败。以后还要,加油!专心-专注-专业