#include <stdio.h> #include <string.h> #include <ctype.h>

#define MAX_LINES 4 #define MAX_WORDS 100 #define MAX_INTS 100

void count_letters(FILE* fp); void count_words(FILE* fp); void find_longest_word(FILE* fp); void count_integers(FILE* fp);

int main() { FILE* fp; char filename[100];

// 获取文件名
printf("请输入文件名: ");
scanf("%s", filename);

// 打开文件
fp = fopen(filename, "r");
if (fp == NULL) {
    printf("文件打开失败!\n");
    return 1;
}

// 统计字母频度
printf("字母频度统计结果:\n");
count_letters(fp);

// 统计单词个数
printf("\n单词个数统计结果:\n");
count_words(fp);

// 找出最长单词
printf("\n最长单词统计结果:\n");
find_longest_word(fp);

// 统计整数个数
printf("\n整数个数统计结果:\n");
count_integers(fp);

// 关闭文件
fclose(fp);

return 0;

}

void count_letters(FILE* fp) { int letters[26] = {0}; // 存储每个字母出现的次数 char line[100]; // 存储每行文本 int i, j;

// 读取每行文本,统计字母频度
for (i = 0; i < MAX_LINES && fgets(line, sizeof(line), fp) != NULL; i++) {
    for (j = 0; j < strlen(line); j++) {
        if (isalpha(line[j])) { // 判断是否为字母
            letters[toupper(line[j]) - 'A']++; // 统计字母频度
        }
    }
}

// 输出统计结果
for (i = 0; i < 26; i++) {
    printf("%c: %d\n", i + 'A', letters[i]);
}

}

void count_words(FILE* fp) { char line[100]; // 存储每行文本 char* word; // 存储每个单词 int word_count = 0; // 统计单词个数 int i;

// 读取每行文本,统计单词个数
for (i = 0; i < MAX_LINES && fgets(line, sizeof(line), fp) != NULL; i++) {
    word = strtok(line, " \n\t"); // 根据空格、换行符、制表符分割单词
    while (word != NULL) {
        word_count++; // 统计单词个数
        word = strtok(NULL, " \n\t"); // 继续分割下一个单词
    }
}

// 输出统计结果
printf("单词个数: %d\n", word_count);

}

void find_longest_word(FILE* fp) { char line[100]; // 存储每行文本 char* word; // 存储每个单词 char longest_word[100]; // 存储最长单词 int longest_length = 0; // 存储最长单词长度 int i;

// 读取每行文本,找出最长单词
for (i = 0; i < MAX_LINES && fgets(line, sizeof(line), fp) != NULL; i++) {
    word = strtok(line, " \n\t"); // 根据空格、换行符、制表符分割单词
    while (word != NULL) {
        if (strlen(word) > longest_length) { // 如果当前单词长度超过最长单词长度
            longest_length = strlen(word); // 更新最长单词长度
            strcpy(longest_word, word); // 更新最长单词
        }
        word = strtok(NULL, " \n\t"); // 继续分割下一个单词
    }
}

// 输出统计结果
printf("最长单词: %s\n长度: %d\n", longest_word, longest_length);

}

void count_integers(FILE* fp) { char line[100]; // 存储每行文本 int integers[MAX_INTS]; // 存储整数 int integer_count = 0; // 统计整数个数 char* word; // 存储每个单词 int i;

// 读取每行文本,统计整数个数
for (i = 0; i < MAX_LINES && fgets(line, sizeof(line), fp) != NULL; i++) {
    word = strtok(line, " \n\t"); // 根据空格、换行符、制表符分割单词
    while (word != NULL) {
        if (sscanf(word, "%d", &integers[integer_count]) == 1) { // 如果能够成功读取一个整数
            integer_count++; // 统计整数个数
        }
        word = strtok(NULL, " \n\t"); // 继续分割下一个单词
    }
}

// 输出统计结果
printf("整数个数: %d\n", integer_count);
printf("整数列表: ");
for (i = 0; i < integer_count; i++) {
    printf("%d ", integers[i]);
}
printf("\n");

}

用c语言读入一个英文文本文件英文中有字母、数字、其它字符无汉字文件有多行程序要能处理至少4行文字附件中有参考素材。 2编写函数实现:统计字母的出现频度并显示结果。大小写算同一字母 3编写函数实现:统计其中单词的个数并显示结果。单词个数以WORD统计作为参考不一定100相同。 4编写函数实现:找出最长的单词并显示单词及其长度。 5编写函数实现:统计出现的整数存入一维数组并显示

原文地址: https://www.cveoy.top/t/topic/b5mL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录