数据结构课程设计

您所在的位置:网站首页 c程序分析题如何做的 数据结构课程设计

数据结构课程设计

2024-07-02 01:48| 来源: 网络整理| 查看: 265

4题 程序分析

班级:-------------  姓名:------  学号:-------------------------------------   完成日期:----------

【问题描述】

读入一个C程序,统计程序中的代码、注释、和空行的行数以及函数的个数和平均行数,并利用统计信息分析评价该程序的风格。

【基本要求】

把C程序文件按字符顺序读入源程序;边读入程序、边识别统计代码行、注释行、空行,同时还要识别函数的开始和结束,以便统计其个数和平均行数;程序的风格评分分为代码、注释和空行三个方面、每个方面分为A、B、C和D四个等级,划分标准为:

 

A级

B级

C级

D级

代码(函数平均长度)

10~15行

8~9或16~20行

8~9或16~20行

8~9或16~20行

注释(占总行比率)

15~25%

10~14或26~30%

5~9或31~35%

35%

空行(占总行比率)

15~25%

10~14或26~30%

5~9或31~35%

35%

 

【测试数据】

先对较小的程序进行分析。当年的程序能正确运行时,对你的程序本身进行分析。

【实现提示】

为了实现的方便,可做以下的约定:

头两个字符是“//”的行称为注释行(该行不含语句)。除了空行和注释行外,其余均为代码行(包括类型定义、变量定义和函数头)。每个函数代码的行数(除去空格和注释行)称为该函数的长度。每行最多只有一个“{”、“}”、“switch”和“struct”(便于识别函数的结束行)。

【选作内容】

报告函数的平均长度。找出最长函数及其在程序中的位置。运行函数的嵌套定义,报告最大的函数嵌套深度。  代码: #include #include #include #include #include #include #include #define max 10000 //正则表达式头文件 using namespace std; typedef struct node; typedef node *tree; int graph[max][max]; //函数嵌套定义邻接矩阵 int num[max]; int visited[max]; //打开文件名 string name ("test.cpp"); //括号匹配 int kuohao = 0; //函数长度计数开关 int flag = 0; //函数行数 int fun = 0; //代码总行数 int line = 0; struct result { //代码行 int Code = 0; //注释行 int Comments = 0; //空行 int Blanklines = 0; int scode = 0; int scomment = 0; int sspace = 0; //函数个数 int countfun = 0; //函数总长度 int funlen = 0; //最长函数 int maxlen = 0; //函数平均长度 double avelen = 0; //最长函数所在行数 int maxline = 0; //记录函数名 int maxh = 0; string funname[max]; //最长函数名 string maxfun; }list; /*根据获取到的数据,对程序进行评分*/ string evaluate(result l) { string value; if (l.avelen >= 10 && l.avelen = 8 && l.avelen < 10) || (l.avelen > 15 && l.avelen = 5 && l.avelen < 8) || (l.avelen > 20 && l.avelen = 15 && list.scomment = 10 && list.scomment < 15) || (list.scomment > 25 && list.scomment = 5 && list.scomment < 10) || (list.scomment > 30 && list.scomment = 15 && list.sspace = 10 && list.sspace < 15) || (list.sspace > 25 && list.sspace = 5 && list.sspace < 10) || (list.sspace > 30 && list.sspace list.maxlen) { list.maxlen = fun; list.maxfun = list.funname[list.countfun]; list.maxline = line-fun+1; } fun = 0; flag = 0; } } if (regex_search(s, regex("//"))) //注释行搜索; list.Comments++; else if (s == "") //空行搜索 list.Blanklines++; else { if (flag) { for (int i = 1; i < list.countfun; i++) { if (regex_search(s,regex(off(list.funname[i]))) != NULL) { graph[list.countfun][++num[list.countfun]] = i; } } list.funlen++; } list.Code++; } if (flag) { fun++; } } /*通过深度优先遍历寻找函数的最大深度*/ void dfs(int i) { static int count; visited[i] = 1; for (int j = 1; j list.maxh) { list.maxh = count + 1; } count = 0; } } /*展示面板*/ void show() { //计算每个函数的最大镶嵌深度 for (int i = 1; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3