2016-08-15 47 views
2

好吧,我现在所拥有的东西,检查单词的数量。但我无法按字母顺序排列单词。如何在C中按字母顺序排序文件的行?

我宁愿这样做,然后只是计算它们的数量。

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

typedef struct node *node_ptr; 

typedef struct node { 
    int count; 
    char *word; 
    node_ptr next; 
} node_t; 

char *words[] = { "hello", "goodbye", "sometimes", "others", "hello", "others", NULL }; 

node_ptr new_node() { 
    node_ptr aNode; 

    aNode = (node_ptr)(malloc(sizeof(node_t))); 
    if (aNode) { 
     aNode->next = (node_ptr)NULL; 
     aNode->word = (char *)NULL; 
     aNode->count = 0; 
    } 
    return aNode; 
} 

node_ptr add_word(char *word, node_ptr theList) { 
    node_ptr currPtr, lastPtr, newPtr; 
    int result; 
    int found = 0; 

    currPtr = theList; 
    lastPtr = NULL; 
    printf("Checking word:%s\n", word); 

    if (!currPtr) { 
     newPtr = new_node(); 
     if (!newPtr) { 
      fprintf(stderr, "Fatal Error. Memory alloc error\n"); 
      exit(1); 
     } 
     newPtr->word = word; 
     newPtr->next = currPtr; 
     newPtr->count = 1; 
     found = 1; 
     theList = newPtr; 
    } 
    while (currPtr && !found) { 
     result = strcmp(currPtr->word, word); 
     if (result == 0) { 
      currPtr->count += 1; 
      found = 1; 
     } else 
     if (result>0) { 
      newPtr = new_node(); 
      if (!newPtr) { 
       fprintf(stderr, "Fatal Error. Memory alloc error\n"); 
       exit(1); 
      } 
      newPtr->word = word; 
      newPtr->next = currPtr; 
      newPtr->count = 1; 

      if (lastPtr) { 
       lastPtr->next = newPtr; 
      } else { 
       theList = newPtr; 
      } 
      found = 1; 
     } else { 
      lastPtr = currPtr; 
      currPtr = currPtr->next; 
     } 
    } 

    if ((!found) && lastPtr) { 
     newPtr = new_node(); 
     if (!newPtr) { 
      fprintf(stderr, "Fatal Error. Memory alloc error\n"); 
      exit(1); 
     } 
     newPtr->word = word; 
     newPtr->next = (node_ptr)NULL; 
     newPtr->count = 1; 
     lastPtr->next = newPtr; 
     found = 1; 
    } 
    return theList; 
} 

void printList(node_ptr theList) { 
    node_ptr currPtr = theList; 

    while (currPtr) { 
     printf("word: %s\n", currPtr->word); 
     printf("count: %d\n", currPtr->count); 
     printf("---\n"); 
     currPtr = currPtr->next; 
    } 
} 

int main() { 
    char **w = words; 
    node_ptr theList = (node_ptr)NULL; 

    printf("Start\n"); 
    while (*w) { 
     theList = add_word(*w, theList); 
     w++; 
    } 

    printList(theList); 
    printf("OK!\n"); 
    return 0; 
} 

我还想从单词数组中读取数据,我宁愿从文件中读取数据。

FILE *fp; 
fp = fopen("some.txt", "w"); 

如何从使用我的结构的文件中读取我创建并对它们进行排序?

感谢您的帮助!我想自学C :)

+2

这是一个坏习惯的typedef指针:'typedef结构节点* node_ptr;'隐藏指针不会使它们更容易理解或处理,恰恰相反,它会混淆您的代码。 – chqrlie

+0

在'add_word()'中,'newPtr-> word = word;'是一个问题。代码可能需要添加字符串的副本。 – chux

+0

对不起,我很困惑。我想同时做@WeatherVane ..我想阅读文件中的单词并按字母顺序排序! – hiquetj

回答

0

您可以fscanf()从文件中读取的话:

int main(int argc, char *argv[]) { 
    node_t *theList = NULL; 
    for (int i = 1; i < argc; i++) { 
     FILE *fp = fopen(argv[i], "r"); 
     if (fp != NULL) { 
      char word[100]; 
      while (fscanf(fp, "%99s", word) == 1) { 
       theList = add_word(word, theList); 
      } 
      fclose(fp); 
     } 
    } 
    printList(theList); 
    printf("OK!\n"); 
    return 0; 
} 
+0

代码中的“行”是什么?在addword函数中..你在哪里初始化它? – hiquetj

+0

@hiquetj:对不起,这只是一个错字,我将本地数组重新命名为'word',并忘记了这个例子。正如你所看到的,我用'fopen'打开这个文件,用'fscanf()'逐个读一个单词并将它们添加到列表中。单词由任意数量的空白字符分隔,包括换行符。 – chqrlie

+0

@hiquetj:这个答案对你有帮助吗?你介意接受它吗? – chqrlie