69,382
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <conio.h>
int main() {
FILE *text;
const int MAXSIZE = 1000; //串的最大长度
int i = 0, num = 0, space = 0, symbol = 0, number = 0, letter = 0, word = 0, line = 0, english = 0;
char str[MAXSIZE];
text = fopen("test.txt", "r");
if (NULL == text)
printf("erro!");
else {
while (!feof(text)) {
fgets(str, MAXSIZE, text);
}
printf("%s",str);
i = 0;
if (str[0] != '\0')
line++; //确定字符串非空
while (str[i] != '\0') {
if (int(str[i]) == 32)
space++; //空格
else if ((int(str[i]) > 32 && int(str[i]) < 48) || (int(str[i]) > 57 && int(str[i]) < 65) ||
(int(str[i]) > 91 && int(str[i]) < 97) || (int(str[i]) > 122 && int(str[i]) < 128))
symbol++; //标点符号
else if ((int(str[i]) > 47 && int(str[i]) < 58))
number++; //数字
else if ((int(str[i]) > 64 && int(str[i]) < 91) || (int(str[i]) > 96 && int(str[i]) < 123))
letter++; //英文
else if (int(str[i]) < 0)
word++; //中文
if ((i + 1 - num) / 120 > 0) {
num += 120;
line++; //行数
}
if (i > 0)
if (((int(str[i - 1]) > 64 && int(str[i - 1]) < 91) || (int(str[i - 1]) > 96 && int(str[i - 1]) < 123)) &&
((str[i] == ' ')|| (int(str[i]) > 32 && int(str[i]) < 48) || (int(str[i]) > 57 && int(str[i]) < 65) ||
(int(str[i]) > 91 && int(str[i]) < 97) || (int(str[i]) > 122 && int(str[i]) < 128))) {
english++; //单词
}
i++;
}
word /= 2;
fclose(text);
printf("\n空格 = %d\n标点 = %d\n数字 = %d\n字母 = %d\n中文 = %d\n行数 = %d\n单词 = %d"
, space, symbol, number, letter, word, line, english);
}
return 0;
}
are you ready? 统计文本文件基本信息和其他基本操作。文本文件中事先存放着中文,英文,以及各种标点符号,试编写程序统计其中字符总数(含空格),空格总数,字母个数,数字个数,标点符号个数,汉字个数(含汉字标点),单词个数,行数和段落数。0123456789
are you ready? 统计文本文件基本信息和其他基本操作。文本文件中事先存放着中文,0123456789