《课程设计报告-文本格式化.docx》由会员分享,可在线阅读,更多相关《课程设计报告-文本格式化.docx(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、合肥学院计算机科学与技术系数据结构课程设计报告20122013 学年第2 学期if (m 二二n,H m 二二,)(fputsC,wf);/段首空白LineCur = 9;if (ch =二,| | ch 二二n,)(if(wO+LineCur PageWedth)fputs(n , wf);LineCur = 1;LineNum+;)for (int k = 1; k PageWedth)if(LineNum+1 = PageLength-2) fputc (J n , wf);LineNum+;continue;)fputs (z,n ,wf);LineCur = 1;LineNum+;f
2、or (int k = 1; k PageWedth)(if(LineNum+1 = PageLength-2) (fputc (? n,, wf);LineNum+;continue;)fputs (n ,wf);LineCur = 1;LineNum+;)for (int k = 1; k = w0; k+)(fputc (wk, wf);LineCur+;)fputc (ch, wf);LineCur+;fputc ( , wf);标点后加空格、LineCur+;m 二 ;elseelsew0= 0;输入其他的字符w0+;wj = ch;m = ch;j+;)fflush(stdin);
3、 清除ch = fgetc(rf);)for(int k = 1; k = PageLength-2)fputs (n, wf);fputc (PageNum/10+48, wf);fputc (PageNum%10+48, wf);fputs (nn, wf);LineCur = 0;LineNum = 1;PageNum+;换行设计:在输出文件中,任何完整的字仍不能分割在两行,行尾不齐没关系,但行首要对齐,即左对齐。*页宽假设为80并k为0,那么换行,页长自增1。if (ch =二 | ch = n)换行(if (w0+LineCur PageWedth)判断是否超过页宽(fputs (n
4、 ,wf);LineCur = 1;LineNum+;重起段落设计:除了上述文本类字符之外,还存在着起控制作用的字符:符号 指示它后面的正文在格 式化时应另起一段排放,即空一行,并在段首缩入8个字符位置。矿自成一个字。if (m = ? n H m 二二,)fputs r,wf); 段首空白LineCur = 9;)缓冲区设置:-先结束的进程可以吧结果放入缓冲区内,进行下面的工作,而后做完的进程可以从缓冲区内取出原来的数据继续工作。指由多个以不同速度或优先级运行的硬件或程序进程共享的数据存储区设置缓冲区,使对应的文本间隔相同的时间输出,让用户可以更清楚编辑文本的过程。fflush(stdin)
5、 ;/*清空输入缓冲区清除多余空格设计:文本中多余的空格符需要清除掉else if (ch =)空格(if(m !二)多余的空格不输入(if (w0+LineCur-l PageWedth) 判断是否超过页宽(if (LineNum+1 = PageLength-2)判断是否超过页长fputc C n , wf);LineNum+;continue;)fputs (n ,wf);LineCur = 1;LineNum+;for (int k = 1; k = w0; k+) 输入到文件流fputc(wk, wf);LineCur+;fputc (ch, wf);m =;j=l;LineCur+
6、;w0 = 0;主函数操作局部设计:void main()char i_f255, o_f255;char ch, m = n;int i = 0,j=0;当前行的游标当前页的当前行数当前页数int LineCur = 0, LineNum= 1, PageNum= 1;FILE *rf, *wf;SString w;w0 = 0;while(l) /翻开文件 (printf(”请输入需要格式化的文本文件名(可不输入.txt): ”); scanf(n%sn,i_f);if(!(rf=fopen(i_f;rH) (strcat(i_f, .txt1); if(!(rf=fopen(i_f,nr
7、n) (printf(”读取错误或文件不存在!请重新输入n”); continue;) )printf(”请输入保存文本的文件名(不用输入.txt): ”); scanf(n%s,o_f);strcat(o_f, n.txtH);if(!(wf = fopen(o_f,M w+n)(printf(读取错误 n”);)if(rf != NULL & wf !二 NULL) break;五、上机调试过程:图5. 1原doc文件里的文章上面为准备格式化的文本文件。运行文本格式化.exeThe TXT has been edited, please chack it in the new txt na
8、med new. txt-new.txt -记事本文件(E) 编辑(E) 格式9) 查看(y) 希助也)I sai/ to you today 9 my Friends, that in spite oF the diFFiculties an d Frustrations oF the moment:, I still haue a dream. 11 s a dream deeply rooted in the American drean.QI haue a dream that: one day this nation will rise up and liue ou七 the tru
9、e meaning oF its creed: We hold these truths to be selF-euident: that: all m en are created equal -*QI haue a dream that one day on the red hills oF Georgia the sons oF Fo rmer slaues and the sons oF Former slaueowners will be able to sit down togethe r at a table oF brotherhood.QI haue a dream that
10、 one day euen the state oF Mississippi v a desert st atze v sweltering with the heat oF in justice and oppression v viill be transForned into an oasis oF Freedom and justice -1 haue a dream that my Four children will one day liue in a nation whe re they will not be judged by the color oF their skin
11、but: by the content oF their character.1 haue a dream today.Ln 1, Col 1图5. 2转化后new文件里的文章六、测试结果及其分析:实验结果:输入文本按预定格式输出。分析:程序输出正常,但尚有一些需改进的地方,例如:文本输出中,作为换段符的本不 该存在(按实验要求保存下来,减轻了程序难度)。而后,分页时,最后缺乏页的文本也没 用分配成新的一页打上页码。最后,文本用到的参数都是预先写在程序之中,可以考虑由操 作者自行输入,只需在主函数中写入相关的提示及参数输入语句。编程过程中,编辑出的文 本较美观,不浪费空间。运行结果时直接出现最
12、终排版好的文本,这样让用户看出编辑结果, 感觉清空缓冲区比拟好,使用户比拟清楚的看到文本格式化的结果。七.用户使用说明书D:TDDOWNLOAD/fA3wMicrosott Visual StudioCommonMSDev98BinDebugi.1清输入需清输入需要格式化的文本文件名 请前人需要贽弟他的本文件名,(可不输入. txt: C : Soc 请刖人葆春文天的女碎名.本用输入. t xt: c : n e町Press any key to continue图7. 3格式化结束八、附源程序清单和运行结果附录一源程序ttinclude ttinclude # define PageLeng
13、th20define PageWedth80# define LeftMargin1ttdefine HeadingLength3# define FootingLength3define StaringPageNumber 1typedef char SString41; 。号单元存放串的长度void main()char i f255, o f255; char ch, m = n;int i = 0,j=0;int LineCur = 0,LineNum = 1,PageNum = 1;int i = 0,j=0;int LineCur = 0,LineNum = 1,PageNum =
14、 1;当前行的游标当前页的当前行数当前页数FILE *rf, *wf;SString w; w0= 0;while(1)翻开文件printf (请输入需要格式化的文本文件名(可不输入.txt):);scanf (%s,i_f);if(!(rf = fopen(i_f, r)(strcat (i_f, . txt);if (! (rf = fopen(i_f, r)(printf (读取错误或文件不存在!请重新输入n); continue;)printf (请输入保存文本的文件名(不用输入.txt):);scanf (s, o_f);strcat (o_f, . txt);if(!(wf = f
15、open(o_f, w+)(printf (读取错误n);)if(rf != NULL & wf != NULL)break;ch = fgetc(rf);while (ch != EOF) |if(LineNum = PageLength-2) (fputs(n,wf)fputc (PageNum/10+48, wf) fputc (PageNum%10+48, wf) fputs (nn, wf);LineCur = 0;LineNum = 1;读取第一个字符总循环到页尾是输入脚长空格数二页宽/2-1;十位数,int转char/个位数,int转char脚长PageNum+;if (LineCur =二 0 & LineNum 二二 1) 页头 (fputs (/nnn,, wf);头长fputcC , wf);左空白LineCur = 1;LineNum = 4;