#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;
}