用c语言实现 文学研究人员需要统计某篇英文小说中某些特定单词的出现次数和位置(行号和列号)。试写出一个实现这一目标的文字统计系统。 要求: 建立一个文本文件存储一篇英文小说的片段,每个单词不包含空格且不跨行,单词由字符序列构成且区分大小写,检索输出给定单词出现在文本中的行号,以及在该行中出现的位置(列号);统计给定单词在文本文件中出现的总次数。
时间: 2024-02-13 17:03:01 浏览: 154
好的,以下是使用 C 语言实现的文字统计系统:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LENGTH 50
#define MAX_LINE_LENGTH 1000
int count_words(char* filename, char* words[], int num_words);
int main() {
char* filename = "novel.txt";
char* words[] = {"the", "of", "and", "to", "in", "that", "is", "for", "it", "with", "was", "on", "be", "by", "as", "at", "an", "this", "which", "or", "from", "but", "not", "are", "have", "they", "one", "had", "her", "all", "she", "there", "been", "their", "who", "will", "would", "when", "him", "his", "could", "no", "than", "out", "them", "other", "some", "into", "then", "up", "its", "over", "only", "most", "see", "my", "me", "know", "any", "more", "than", "us", "like", "made", "upon", "such", "great", "before", "must", "these", "two", "between", "own", "down", "those", "through", "might", "just", "now", "under", "people", "could", "mr", "mrs", "miss", "dr", "prof", "rev"};
int num_words = 100;
count_words(filename, words, num_words);
return 0;
}
int count_words(char* filename, char* words[], int num_words) {
FILE* fp;
char line[MAX_LINE_LENGTH];
int line_num = 0;
int word_count[num_words];
int line_count[num_words][MAX_LINE_LENGTH];
int i, j;
// Initialize arrays
for (i = 0; i < num_words; i++) {
word_count[i] = 0;
for (j = 0; j < MAX_LINE_LENGTH; j++) {
line_count[i][j] = 0;
}
}
// Open file
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error: can't open file %s.\n", filename);
return -1;
}
// Read file line by line
while (fgets(line, MAX_LINE_LENGTH, fp) != NULL) {
line_num++;
// Split line into words
char* word = strtok(line, " ");
while (word != NULL) {
// Remove punctuation and convert to lowercase
int len = strlen(word);
if (ispunct(word[len-1])) {
word[len-1] = '\0';
len--;
}
for (i = 0; i < len; i++) {
word[i] = tolower(word[i]);
}
// Check if word is in list of words to count
for (i = 0; i < num_words; i++) {
if (strcmp(word, words[i]) == 0) {
word_count[i]++;
line_count[i][line_num]++;
line_count[i][line_num] = strlen(line) - strlen(strstr(line, word)) + 1;
}
}
word = strtok(NULL, " ");
}
}
// Print results
for (i = 0; i < num_words; i++) {
printf("%s:\n", words[i]);
printf("Total count: %d\n", word_count[i]);
if (word_count[i] > 0) {
printf("Line numbers and positions: ");
for (j = 1; j <= line_num; j++) {
if (line_count[i][j] > 0) {
printf("%d:%d ", j, line_count[i][j]);
}
}
printf("\n");
}
printf("\n");
}
fclose(fp);
return 0;
}
```
在上述代码中,我们使用了 `count_words` 函数来实现文字统计,输入参数为文本文件名、待统计的单词列表和列表长度。该函数会打开文件、逐行读取并统计每个单词的出现位置和总次数,最后输出结果。
我们在 `main` 函数中定义了一个默认的文本文件名 `novel.txt` 和待统计的单词列表,可以根据需要自行修改。
需要注意的是,在将单词转换为小写字母之后,我们使用 `line_count` 数组来记录每个单词在每一行出现的位置,以便后续输出。
阅读全文