📄 programanalys.cpp
字号:
// programanalys.cpp : Defines the entry point for the console application.
//
#define NULL 0
#include "stdafx.h"
//////////////////////输出预定义////////////////////
#define PRINTA(name) fprintf(outfile," 等级 A : 函数%s风格优!\n",name)
#define PRINTB(name) fprintf(outfile," 等级 B : 函数%s风格良!\n",name)
#define PRINTC(name) fprintf(outfile," 等级 C : 函数%s风格中!\n",name)
#define PRINTD(name) fprintf(outfile," 等级 D : 函数%s风格差!\n",name)
//////////////////////输出预定义////////////////////
//////////////////////全局变量定义////////////////////
int codelength=0; //代码行长
int commentlength=0; //注释行长
int blanklength=0; //空白行长
int functionnumber=0; //函数个数
int functionlength=0; //函数总长
int bracketnumber=0; //value=0:在函数外
int linestate=0; //空行为0,代码为1
int wait; //可能进入函数value=1
char infilename[30]="program.c"; //源文件
char outfilename[30]="output.txt"; //目标文件
FILE *infile,*outfile; //读写文件流
//////////////////////全局变量定义////////////////////
//////////////////////判别行是否注释行////////////////////
char comment()
{
char tempchar;
if('/'==fgetc(infile))
{
while(!feof(infile)&&10!=tempchar&&13!=tempchar)
{
tempchar=fgetc(infile);
}
if(!linestate)
{
commentlength++;
}
else
{
codelength++;
if(bracketnumber)
{
functionlength++;
}
}
if(feof(infile))linestate=2;
else linestate=0;
return tempchar;
}
else
{
wait=0;
linestate=1;
}
if(feof(infile))linestate=2;
return 0;
}
//////////////////////判别行是否注释行////////////////////
//////////////////////取字符并进行处理////////////////////
void getlinechar()
{
char nowchar=0,lastchar=0;
while(!feof(infile))
{
if(10!=lastchar&&13!=lastchar)lastchar=0;
nowchar=fgetc(infile);
switch(nowchar)
{
case ' ' :break;
case 9 :break;
case '/' :lastchar=comment();break;
case ')' :if(!bracketnumber)wait=1;linestate=1;break;
case '}' :if(bracketnumber)bracketnumber--;linestate=1;break;
////////////////////////若读到‘{’进行处理///////////////////////////////////////
case '{' :
if(bracketnumber||wait)
{
if(!bracketnumber)
{
functionnumber++;
wait=0;
}
bracketnumber++;
}
linestate=1;break;
////////////////////////若读到“回车符”进行处理///////////////////////////////////////
case '\n' :
if(13==lastchar)
{lastchar=0;linestate=0;break;}
if(linestate)
{
codelength++;
if(bracketnumber)
{
functionlength++;
}
}
else
{
blanklength++;
}
linestate=0;lastchar=10;break;
////////////////////////若读到“换行符”进行处理///////////////////////////////////////
case 13 :
if(10==lastchar)
{lastchar=0;linestate=0;break;}
if(linestate)
{
codelength++;
if(bracketnumber)
{
functionlength++;
}
}
else
{
blanklength++;
}
linestate=0;lastchar=13;break;
default :linestate=1;
}
}
if(10!=nowchar&&13!=nowchar&&1==linestate)
{
codelength++;
}
if(10!=nowchar&&13!=nowchar&&0==linestate)
{
blanklength++;
}
return;
}
//////////////////////取字符并进行处理////////////////////
int main(int argc, char* argv[])
{
char filename[30];
int sum,code,comment,blank;
float function;
////////////////////////打开源文件///////////////////////////////////////
printf("请输入待测程序名(文件不存在则为program.c):");
scanf("%s",filename);
if((infile=fopen(filename,"r"))==NULL)
{
infile=fopen(infilename,"r");
}
else
{
strcpy(infilename,filename);
}
////////////////////////打开目标文件///////////////////////////////////////
printf("请指定结果输出的文件名(文件创建或打开失败则为output.txt):");
scanf("%s",filename);
if((outfile=fopen(filename,"w"))==NULL)
{
outfile=fopen(outfilename,"w");
}
else
{
strcpy(outfilename,filename);
}
getlinechar();
fclose(infile);
sum=codelength+commentlength+blanklength;
code=codelength*100/sum;
comment=commentlength*100/sum;
blank=100-code-comment;
function=(float)functionlength/(float)functionnumber+1;
fprintf(outfile,"程序代码共 %d 行\n",codelength);
fprintf(outfile,"程序注释共 %d 行\n",commentlength);
fprintf(outfile,"程序空白行共 %d 行\n",blanklength);
fprintf(outfile,"程序代码占 %%%d \n",code);
fprintf(outfile,"程序注释占 %%%d \n",comment);
fprintf(outfile,"程序空白行占 %%%d \n",blank);
fprintf(outfile,"程序共 %d 个函数\n",functionnumber);
fprintf(outfile,"函数的平均长度为 %.1f 行\n",function);
if(function>=10&&function<=15)
PRINTA("长度");
else
if((function>=8&&function<=9)||(function>=16&&function<=20))
PRINTB("长度");
else
if(function>24||function<5)
PRINTD("长度");
else
PRINTC("长度");
if(comment>=15&&comment<=25)
PRINTA("注释");
else
if((comment>=10&&comment<=14)||(comment>=26&&comment<=30))
PRINTB("注释");
else
if(comment>35||comment<5)
PRINTD("注释");
else
PRINTC("注释");
if(blank>=15&&blank<=25)
PRINTA("空白");
else
if((blank>=10&&blank<=14)||(blank>=26&&blank<=30))
PRINTB("空白");
else
if(blank>35||blank<5)
PRINTD("空白");
else
PRINTC("空白");
fclose(outfile);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -