代码注释行数估算(代码注释行数估算)
admin 发布:2022-12-19 03:45 105
本篇文章给大家谈谈代码注释行数估算,以及代码注释行数估算对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、c语言代码统计工具 怎样统计一个文件夹中所有txt文件每个文件的总代码行、行内注释行和空行数?
- 2、跪求:用[C++]编一个程序用于计算代码行数
- 3、计算代码行数
- 4、统计C++源程序中代码行数,注释行数,注释率。
- 5、程序员写过的代码行数是怎么统计的
- 6、问题描述]读入一个C程序,统计程序中代码、注释
c语言代码统计工具 怎样统计一个文件夹中所有txt文件每个文件的总代码行、行内注释行和空行数?
搜索到个代码统计工具,
未曾使用,可以试用下。
如果使用VC的话,有行数统计插件LineCounter,网址为:
我正在使用中,较为实用,缺点是只能用于VC。
跪求:用[C++]编一个程序用于计算代码行数
修改过了,已经去除了空格,回车等空行
但我觉得注释也应该算代码行的,如果,你想把注释也不算的话,自己在改一改吧。
#include stdio.h
#include memory.h
#include string.h
int main(int argc, char* argv[])
{
FILE *fp;
unsigned char temp,temp1,temp2,temp4;
temp='\0';
temp1='\0';
temp2='\0';
temp4='\0';
int total = 0; //统计数
int bj = 0;
int bakbj=0;
int singlebj=0;
int start=0;
if (argc==1)
{
printf("使用说明!\n");
}
else if(argc==2)
{
fp=fopen(*(argv+1),"rb+");
if (fp!=NULL)
{
for(int i=0;;i++)
{
fseek(fp,i,SEEK_SET);
fread(temp,1,1,fp);
if (!feof(fp)) //正确读出数据
{
if (temp!='\n'temp!=32temp!=13temp!=10temp!=10temp!=47temp!=42)
{
bj=1; //表示要统计的行
}
}
else
{
break;
}
//进行统计
if (temp=='\n') //一行结束
{
if (bj==1) //当一行结束时,开始统计
{
total++;
}
bj=0;
singlebj=0;
}
}
if (temp!='\n') //最后一行不以回车结束,则上面没统计
{
total++;
}
printf("合计行数%d\n",total);
}
else
{
printf("File no Find!");
}
}
else
{
printf("Parameter Error!\n");
}
return 0;
}
计算代码行数
/* 很早以前写的程序了,数的实际是分号、#号的个数
* 在命令行下使用。如程序编译为a.exe,则输入a *.c *.h就可以了
*/
#includestdio.h
#includestdlib.h
int main(int argc, char *argv[])
{
int i;
int nfile = 0;
int c;
int nsimicolon = 0, nsharp = 0;
FILE *fp;
if (argc == 1) {
printf("Usage: %s filename1 [filename2 ...]\n", argv[0]);
system("pause");
return 0;
}
for (i = 1; i argc; ++i) {
fp = fopen(argv[i], "r");
if (fp) {
do {
c = getc(fp);
if (c == ';')
nsimicolon++;
if (c == '#')
nsharp++;
}
while (c != EOF);
fclose(fp);
printf("%s\n", argv[i]);
nfile++;
}
else;
}
printf("\nnfile = %d\n...; = %d\n#... = %d\n", nfile, nsimicolon,
nsharp);
return 0;
}
统计C++源程序中代码行数,注释行数,注释率。
文件读入
如果前两个字符不为“//”或空格则为实际代码
如果前两个字符为“//”则为注释
注释行/(注释+代码)就是注释率
程序员写过的代码行数是怎么统计的
程序员写过的代码行数是怎么统计的
StatSVN统计的是所有代码行,包括注释和空行,但一般度量要求是有效代码行,在分析时需要注意这一点。 StatSVN不考虑修改的代码行数,只考虑与上一版本相比新增(+)与删除(-)的代码行数。
问题描述]读入一个C程序,统计程序中代码、注释
#includestdio.h
#includestdlib.h
#includestring.h
#define TRUE 1
#define FALSE 0
#define BOOL int
#define MAXSIZE 5000
#define COUNT 20 // 可以统计的最大的文件个数
#define LEN 20 // 文件名的最大长度
#includestdio.h
#includestdlib.h
#includeconio.h
voidcheckfile(char *filename,int i);
voidanaly(char filename[COUNT][LEN],int n);
BOOLGetIn(int *n);
//函数属性结构
typedefstruct {
char filename[20]; //每一个函数的名字
int length; //每一个函数的长度
int pos; //每一个函数的位置
}Fun;
//统计结构的声明
typedefstruct {
int comments; //纯注释的个数
int comment; //混合注释个数
int blank; //空行的个数
Fun fun[MAXSIZE]; //函数的属性
int others; //除去函数中代码外其余的代码个数
int funcount; //函数的个数
}Analy;
//串的基本操作ADT
//检测是否为空串
BOOLStrEmpty(char *s)
{
if(s[0]=='\0')
return TRUE;
return FALSE;
}
//查看S1中是否有值为S2的子串,若有则返回第一个子串的位置,若无则返回-1;
intFind(char *s1,char *s2)
{
int i = 0,j = 0;
if(strlen(s1) strlen(s2))
return -1;
while (s1[i]!='\0')
{
if(s1[i] == s2[j])
{
i++;
j++;
if(s2[j]=='\0')
return i-j;
continue;
}
i++;
j=0;
}
return -1;
}
//读取文件中的一行字符
voidHaveLine(FILE *fp,char *s)
{
while(!feof(fp))
{
*s = fgetc(fp);
if(*s=='\n'){ //若是一行的结尾则表示取完了一行
*s='\0';
return;
}
s++;
}
*s = '\0';
}
//忽略一行字符开头的空格和tab,返回截断后上的串指针
char*IgnoreB(char *s)
{
while (*s== ' ' || *s== ' ')
s++;
return s;
}
//判断一行字符是不是注释
intIsCom(char *s)
{
int posc,pos1,pos2;
s= IgnoreB(s);
posc = Find(s,"//");
if(posc == 0)//此行仅有注释,无代码;
return 1;
if(posc == -1)
return 0;
pos1 = Find(s,"\"");
pos2 =Find(s[pos1+1],"\"");
if(posc pos1 posc pos2)
return 0;
return 2;
}
//判断一行字符是不是空白
BOOLIsBlank(char *s)
{
s= IgnoreB(s);
if(*s== '\0')
return TRUE;
return FALSE;
}
intIsFunB(char *s)
{
int i,j,pos,pos2;
//有分号,if while for的不是函数开头
if(Find(s,";") != -1 ||Find(s,"if") != -1 || Find(s,"for") != -1 ||Find(s,"while") != -1||Find(s,"switch") != -1)
return FALSE;
//没有小括号的不是函数开头
if((pos = Find(s,"(")) == -1)
return FALSE;
s = IgnoreB(s);
i = Find(s," ");
j = Find(s," ");
if (i != -1 j != -1)
i = i j ? j : i;
else if (i == -1)
i = j;
else if (1 == -1 j == -1)
return FALSE;
if (i pos)
return FALSE;
s = s[i];
s = IgnoreB(s);
pos2 = Find(s,"(");
if(*s == '\0' || *s == '(' ||pos2 pos)
return FALSE;
return TRUE;
}
voidprintR(int aver ,int comc,int blanks )
{
//按代码级别判定标准输出分析结果
int i;
char Grade[4][15] = {"A:Excellent","B: Good","C: just So-So","D:Bad"};//定义四个级别段
//判定代码的级别
if (aver = 15 aver =10)
i = 0;
else if((aver =20 aver =16) || aver = 9 aver = 8)
i = 1;
else if((aver =7 aver =5) || aver = 24 aver =21)
i = 2;
else if ((aver 5) || (aver 24))
i = 3;
printf(" Grade %s routine code style\n",Grade[i]);
//判定注释的级别
if (comc= 25 comc = 15)
i = 0;
else if((comc =14 comc =10) || comc = 30 comc =26)
i = 1;
else if((comc =9 comc =5) || comc = 35 comc =31)
i = 2;
else if((comc 5) || (comc 35))
i = 3;
printf(" Grade %s routine commenting style\n",Grade[i]);
//判定空行的级别
if (blanks = 25 blanks =15)
i = 0;
else if((blanks =14 blanks= 10) || blanks = 30 blanks =26)
i = 1;
else if((blanks =9 blanks= 5) || blanks = 35 blanks =31)
i = 2;
else if((blanks 5) || (blanks 35))
i = 3;
printf(" Grade %s white spacestyle\n",Grade[i]);
}
//打印输出结果
voidprint(Analy *An)
{
int sum = 0,funcode = 0;
int i, comc , blanks, aver ,code;
for(i = 0;i An-funcount ;i++)//求函数的代码总数
funcode += An-fun[i].length;
//求所有的代码总数
sum += An-blank;
sum += An-comments;
sum += An-comment;
sum += An-others;
sum += funcode;
if(sum == 0)//防止除数sum为0
sum = 1;
if(An-funcount == 0)//防止除数m为0
aver = 0;
else
aver = 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(" 程序源代码分析结果如下所示\n\n");
printf(" 代码行数: %d\n",sum -An-blank - An-comment - An-comments);
printf(" 注释行数: %d\n",An-comments +An-comment);
printf(" 空行数: %d\n",An-blank);
printf(" 代码 注释 空行\n");
printf(" ***** ****** ******\n");
printf(" ***** ****** ******\n");
printf(" %d%% %d%% %d%%\n",code,comc,blanks);
printf(" The program includes %d functions\n",An-funcount);
printf(" The average length of section of function is %d\n",aver);
printf("\n");
//按代码级别判定标准输出分析结果
printR(aver,comc,blanks);
}
voidcheckfile(char *filename,int i)
{
FILE *fp;
while((fp = fopen(filename,"r"))== NULL)
{
printf("对不起!文件不存在%s\n",filename);
printf("\n请重新输入第%d个源文件: ",i+1);
scanf("%s",filename);
}
}
BOOLGetIn(int *n)//规范输入的数据,只能为数字
{
char c;
*n = 0;
fflush(stdin);
c = getchar();//当输入一串数据并按回车后,getchar()取出缓存队列中的第一个字符
while(c != '\n'){
if(c == '0' n == 0)
printf("输入有误!请重新输入....\n");
if(c = '0' c = '9')
*n = (*n) * 10 +c - 48;
else{
printf("输入有误!请重新输入....\n");
fflush(stdin);//清空(刷新)缓存里的内容,以防被下次getchar()取用
return FALSE;
}
c = getchar();
}
return TRUE;
}
voidanaly(char filename[COUNT][LEN],int n)
{
FILE *fp;//分析源文件指针
FILE *fpp;//日志文件指针
Analy An;//程序统计结构体
char s[200];//存储每行的文件
BOOL begin = 0,start = 0;//设置函数开始标记
int i,j = -1,pos = 0;//函数的位置 长度信息
//c检测函数内大括号的匹配,comtype是注释的类型
int c=0,comtype;
An.blank = 0;
An.comments = 0;
An.comment = 0;
An.others = 0;
An.funcount = 0;
if((fpp =fopen("log.txt","w")) == NULL)//建立日志文件
printf("cannot open the file%s\n");
for (i = 0 ;i n; i++)//遍历所有的文件
{
if((fp =fopen(filename[i],"r")) == NULL)
{
printf("cannot open the file%s\n",filename[i]); //输出filename[i]
getchar();
exit(0);
}
pos = 0; //函数在新一个文件中的位置初始化
while (!feof(fp))
{
HaveLine(fp,s); //从文件中读取一行数据
pos++; //每个函数在文件中开始的位置
//分类统计文件中的代码个数
comtype = IsCom(s);
if (comtype ==1)
{
An.comment++;
continue;
}
if (comtype == 2)
An.comments++;
if (IsFunB(s))
{
fprintf(fpp,"%s\n",s);//提取每个函数的名字写入到文件
j++; //j为函数的个数-1;
c = 0;//大括号个数初始化为0
begin = TRUE;
strcpy(An.fun[j].filename,filename[i]);//记录函数在哪个文件中
An.fun[j].pos = pos;
An.fun[j].length = 1;
}
else if(IsBlank(s))
An.blank++;
else if(begin){
An.fun[j].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); //打印分析结果
}
voidmain()
{
int n,i;
char c;
char filename[COUNT][LEN];
printf("\t-------------------------------------------------------------\n");
printf("\t 程序源代码分析软件 BY 沈航电子信息工程学院--王盼 \n");
printf("\t--------------------------------------------------------------\n");
while(1){
n=0;
printf("\t\t\t**********程序分析*********\n");
printf("请输入您要分析的源文件的个数: ");
while(!GetIn(n) || n = 0){
printf("请输入您要分析的源文件个数:");
}
for(i = 0 ;i n ;i++)
{
printf("\n请输入第%d个源文件: ",i+1);
scanf("%s",filename[i]);
checkfile(filename[i],i);
fflush(stdin);
}
analy(filename,n);
printf("\t********您需要继续使用本软件吗?Y/N***********\n");
c =getchar();
if(c == 'Y' || c == 'y')
continue;
break;
}
}
关于代码注释行数估算和代码注释行数估算的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;
相关推荐
- 05-09网页代码,网页代码快捷键
- 05-06单页网站的代码(完整的网页代码)[20240506更新]
- 05-06个人主页图片代码(个人主页图片代码怎么弄)[20240506更新]
- 05-06提取微信名片代码(微信名片信息提取)[20240506更新]
- 05-06php后台权限管理代码(php管理员权限)[20240506更新]
- 05-06付费观看代码php(付费观看代码)[20240506更新]
- 05-06在线html执行代码(html怎么运行)[20240506更新]
- 05-06源代码管理资源管理器(资源管理器运行代码)[20240506更新]
- 05-06代码源软件库(程序代码库)[20240506更新]
- 05-06点击弹出密码代码(点击弹出密码代码错误)[20240506更新]
取消回复欢迎 你 发表评论:
- 标签列表
- 最近发表
- 友情链接