1、附件1:学 号: 7课 程 设 计题 目文本格式化学 院计算机科学与技术学院专 业计算机科学与技术班 级计算机1104班姓 名成霖指引教师吴利军年6月17日题目:文本格式化一,问题描述:试写一种文本格式化程序可以解决一种输入文献,它按照顾客指定下列版面规格参数重排版面:实现页内调节、分段、分页等文本解决功能,排版成果存入输出文本文献中。(1)输出文献中字与字之间只留一种空格符,即实现多余空格符压缩。(2)输出文献中,任何完整字不能分割在两行,行尾可以不齐,但行首要对齐。 (3)将页号印在页底空行中第2行中间位置上。(4)版面规定参数见题集p117。(5)测试用例自己设计。二,设计:1,版面规定
2、参数要包括: 页长(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:int zuo
3、kongbai(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 * columns,int * page)该函数功能是:输出页首格式(头长+左空
4、白) 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 FootingLength = 5;/脚长(每页最后空5行)int StaringPageNumber
5、= 1;/起始页号int page = 1;/记录页数 int i = 0;/循环变量 char a5;/记录pagechar类型 char * aIndex = a;/数组a首地址 /函数声明(背面函数调用了它) void linageFull(FILE * fp2,int * linage,int * columns,int * page);/函数1:void printOut()/输出头文献中因此整形变量/这个函数中参数所有来自头文献printf(n);printf(格式化文本文献结束n);printf(版面参数如下:n);printf(页长:%dn,PageLength);printf
6、(页宽:%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和屏幕char ch1;ch1 = fputc(c1,fp2);/接受从文本文献1中读出一种字符,并且保存
7、到文本文献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+strlen(array) 60)/判断这一行与否还可以输出空格加一种单词/这一行不能再输出了,开始换行i
8、f(*(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;/如果是各种空格连在一起,那么就需要把array数组第一种设为结束字符。else/这一页还没印满,换行后开始输出writeOut(fp2,n);/换行(*
9、(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,*(array+i);(*(columns)+;(*(size) = 0;(*(array) =0;/函数6:void l
10、inageFull(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;/开始打印第二页。实现输出页首格式(头长+左空白)for(i=0;i5;i+)/头长5writeOut(fp2,n);*linage+;/已经占有l
11、inage行*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=0;i5;i+)/头长5writeOut(fp2,n);linage+;/已经占有linage行columns = zuokongbai(
12、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)/程序中设计一种字不能超过60个字符,这里判断条件为61是加了一种结束符0.printf(字符格式错误!哪有这样长单词!n);break;/结束while
13、循环/函数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)=NULL)printf(The file %s was not fopen!n,fileName_one);exit(0);/得到要进行格式化文本文献1地址printf(
14、请输入输出文献文献名(涉及后缀名):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);/关闭文献(把文献缓冲区数据送入硬盘中)fclose(fp2);/关闭文献(把文献缓冲区数据送入硬盘中)3,调试过程:l (1):开始我设计页码是整形,当格式化文本文献过大时,页码输出就浮现
15、了问题。实现输出页码是用fputc这个函数,它是实现字符输出,就把整形页码数转换为ASCII码形式输出了。改为:把整形页码转换为字符型用一种字符数组来保存。每次输出页码是就是输出这个数组中数据。这样就实现了页码完整输出。l (2)读一种字符判断与否为:空,。(占不考虑其她条件)不是就直接输出。导致不能实现任何完整字都没有被分割在两行这个功能。改为:读一种字符判断与否为:空字符,。(占不考虑其她条件)不是就保存在数组中,这个数组就是保存一种字作用。判断输出就是一种一种字输出,这样就实现了任何完整字都没有被分割在两行这个功能。4,运营成果:本人运营程序到成果:(测试数据1.txt) The use
16、 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,silence has generally been considered socially disagree
17、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. While
18、 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,respect, sadness,or any number of mea
19、nings. 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 different from culture to culture. Silen
20、ce 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 United States,silence The use of silence The
21、 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,silence has generally been considered socially disa
22、greeable. 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. W
23、hile 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 silence in communication is different from culture to culture. Silence can communicate agreement,confusion,respect, sadness,or any number
24、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 different from culture to culture.
25、 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 United States,silence The use of silence附截图:5,经验和体会:本次实验,耗费时间有点长,但是在我多方面查找资料和询问同窗下终于顺利完毕了。本次实验也很成功,让自己也很有成就感。通过本次实验,让我熟悉了程序设计中应当注意事项和容易出错地方,但让我看到了诸多自己算法设计上局限性,后来还需改进和提高地方。学无止境,通过本次实验,也同样让我明白了细节决定成败。后来还要,加油!