while (fgets(total, 10000, fp) != NULL) { int blanki[1000] = { -1 };//统计空格出现的位置 if (total[0] == ' ') { j = 0; } if (total[0] != ' ') { j = 1; } int len = strlen(total); for (i = 0; i < len; i++) { if (total[i] >= 'A' && total[i] <= 'z') { count.letter++; } else if (total[i] >= '0' && total[i] <= '9') { count.number++; } else if (total[0] != ' ' && total[i] == ' ') { count.blank++; blanki[j] = i; j++; } else if (total[0] == ' ' && total[i] == ' ' && total[i + 1] == ' ') { count.blank++; } else if (total[0] == ' ' && total[i] == ' ' && total[i + 1] != ' ') { count.blank++; blanki[j] = i; j++; } else if (total[i] == ',' || total[i] == '.') { count.other++; blanki[j] = i; j++; } else { if (total[i] != '\n')count.other++; } } if (total[strlen(total) - 1] == '\n') { blanki[j] = strlen(total) - 1; } else { blanki[j] = strlen(total); } int from, to, sum = 0; i = 1; while (blanki[i] != 0) { from = blanki[i - 1]; to = blanki[i]; for (int q = blanki[i - 1]; q < blanki[i]; q++) { if (total[q] >= 'A' && total[q] <= 'z') { sum++; } } if (sum == blanki[i] - blanki[i - 1] - 1 && sum != 0) { count.word++; } i++; sum = 0; } count.sumchar = count.blank + count.number + count.letter + count.other; word_s += count.word; sumchar_s += count.sumchar; letter_s += count.letter; number_s += count.number; blank_s += count.blank; other_s += count.other; count.sumchar = count.blank = count.number = count.letter = count.other =count.word = 0; }写注释
时间: 2024-01-20 12:02:17 浏览: 20
这段代码用于统计文件中的字符数量、字母数量、数字数量、空格数量、其他字符数量和单词数量。具体实现是通过读取文件中的每一行,然后对每一行进行遍历,统计各种字符的数量,并且通过空格的位置来判断单词的数量。最后将统计结果累加到相应的总计数器中。以下是具体的注释:
```
while (fgets(total, 10000, fp) != NULL) { //逐行读取文件,直到文件末尾
int blanki[1000] = { -1 };//统计空格出现的位置
if (total[0] == ' ') { //如果第一个字符为空格,说明当前行的第一个单词前面没有空格
j = 0;
}
if (total[0] != ' ') { //如果第一个字符不是空格,说明当前行的第一个单词前面有一个空格
j = 1;
}
int len = strlen(total); //获取当前行的长度
for (i = 0; i < len; i++) { //遍历当前行的每一个字符
if (total[i] >= 'A' && total[i] <= 'z') { //如果当前字符是字母,则字母数量加1
count.letter++;
}
else if (total[i] >= '0' && total[i] <= '9') { //如果当前字符是数字,则数字数量加1
count.number++;
}
else if (total[0] != ' ' && total[i] == ' ') { //如果当前字符是空格,并且第一个字符不是空格,说明当前空格是一个新单词的开头
count.blank++;
blanki[j] = i; //记录当前空格出现的位置
j++;
}
else if (total[0] == ' ' && total[i] == ' ' && total[i + 1] == ' ') { //如果当前字符是连续的两个空格,并且第一个字符是空格,说明当前空格是一个新单词的开头
count.blank++;
}
else if (total[0] == ' ' && total[i] == ' ' && total[i + 1] != ' ') { //如果当前字符是空格,并且第一个字符是空格,但是后面不是空格,说明当前空格是一个新单词的开头
count.blank++;
blanki[j] = i; //记录当前空格出现的位置
j++;
}
else if (total[i] == ',' || total[i] == '.') { //如果当前字符是逗号或句号,则其他字符数量加1
count.other++;
blanki[j] = i; //记录当前字符出现的位置
j++;
}
else { //如果当前字符不是字母、数字、空格、逗号或句号,则其他字符数量加1
if (total[i] != '\n')count.other++; //注意要排除换行符
}
}
if (total[strlen(total) - 1] == '\n') { //如果当前行以换行符结尾,说明最后一个单词已经统计完毕
blanki[j] = strlen(total) - 1;
}
else { //如果当前行没有以换行符结尾,说明最后一个单词还没有统计完毕
blanki[j] = strlen(total);
}
int from, to, sum = 0;
i = 1;
while (blanki[i] != 0) { //根据空格的位置来计算单词的数量
from = blanki[i - 1];
to = blanki[i];
for (int q = blanki[i - 1]; q < blanki[i]; q++) { //遍历当前单词的每一个字符
if (total[q] >= 'A' && total[q] <= 'z') { //如果当前字符是字母,则累加到sum中
sum++;
}
}
if (sum == blanki[i] - blanki[i - 1] - 1 && sum != 0) { //如果当前单词的长度等于空格的距离,说明当前单词不包含其他字符
count.word++; //单词数量加1
}
i++;
sum = 0;
}
count.sumchar = count.blank + count.number + count.letter + count.other; //计算当前行的字符总数
word_s += count.word; //将当前行的统计结果累加到总计数器中
sumchar_s += count.sumchar;
letter_s += count.letter;
number_s += count.number;
blank_s += count.blank;
other_s += count.other;
count.sumchar = count.blank = count.number = count.letter = count.other =count.word = 0; //清空当前行的统计结果
}
```