📅  最后修改于: 2023-12-03 14:49:25.603000             🧑  作者: Mango
在 C 编程语言中,我们可以通过文件流来读取文本文件中的内容,并对单词进行处理。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_WORD_LEN 100
int main(void) {
char word[MAX_WORD_LEN];
char *filename = "text.txt";
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file %s.\n", filename);
return 1;
}
while (fscanf(fp, "%s", word) != EOF) {
int len = strlen(word);
if (!isalpha(word[len - 1])) {
word[len - 1] = '\0';
}
printf("%s\n", word);
}
fclose(fp);
return 0;
}
上面的代码片段可以读取文本文件中的每个单词,并在控制台上显示出来。
但是,我们要按字母升序显示所有单词,可以使用数组或链表来存储单词,并对其进行排序。
下面是使用数组来存储单词并按字母升序排序的代码:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_WORD_LEN 100
#define MAX_WORDS 1000
void sort_words(char *words[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(words[i], words[j]) > 0) {
char *tmp = words[i];
words[i] = words[j];
words[j] = tmp;
}
}
}
}
int main(void) {
char word[MAX_WORD_LEN];
char *filename = "text.txt";
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file %s.\n", filename);
return 1;
}
char *words[MAX_WORDS];
int n = 0;
while (fscanf(fp, "%s", word) != EOF) {
int len = strlen(word);
if (!isalpha(word[len - 1])) {
word[len - 1] = '\0';
}
int found = 0;
for (int i = 0; i < n; i++) {
if (strcmp(words[i], word) == 0) {
found = 1;
break;
}
}
if (!found) {
words[n++] = strdup(word);
}
}
sort_words(words, n);
for (int i = 0; i < n; i++) {
printf("%s\n", words[i]);
free(words[i]);
}
fclose(fp);
return 0;
}
上面的代码片段将单词存储在一个数组中,并使用冒泡排序算法对其进行排序。注意,在排序之前,需要先去除重复的单词。
也可以使用链表来存储单词,并对其进行排序。下面是使用链表来存储单词并按字母升序排序的代码:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAX_WORD_LEN 100
typedef struct node {
char word[MAX_WORD_LEN];
struct node *next;
} Node;
Node *create_node(char *word) {
Node *node = malloc(sizeof(Node));
strcpy(node->word, word);
node->next = NULL;
return node;
}
void free_list(Node *head) {
while (head != NULL) {
Node *tmp = head;
head = head->next;
free(tmp);
}
}
void insert_node(Node **head, char *word) {
Node *prev = NULL;
Node *curr = *head;
Node *node = create_node(word);
while (curr != NULL && strcmp(curr->word, word) < 0) {
prev = curr;
curr = curr->next;
}
if (prev == NULL) {
node->next = *head;
*head = node;
} else {
node->next = curr;
prev->next = node;
}
}
int main(void) {
char word[MAX_WORD_LEN];
char *filename = "text.txt";
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file %s.\n", filename);
return 1;
}
Node *head = NULL;
while (fscanf(fp, "%s", word) != EOF) {
int len = strlen(word);
if (!isalpha(word[len - 1])) {
word[len - 1] = '\0';
}
int found = 0;
Node *prev = NULL;
Node *curr = head;
while (curr != NULL) {
int cmp = strcmp(curr->word, word);
if (cmp == 0) {
found = 1;
break;
} else if (cmp > 0) {
break;
} else {
prev = curr;
curr = curr->next;
}
}
if (!found) {
insert_node(&head, word);
}
}
Node *curr = head;
while (curr != NULL) {
printf("%s\n", curr->word);
curr = curr->next;
}
free_list(head);
fclose(fp);
return 0;
}
上面的代码片段使用一个链表来存储单词,并在插入时保持其按字母升序排序。注意,在插入之前,需要先去除重复的单词。