⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sum.txt

📁 这是数据结构习题书上实验四的一个程序,可以下载来
💻 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 + -