2016-08-15 74 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