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

📄 program.cpp

📁 针对函数代码行符号、字母等进行计算统计的数据结构算法
💻 CPP
字号:
#include <stdlib.h> 
#include <iostream>
#include <string>

using namespace std;

#define TRUE   1
#define FALSE  0
#define OK     1
#define ERROR  0
#define IBFEASIBLE  -1
#define OVERFLOW    -2

typedef char Status;
typedef char SElemType;   //元素类型为整型


#define  STACK_INIT_SIZE  100   //存储空间初始分配量
#define  STACKINCREMENT   10    //存储空间分配增量
typedef struct{
	SElemType  *base;   //在栈构造之前和销毁之后,base的值为NULL
	SElemType  *top;    //栈顶指针
	int  stacksize;     //当前已分配的存储空间,以元素为单位
}SqStack;

SqStack S;


Status InitStack(SqStack &S){
	//构造一个空栈S
 S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
 S.top=S.base;
 S.stacksize=STACK_INIT_SIZE;
 return 1;
}

Status Pop(SqStack &S,SElemType &e) {
	//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR
 if(S.top==S.base)  return 0;
 e=* --S.top;
 return 1;
}

Status GetTop(SqStack &S,SElemType &e){
	//若栈不空,则用e返回的栈S的栈顶元素,并返回1,否则返回0
   if(S.top==S.base)  return 0;   
   e=*(S.top-1);
   return 1 ;   
   }

Status Push(SqStack &S,SElemType e){
	//插入元素e为新的栈顶元素
 if(S.top-S.base>=S.stacksize)      //栈满,追加存储空间
   {
    S.base=(SElemType * )realloc(S.base,
	           (S.stacksize+STACKINCREMENT)*sizeof(SElemType));
    if(!S.base)  exit(OVERFLOW);   // 存储分配失败
	S.top=S.base+S.stacksize;
	S.stacksize+=STACKINCREMENT;
   }
 *S.top++=e;
 return 1;
}

Status StackEmpty(SqStack S) {
//若栈S为空栈,则返回TRUE,否则返回FALSE
 if(S.top==S.base)  return 1;
 else return 0;
}

void main()
{
 InitStack(S);
 FILE *fp;
 char ch,e;
 int l=1,t=0,o=0,bl=0,c=0,d,bz,bk,bd,bb;
 float ad;
 char str[10];
 printf("Please input a program :");
 fp=fopen(gets(str),"r");
 while(fp==NULL)
	{
	 printf("The program isn't here! Press another program.\n");
	 printf("Please input a program :");
	 fp=fopen(gets(str),"r");
	} 
 ch=fgetc(fp);
 while(ch!=EOF)
	{
	 //求行数l,空白行数bl
	 if(ch=='\n')   
	   {
	   	l++;
	   	ch=fgetc(fp);
	   	while(ch=='\t'|| ch==' ')
	   	  ch=fgetc(fp);
	   	while(ch=='/')
			{
			 c++;
			 break;	
			}
	   	while(ch=='\n')
	   	  {				
	   	   bl++;
	   	   l++;
	   	   ch=fgetc(fp);
	   	   while(ch=='\t'|| ch==' ')
	   	   ch=fgetc(fp);
	   	  }
	   }		
	
	//求注释行数o
	if(ch=='/')
	  {
	   ch=fgetc(fp);
	   if(ch=='/')
	      o++;
	  }
	
	//求函数个数t
	if(ch=='{')
	  Push(S,ch);
	if(ch=='}')
	 {	
	  Pop(S,e);
	  if(StackEmpty(S)) t++;						
	 }
	
	ch=fgetc(fp);		
	}
	
 printf("Lines:%d\n",l);
 printf("the number of code:%d\n",t);
 printf("Blank lines:%d\n",bl);
 printf("Lines of comments:%d\n",o);
 
 d=l-bl-c;    //代码行数
 ad=d/t;      //平均行数
 bb=l/t;      //函数平均行数
 bd=d*100/l;  
 bz=o*100/l;
 bk=bl*100/l;
  
 printf("\nThe results of analysing program file:");
 puts(str);
 printf("Lines of code:    %d\n",d);
 printf("Lines of comments:%d\n",o);
 printf("Blank lines:      %d\n\n",bl);
 printf("Code\tComments\tSpace\n");
 printf("====\t========\t=====\n");
 printf("%d\t%d\t\t%d\n",bd,bz,bk);
 printf("The program includes %d functions.\n",t);
 printf("The average length of a section of code is %f lines.\n",ad);
 
 if(bb>=10&&bb<=15) 
    printf("等级 A :Excellent routine size style.\n");
 if((bb>=8&&bb<=9)||(bb>=16&&bb<=20)) 
    printf("等级 B :Excellent routine size style.\n");
 if((bb>=5&&bb<=7)||(bb>=21&&bb<=24)) 
    printf("等级 C :Excellent routine size style.\n");
 if(bb<=5||bb>=24) 
    printf("等级 D :Excellent routine size style.\n");
    
 if(bz>=15&&bz<=25) 
    printf("等级 A :Excellent commenting size style.\n");
 if((bz>=10&&bz<=14)||(bb>=26&&bb<=30)) 
    printf("等级 B :Excellent commenting size style.\n");
 if((bz>=5&&bz<=9)||(bz>=31&&bz<=35)) 
    printf("等级 C :Excellent commenting size style.\n");
 if(bz<=5||bz>=35) 
    printf("等级 D :Excellent commenting size style.\n");
 
 if(bk>=15&&bk<=25) 
   printf("等级 A :Excellent white space size style.\n");
 if((bk>=10&&bk<=14)||(bk>=26&&bk<=30)) 
   printf("等级 B :Excellent white space size style.\n");
 if((bk>=5&&bk<=9)||(bk>=31&&bk<=35)) 
   printf("等级 C :Excellent white space size style.\n");
 if(bk<=5||bk>=35) 
   printf("等级 D :Excellent white space size style.\n");
 
 fclose(fp);	
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -