一个简单的统计C源程序代码行数的小程序

#include

#include

#define M 255

static int label = 0;

unsigned long count_line(char file_name[]);

void process(char str[]);

int is_legal(char str[]);

int main(int argc, char* argv[])

{

char file_name[100];

unsigned long count = 0;

if(argc <= 1 || argc >2){

printf("input a name of '.c' file/n");

return -1;

}

strcpy(file_name, argv[1]);

count = count_line(file_name);

printf("The file %s has %lu lines/n", file_name, count);

return 0;

}

unsigned long count_line(char file_name[])

{

FILE* re;

char str_line[M];

unsigned long count = 0;

re = fopen(file_name, "r");

if(re == NULL){

printf("open file is failure/n");

return 0;

}

while(fgets(str_line, M, re) != NULL){

process(str_line);

//printf("%s/n", str_line);

if(is_legal(str_line))

count++;

}

fclose(re);

return count;

}

void process(char str[])

{

char temp[M];

int loop;

int index = 0;

int length;

length = strlen(str);

for(loop = 0; loop <= length && (str[loop] == ' ' || str[loop] == 0X0D || str[loop] == 0X09 || str[loop] == 0X0A); loop++);

strcpy(str, str + loop);

length = strlen(str);

for(loop = length - 1; loop >= 0 && (str[loop] == ' ' || str[loop] == 0X0D || str[loop] == 0X09 || str[loop] == 0X0A); loop--);

str[loop + 1] = '/0';

}

int is_legal(char str[])

{

int re = 1;

int length;

length = strlen(str);

if(length == 0){

re = 0;

//printf("the line is NULL/n");

}

else if(str[0] == '/' && str[1] == '/'){

re = 0;

//printf("the header of line is '//'/n");

}

else if(str[0] == '/' && str[1] =='*'){

label = 1;

re = 0;

//printf("the header of line is '/*'/n");

if(str[length - 1] == '/' && str[length - 2] == '*')

label = 0;

}

else{

if(label){

re =0;

if(str[length - 1] == '/' && str[length - 2] == '*')

label = 0;

//printf("the line is notation/n");

}

//else

//printf("the line is legal/n");

}

return re;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值