统计程序中各种代码行数的课程设计.doc_第1页
统计程序中各种代码行数的课程设计.doc_第2页
统计程序中各种代码行数的课程设计.doc_第3页
统计程序中各种代码行数的课程设计.doc_第4页
统计程序中各种代码行数的课程设计.doc_第5页
免费预览已结束,剩余11页可下载查看

下载本文档

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

文档简介

需求分析1) 问题描述读入一个C程序,统计程序中代码、注释和空行数以及函数的个数和平均行数,并利用统计信息分析评价该程序风格。2) 基本要求如下:(1)、把C程序文件按字符顺序读入源程序;(2)、边读入程序,边识别统计代码行、注释行和空行,同时还要识别函数的开始和结束,以便统计其个数及平均行数。(3)、程序风格分为代码注释和空行三方面。每方面分A、B、C、D四个等级。AB C D代码(函数的平均长度) 1015行89或1620 57或2124 24 注释(占总行数比例) 1525%1014或2630% 59或3135% 35% 空行(占总行数比率) 1525%1014或2630% 59或3135% 35%3)输入输出范例以下是对程序文件ProgAnal.C分析的输出结果示例: The results of analysing program file ProgAnal.C: Lines of code :180 Lines of comments: 63 Blank lines: 52 Code Comments Space 61% 21% 18% The program includes 9 functions. The average length of a section of code is 12.9 lines. Grade A: Excellent routine size style. Grade A: Excellent commenting style. Grade A: Excellent white space style.1. 概要设计1).头文件引用与宏定义:#include #include #include #define TRUE 1#define FALSE 0#define BOOL int#define MAXSIZE 5000#define COUNT20/可以统计的最大的文件个数#define LEN 20 / 文件名的最大长度2).所用存储结构/函数属性结构typedef struct char filename20; /每一个函数的名字int length; /每一个函数的长度int pos;/每一个函数的位置Fun;/统计结构的声明typedef struct int comments; /纯注释的个数int comment; /混合注释个数int blank;/空行的个数Fun funMAXSIZE;/函数的属性int others;/除去函数中代码外其余的代码个数int funcount; /函数的个数Analy;2. 详细设计1).函数功能及声明BOOL StrEmpty(char *s)/S是不是空int Find(char *s1,char *s2)/查找S1中是否有值为S2的子串void HaveLine(FILE *fp,char *s)/重文件中获取一行char* IgnoreB(char *s)/截断一行的空字符int IsCom(char *s)/判断一行是不是注释BOOL IsBlank(char *s)/判断一行是不是空格BOOL IsFunB(char *s)/判断一行是否是函数的开头void PrintMax(Analy *An)/打印最大函数的信息void printR(int aver ,int comc,int blanks )/打印代码风格级别void print(Analy *An)/void checkfile(char *filename,int i)/检测文件是否存在BOOL GetIn(int *n)/规范输入的数据,只能为数字void analy(char filenameCOUNTLEN,int n)/分析单个文件void savelog()/保存日志2)详细源代码/检测是否为空串BOOL StrEmpty(char *s)if(s0=0)return TRUE;return FALSE;/查看S1中是否有值为S2的子串,若有则返回第一个子串的位置,若无则返回-1;int Find(char *s1,char *s2)int i = 0,j = 0;if(strlen(s1) pos1 & posc j ? j : i;else if (i = -1)i = j;else if (1 = -1 & j = -1)return FALSE;if (i pos)return FALSE;s = &si;s = IgnoreB(s);pos2 = Find(s,();if(*s = 0 | *s = ( |pos2 pos)return FALSE;return TRUE;/打印最大的函数属性void PrintMax(Analy *An)/FILE *fp;int i,j = 0;/ int len;for(i = 1;i funcount ;i+)if(An-funj.length funi.length)/找出最大的函数的位置j = i;if(An-funj.length funj.pos funj.length);/打印长度printf(The pos of lengthest function is in %s the %dth linen,An-funj.filename,An-funj.pos);/打印出函数所在的文件和位置printf(The length of lengthest function have %d lines n,An-funj.length);/打印长度void printR(int aver ,int comc,int blanks )/按代码级别判定标准输出分析结果int i;char Grade415 = Excellent,Good,So-So,Bad;/定义四个级别段/判定代码的级别if (aver =10)i = 0;else if(aver = 16) | aver = 8)i = 1;else if(aver = 5) | aver =21)i = 2;else if (aver 24)i = 3;printf(Grade %s routine code stylen,Gradei);/判定注释的级别if (comc= 15)i = 0;else if(comc = 10) | comc =26)i = 1;else if(comc = 5) | comc =31)i = 2;else if(comc 35)i = 3;printf(Grade %s routine commenting stylen,Gradei);/判定空行的级别if (blanks = 15)i = 0;else if(blanks = 10) | blanks =26)i = 1;else if(blanks = 5) | blanks =31)i = 2;else if(blanks 35)i = 3;printf(Grade %s white space stylen,Gradei);/打印输出结果void print(Analy *An)int sum = 0,funcode = 0;int i, comc , blanks, aver ,code;for(i = 0;i funcount ;i+)/求函数的代码总数funcode += An-funi.length;/求所有的代码总数sum += An-blank;sum += An-comments;sum += An-comment;sum += An-others;sum += funcode;if(sum = 0)/防止除数sum为0sum = 1;if(An-funcount = 0)/防止除数m为0aver = 0;elseaver = funcode/An-funcount;comc = (An-comments + An-comment)*100/sum;blanks = (An-blank)*100)/sum;code = 100 - comc - blanks; /(funcode + An-others)*100)/sum;printf(The results of analysing program file:nn);printf(Lines of code:%dn,sum - An-blank - An-comment - An-comments);printf(Lines of comments:%dn,An-comments + An-comment);printf(Blank lines:%dn,An-blank);printf(CodeCommentsSpacen);printf(_n);printf(-n);printf(%d% %d%d%n,code,comc,blanks);printf(The program includes %d functionsn,An-funcount);printf(The average length of section of function is %dn,aver);PrintMax(An);printf(n);/按代码级别判定标准输出分析结果printR(aver,comc,blanks);void checkfile(char *filename,int i)FILE *fp;while(fp = fopen(filename,r) = NULL)printf(文件不存在 %sn,filename);printf(n请重新输入第%d个源文件: ,i+1);scanf(%s,filename);BOOL GetIn(int *n)/规范输入的数据,只能为数字char c;*n = 0;fflush(stdin);c = getchar();/当输入一串数据并按回车后,getchar()取出缓存队列中的第一个字符while(c != n)if(c = 0)printf(输入有误!请重新输入.n);if(c = 0 & c = 9)*n = (*n) * 10 +c - 48;elseprintf(输入有误!请重新输入.n);fflush(stdin);/清空(刷新)缓存里的内容,以防被下次getchar()取用return FALSE;c = getchar();return TRUE;void analy(char filenameCOUNTLEN,int n)FILE *fp;/分析源文件指针FILE *fpp;/日志文件指针Analy An;/程序统计结构体char s200;/存储每行的文件BOOL begin = 0,start = 0;/设置函数开始标记int i,j = -1,pos = 0;/函数的位置 长度信息/c检测函数内大括号的匹配,comtype是注释的类型int c=0,comtype;An.blank = 0;Aments = 0;Ament = 0;An.others = 0;An.funcount = 0;if(fpp = fopen(log.txt,w) = NULL)/建立日志文件printf(cannot open the file %sn,filenamei);for (i = 0 ;i n; i+)/遍历所有的文件if(fp = fopen(filenamei,r) = NULL)printf(cannot open the file %sn,filenamei);getchar();exit(0);pos = 0; /函数在新一个文件中的位置初始化while (!feof(fp)HaveLine(fp,s); /从文件中读取一行数据pos+; /每个函数在文件中开始的位置/分类统计文件中的代码个数comtype = IsCom(s); if (comtype =1)Ament+;continue;if (comtype = 2)Aments+;if (IsFunB(s)fprintf(fpp,%s n,s);/提取每个函数的名字写入到文件j+; /j为函数的个数-1;c = 0;/大括号个数初始化为0begin = TRUE;strcpy(An.funj.filename,filenamei); /记录函数在哪个文件中An.funj.pos = pos;An.funj.length = 1;else if(IsBlank(s) An.blank+;else if(begin) An.funj.length+;if (Find(s,) != -1) /检测是否进入到了函数体内c+;start = TRUE;if (Find(s,) != -1) /检测是否一个函数的结束c-;if (c=0 & start)begin = FALSE;start = FALSE;else An.others+;fclose(fp);/关闭分析文件fclose(fpp);/关闭日志文件An.funcount = j+1; /把函数的个数保存print(&An); /打印分析结果3)主函数void main()int n,i;char c;char filenameCOUNTLEN;printf(t-n);printf(t 计算机学院网络工程三班*-*张菲*-*学号3107007062n);printf(t-n);while(1)n=0;printf(tttIII-程序分析-IIIn);printf(请输入要分析的源文件的个数: );while(!GetIn(&n) | n = 0)printf(请输入要分析的源文件的个数: );for(i = 0 ;i n ;i+)printf(n请输入第%d个源文件: ,i+1);scanf(%s,filenamei);checkfile(filenamei,i);fflush(stdin);analy(filename,n);printf(t*是否继续使用? Y/N*n); c = getchar();if(c = Y | c = y)continue;break; 3. 调试分析1) 经验收获这个课程设计总体上难度不大,但细节问题很多,尤其是频繁的用到了文件的读写操作,牵扯到了关于缓存的一些细节问题,通过这次的课程设计,使我对C语言有了较深刻的了解,学习了很多以往不知道了语言特性,接触到了很多新的功能强大的函数,特别是对文件的读写有了比较熟练的掌握,知道了其基本的原理,与控制方法,本课程设计的一个难点是关于函数开头的判断问题,尝试了很多种的方法但是都不尽人意,最后经过大量数据的测试,利用了函数定义特性的格式与关键字排除方法相结合,基本上能应对常见的程序。在完成课程设计的过程中还有一点较大的收获是对编程环境的进一步的认识,这次编程用的是教熟悉的VC,以往用过TC,他们有很多的不同的地方,例如有写TC中可以用的函数,特别是在COIN.H目

温馨提示

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

最新文档

评论

0/150

提交评论