?? sum.txt
字號:
# include <stdio.h>
# include <stdlib.h>
# define SIZEMAX 500
# define LEN sizeof(struct rowlist)
struct rowlist{
int row;
int character;
char ch[SIZEMAX];
};
struct linkst {
struct rowlist date[SIZEMAX + 1];
int mrow;
};
//字符串定義,將整個程序以這種數據結構的形式存儲
typedef struct ElemType{
char c;
int row;
}ElemType;
struct sqstack{
struct ElemType * base;
struct ElemType * top;
int stacksize;
};
//棧定義,在本程序中用于計算函數個數時用,存儲的是“{”和“}”
int funnum(struct linkst list)
//計算程序中所包含的函數個數
{
int i,j,t,w=0;
struct sqstack s;
for(i=1;i<=list.mrow;i++)
for(j=0;j<list.date[i].character;j++)
if(list.date[i].ch[j]=='{') w++;
return(w);
}
int accountfun(struct linkst list)
//計算程序中每個函數所占行數
{
int i,j,t,w=0,q,n;
q=funnum(list);
struct sqstack s;
s.base=(ElemType *)malloc(100 * sizeof(ElemType));
s.top=s.base;
s.stacksize=100;
s.top->c='@';
s.top->row=0;
for(i=1;i<=list.mrow;i++){
for(j=0;j<list.date[i].character;j++){
if(list.date[i].ch[j]=='{'){
s.top->c=list.date[i].ch[j];
s.top->row=list.date[i].row;
s.top++;
}
if(list.date[i].ch[j]=='}'){
w++;
if(s.top != s.base){
s.top--;
t=list.date[i].row-s.top->row+1;
n=q-w+1;
printf("第%d個函數,共%d行\n",n,t);
}
}
}
}
return(w);
}
float CountFunAver(int sum,int funnumber)
//計算程序中函數的平均行數
{
float average,sum1;
sum1=(float) sum;
average=sum1 / funnumber;
return average;
}
void JudgeFun (float codeaver,float remark,float blank,float total)
//評價函數風格
{
float x,y,z,x1,y1;
z=(codeaver / total) * 100;
x=remark /total;
y=blank /total;
x1=100 * x;
y1=100 * y;
printf("Code Comments Space\n\n");
printf("======== ============= ============\n\n");
printf("%7.2f%%",z);
printf(" %7.2f%%",x1);
printf(" %7.2f%%\n",y1);
if(codeaver<=15.5 && codeaver>=9.5) printf("代碼等級為A\n");
else if(codeaver>=7.5 && codeaver<9.5 || (codeaver<=20.5 && codeaver>15.5))printf("代碼等級為B\n");
else if((codeaver>=5.0 && codeaver<7.5) || (codeaver>20.5 && codeaver<=24.0)) printf("代碼等級為C\n");
else if(codeaver>24.0 ||codeaver<5.0) printf("代碼等級為D\n");
//估計函數代碼風格等級
if(x<=0.25 && x>=0.15) printf("注釋比率等級為A\n");
else if((x<0.15 && x>=0.10) || (x<=0.30 && x>0.25))printf("注釋比率等級為B\n");
else if((x<0.10 && x>=0.05) || (x<=0.35 && x>0.30))printf("注釋比率等級為C\n");
else if(x<0.05 || x>0.35) printf("注釋比率等級為D\n");
//估計函數注釋行代碼等級
if(y<=0.25 && y>=0.15)printf("空行比率等級為A\n");
else if((y<0.15 && y>=0.10) || (y<=0.30 && y>0.25)) printf("空行比率等級為B\n");
else if((y<0.10 && y>=0.05) || (y<=0.35 && y>0.30)) printf("空行比率等級為C\n");
else if(y<0.05 || y>0.35)printf("空行比率等級為D\n");
//估計函數空行代碼等級
}
main()
{
int j=0,i,funnumber,blank=0,remark=0,sum=0;
float remark1,blank1,sum1,total;
float average;
FILE *fp;
char c1,c2;
struct linkst list;
i=1;
printf("為了測試程序正確與否方便,該程序自帶一個待測試的程序,可以選擇自行輸入或用本程序所自帶的測試程序進行測試\n");
printf("若想用本程序自帶的文件程序測試請按Y,否則按N\n");
c2=getchar();
switch(c2)
{
case 'N':
// 自行輸入程序進行測試
(list.date[i]).ch[0]=getchar();
printf("輸入的函數請以#結束\n");
while( ( (list.date[i]).ch[0] ) !='#'){
//創建程序
list.date[i].row=i;
while((list.date[i]).ch[j] != '\n' && (list.date[i]).ch[j] != '#'){
j++;
(list.date[i]).ch[j]=getchar();
}
(list.date[i]).character=j;
j=0;
i++;
(list.date[i]).ch[j]=getchar();
}
list.mrow=i-1;
break;
case 'Y':
// 用本程序自帶的待測程序進行測試
if((fp=fopen("sum.txt","r"))==NULL){
printf("Can't open the file!\n");
exit(0);
}
(list.date[i]).ch[0]=fgetc(fp);
while( ( (list.date[i]).ch[0] ) !='#'){
//創建程序
list.date[i].row=i;
while((list.date[i]).ch[j] != '\n' && (list.date[i]).ch[j] != '#'){
j++;
(list.date[i]).ch[j]=fgetc(fp);
}
(list.date[i]).character=j;
j=0;
i++;
(list.date[i]).ch[j]=fgetc(fp);
}
list.mrow=i-1;
break;
}
for(i=1;i<=list.mrow;i++)
if((list.date[i]).ch[0]=='\n') blank++;
//計算空行的行數
else if((list.date[i]).ch[0]=='/' && (list.date[i]).ch[1]=='/') remark++;
//計算注釋行的行數
funnumber=accountfun(list);
//每個函數所占的長度,返回函數的個數
sum=list.mrow - blank - remark;
//計算代碼的在程序中所占的行數
printf("Lines of code : %d\n",sum);
printf("Lines of comments: %d\n",remark);
printf("Blank lines: %d\n\n",blank);
average=CountFunAver(sum,funnumber);
//返回程序中函數的平均行數
printf("The program includes %d functions.\n",funnumber);
printf("The average length of a section of code is %7.2f lines\n\n",average);
total=(float)list.mrow;
sum1=(float)sum;
remark1=(float)remark;
blank1=(float)blank;
//將整形數據強制類型轉換為浮點型
JudgeFun(sum1,remark1,blank1,total);
//評價函數風格
return(0);
}
@
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -