2017-02-01 6 views
-3

コードが記載されています。私は同じでない単語の数を数える必要があります。それを行うために、私はstcrmpを使ってそれらを比較する必要があります。下のコードを見て、while文またはif文をダブルリンクリストを使ってファイル内の単語を比較する方法を構築する必要がありますか?私は、この状態がメインに印刷されていなければならないと思います。私の状態は機能しません。また、ここでその長さで単語を並べ替える場所や方法についてアドバイスをいただけますか? コードを理解するために: このプログラムは、コマンドライン引数として入力されたファイルを読み込み、ファイルから各行を読み込み、行から各単語をトークン化し、各単語がそれをa Word Length構造体をその長さに応じて配置し、単語の文字列に応じてword_count構造体に配置し、各単語の出現をファイルに数えます。Cでリンクされている単語が同じ場合はどう比較するのですか?リンクリスト

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

#define DELIM " ,.+-=!?:;\t" 
#define MAXLINE 25000 

typedef struct word_count { 
    char *word; 
    int count; 
    struct word_count *next; 
    struct word_count *prev; 
} WORD; 

typedef struct word_length_count { 
    int length; 
    int count; 
    WORD *words; 
    struct word_length_count *next; 
    struct word_length_count *prev; 
} WLENGTH; 

int splitIntoWords(char line[]); 
void processLength(char *word); 
void processWord(char *word, WORD *wordCount); 
void printWordLength(); 
WLENGTH *createWordLength(char *word); 
WORD *createWordCount(char *word); 

WLENGTH *wordLength = NULL; 

int main(unsigned int argc, unsigned char *argv[]) { 
    FILE *fpin; 
    char line[MAXLINE]; 
    int totalWordCount = 0; 

    if ((fpin = fopen(argv[1], "r")) == NULL) { 
     printf("Can't open input file.\n"); 
     exit(-1); 
    } 

    printf("This is the words all tokenized from the input!\n"); 
    while (fgets(line, MAXLINE, fpin) != NULL) { 
     line[strcspn(line, "\n")] = '\0'; 
     if (line[0] == '\0') 
     continue; 
     totalWordCount += splitIntoWords(line); 
    } 
    printf("Total number of words is: %d\n", totalWordCount); 
    printWordLength(); 
    printf("\nFINISHED!"); 
} 

int splitIntoWords(char line[]) { 
    char *word; 
    int count=0; 
    word = strtok(line, DELIM); 
    for (;word != NULL;) { 
     count++; 
     printf("%s\n", word); 
     processLength(word); 
     word = strtok(NULL, DELIM); 
    } 
    return count; 
} 

void processLength(char *word) 
{ 
    WLENGTH *wLCounter = NULL; 
    WLENGTH *wLLast = NULL; 

    if (wordLength == NULL) { 
     wordLength = createWordLength(word); 
     return; 
    } 
    wLCounter = wordLength; 
    while (wLCounter != NULL) { 
     if (strlen(word) == wLCounter->length) { 
      ++wLCounter->count; 
      processWord(word, wLCounter->words); 
      return; 
     } 
     wLLast = wLCounter; 
     wLCounter = wLCounter->next; 
    } 
    wLLast->next = createWordLength(word); 
} 

void processWord(char *word, WORD *wordCount) { 
    WORD *wCounter = NULL; 
    WORD *wLast = NULL; 

    if (wordCount == NULL) { 
     wordCount = createWordCount(word); 
     return; 
    } 
    wCounter = wordCount; 
    while (wCounter != NULL) { 
     if (strcmp(word, wCounter->word) == 0) { 
      ++wCounter->count; 
      return; 
     } 
     wLast = wCounter; 
     wCounter = wCounter->next; 
    } 
    wLast->next = createWordCount(word); 
} 

WLENGTH *createWordLength(char *word) { 
    WLENGTH *wLCounter = NULL; 
    wLCounter = (WLENGTH*)malloc(sizeof(WLENGTH)); 
    wLCounter->words = createWordCount(word); 
    wLCounter->count = 1; 
    wLCounter->length = strlen(word); 
    wLCounter->next = NULL; 
    return wLCounter; 
} 

WORD *createWordCount(char *word) { 
    WORD *wCount = NULL; 
    wCount = (WORD*)malloc(sizeof(WORD)); 
    wCount->word = (char*)malloc(strlen(word+1)); 
    strcpy(wCount->word, word); 
    wCount->count = 1; 
    wCount->next = NULL; 
    return wCount; 
} 

void printWordLength() { 
    WLENGTH *temp = wordLength; 
    WORD *tempWORD = wordLength->words; 
    while (temp != NULL) { 
     WORD *tempWORD = wordLength->words; 
     tempWORD = temp->words; 
     printf("\nFor Word Length: %d : There are: %d occurances!\n", temp->length, temp->count); 
     while (tempWORD != NULL) { 
      printf("\t%s\toccurs:%d\n", tempWORD->word, tempWORD->count); 
      tempWORD = tempWORD->next; 
     } 
    } 
} 
+2

を正しくコードをインデントしてください。このように読むのは難しいです。 – dbush

+1

コードの最後が欠落しています。 –

+0

'strcmp()'は、 'strcmp()'よりも安全性が高い/ベストプラクティスとみなされます。これは、既知の長さと比較した最大バイト数を制限するので、誰かが末尾の '\ 0'境界線から外れてどこかでハックします。 – clearlight

答えて

2

あなたはprintWordLength()の最も外側のwhileループの一番下に、これを逃している:

temp = temp->next; 

それは(あなたが私たちに教えてくれませんでした)無限ループに入る理由です。

さて、あなたはちょうどあなたがprintWordLength()でそれらを印刷している間にあなたが行うことができ、すべてのWORDLENGTH*内のすべてのWORD*をカウントする必要が明確な言葉カウントする:

void printWordLength() 
{ 
    WLENGTH * temp = wordLength; 
    WORD * tempWORD = wordLength->words; 
    unsigned int unique_words = 0; 
    while(temp != NULL) 
    { 
     WORD * tempWORD = wordLength->words; 
     tempWORD = temp->words; 
     printf("\nFor Word Length: %d : There are: %d occurences!\n", 
       temp->length, temp->count); 
     while(tempWORD != NULL) 
     { 
      printf("\t%s\toccurs:%d\n", tempWORD->word, tempWORD->count); 
      unique_words++; 
      tempWORD = tempWORD->next; 
     } 
     temp = temp->next; 
    } 
    printf("\nThere are %u unique words\n", unique_words); 
} 
+0

すみません、私もそれを投稿するのを忘れましたが、残りはよく働いています –

+0

あなたはまだ持っていますか?質問? –

+0

確かに、以下のコードを見て、whileまたはif文を作成して、ダブルリンクリストを使ってファイル内の単語を比較する必要があります。私の質問に答えがありません –

関連する問題