2016-05-13 3 views
-4
#pragma warning(disable:4996) 
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <ctype.h> 

static WORDS heap[10000]; 
int heapSize; 

void InitHeap() 
{ 
    heapSize = 0; 
    heap[0].words = NULL; 
    heap[0].count = -1; 
} 

void InsertHeap(char* string) 
{ 
    heapSize++; 
    strcpy(heap[heapSize].words, string); 

    int now = heapSize; 
    while (heap[now/2].words > string) 
    { 
     heap[now] = heap[now/2]; 
     now /= 2; 
    } 
    strcpy(heap[now].words, string); 
} 

int DeleteHeap() 
{ 
    char* minElement, lastElement; 
    int child, now; 

    strcpy(minElement, heap[1].words); 
    strcpy(lastElement, heap[heapSize--].words); 

    for (now = 1; now * 2 <= heapSize; now = child) 
    { 
     child = now * 2; 
     if (child != heapSize && heap[child + 1].words < heap[child].words) 
     { 
      child++; 
     } 
     if (lastElement > heap[child].words) 
     { 
      strcpy(heap[now].words, heap[child].words); 
     } 
     else 
     { 
      break; 
     } 
    } 
    strcpy(heap[now].words, lastElement); 

    return now; 
} 

typedef struct _WORDS { 
    char words[64]; 
    int  count; 
}WORDS; 


char* MakeToken(void) 
{ 
    int i, j; 
    static char delim[256]; 
    memset(delim, 0x0, 256); 

    for (i = 1, j = 0; i < 256; i++) 
    { 
     if (!isalpha(i)) delim[j++] = i; 
    } 
    return delim; 
} 

int main() { 
    int i = 0, cur = 0; 
    FILE *pFile; 
    char readLine[1024], *ptr; 
    char *token = MakeToken(); 

    InitHeap(); 

    pFile = fopen("C:\\Users\\Home\\Desktop\\dataset.txt", "r"); 
    if (pFile == NULL) { 
     printf("File open failed.\n"); 
     return 0; 
    } 
    while (fgets(readLine, 1024, pFile) != NULL) { 
     ptr = strtok(readLine, token); 
     while (ptr != NULL) { 
      InsertHeap(ptr); 
      ptr = strtok(NULL, token); 
     } 
    } 

    for (i = 0; i < heapSize; i++) 
    { 
     cur = DeleteHeap(); 
     printf("%s %d\n", heap[cur].words, heap[cur].count); 
    } 

    return 0; 
} 

エラーメッセージ:ランタイムエラー#3 私は、ファイルからTXTを読み込むプログラムを作るの言葉にそれらをカットし、コンソールに表示します。私はそれを作るが、それは働かない。どのようにそれを修正するには? 静的な単語ヒープと思う< - この部分 または 削除部分はエラーです。 またはそのパスに障害があります。は言葉にそれらをカットし、ファイルからTXTを読み込み、表示

+1

「WORDS」とは何ですか? – GMichael

+1

@Michael 'typedef struct _WORDS { char words [64]; int count; } WORDS; ' – BLUEPIXY

+1

'strcpy(minElement、heap [1] .words); strcpy(lastElement、heap [heapSize - ]。words); '間違っています。彼らはメモリを予約していません。 – BLUEPIXY

答えて

0

私はあなたのコードに次のエラーを参照してください。

heap[0].words = NULL; 

言葉は、あなたがNULLに割り当てることはできませんので、(あなたはコンパイラエラーを取得し、配列ではなく、dynamyc割り当てられたポインタであるWORDSように私には思えます!。単語変数の宣言は誤りです)。

strcpy(minElement, heap[1].words); 
strcpy(lastElement, heap[heapSize--].words); 

minElementとlastElementは初期化されていないため、strcpy関数は失敗します。

ここでコードを修正する方法ですが、可能な限り最小限に変更しました。以下では、単語を集め出現箇所の数をカウントし、アルファベット順で結果を印刷:

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

typedef struct _WORDS { 
    char words[64]; 
    int count; 
} WORDS; 


static WORDS heap[10000]; 

int heapSize; 

void InitHeap() 
{ 
    heapSize = 0; 
} 

void InsertHeap(char* string) 
{ 
    int num = 0; 

    // Search string in heap array. if found, increase count. 
    for(num = 0; num < heapSize; ++num) 
    { 
     if(strcmp(string, heap[num].words) == 0) 
     { 
      heap[num].count++; 
      return; 
     } 
    } 

    // If not found, add it to the array. 
    strcpy(heap[heapSize].words, string); 
    heap[heapSize].count = 1; 
    heapSize++; 
} 

char* MakeToken(void) 
{ 
    int i, j; 
    static char delim[256]; 
    memset(delim, 0x0, 256); 

    for (i = 1, j = 0; i < 256; i++) 
    { 
     if (!isalpha(i)) delim[j++] = i; 
    } 
    return delim; 
} 

int compare(const void* v1, const void* v2) 
{ 
    return strcmp((const char*)v1, (const char*)v2); 
} 

int main() 
{ 
    int i = 0, cur = 0; 
    FILE *pFile; 
    char readLine[1024], *ptr; 
    char *token = MakeToken(); 

    InitHeap(); 

    pFile = fopen("C:\\Users\\Home\\Desktop\\dataset.txt", "r"); 
    if(pFile == NULL) 
    { 
     printf("File open failed.\n"); 
     return 0; 
    } 

    while (fgets(readLine, 1024, pFile) != NULL) 
    { 
     ptr = strtok(readLine, token); 
     while (ptr != NULL) 
     { 
      InsertHeap(ptr); 
      ptr = strtok(NULL, token); 
     } 
    } 

    // Order alphabetically the heap array. 
    qsort(heap, heapSize, sizeof(WORDS), compare); 

    for (i = 0; i < heapSize; i++) 
    { 
     printf("%s %d\n", heap[i].words, heap[i].count); 
    } 

    return 0; 
} 
+0

NULL部分を消去します。 strcpyの部分を修正する方法は? –

+0

答えを更新しました。申し訳ありませんが、コードの目的が完全にはっきりしていません。たぶん、あなたの質問にいくつかの情報を追加する必要があります – Carlo

+0

私は単語にtxtファイルをカットし、ヒープに(それらは何回彼らはtxtに表示されるかを数える)、そして辞書の順序で印刷します。だから私は言葉を切るたびに、その親ノードと比較してください。 –

-1

私はコード内のいくつかのエラーを修正しましたし、それはいくつかの結果を生成するために開始しました。私はあなたの仕事を完全に理解していないので、さらに進歩することはできません。作業コードは次のとおりです。

#pragma warning(disable:4996) 
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <ctype.h> 

typedef struct _WORDS { char words[64]; int count; }WORDS; 

static WORDS app_heap[10000]; 
int heapSize; 

void InitHeap() 
{ 
    heapSize = 0; 
    app_heap[0].words[0] = '\0'; 
    app_heap[0].count = -1; 
} 

void InsertHeap(char* string) 
{ 
    heapSize++; 
    strcpy(app_heap[heapSize].words, string); 

    int now = heapSize; 
    while (app_heap[now/2].words > string) 
    { 
     app_heap[now] = app_heap[now/2]; 
     now /= 2; 
    } 
    strcpy(app_heap[now].words, string); 
} 

int DeleteHeap() 
{ 
    char minElement[64], lastElement[64]; 
    int child, now; 

    if(heapSize <= 0) 
    { 
     printf("Wrong call\n"); 
     return 0; 
    } 
    strcpy(minElement, app_heap[1].words); 
    strcpy(lastElement, app_heap[heapSize--].words); 

    for (now = 1; now * 2 <= heapSize; now = child) 
    { 
     child = now * 2; 
     if (child != heapSize && app_heap[child + 1].words < app_heap[child].words) 
     { 
      child++; 
     } 
     if (lastElement > app_heap[child].words) 
     { 
      strcpy(app_heap[now].words, app_heap[child].words); 
     } 
     else 
     { 
      break; 
     } 
    } 
    strcpy(app_heap[now].words, lastElement); 

    return now; 
} 

char* MakeToken(void) 
{ 
    int i, j; 
    static char delim[256]; 
    memset(delim, 0x0, 256); 

    for (i = 1, j = 0; i < 256; i++) 
    { 
     if (!isalpha(i)) delim[j++] = i; 
    } 
    return delim; 
} 

int main() { 
    int i = 0, cur = 0; 
    FILE *pFile; 
    char readLine[1024], *ptr; 
    char *token = MakeToken(); 

    InitHeap(); 

    pFile = fopen("dataset.txt", "r"); 
    if (pFile == NULL) { 
     printf("File open failed.\n"); 
     return 0; 
    } 
    while (fgets(readLine, 1024, pFile) != NULL) { 
     ptr = strtok(readLine, token); 
     while (ptr != NULL) { 
      InsertHeap(ptr); 
      ptr = strtok(NULL, token); 
     } 
    } 

    for (i = 0; i < heapSize; i++) 
    { 
     cur = DeleteHeap(); 
     printf("%s %d\n", app_heap[cur].words, app_heap[cur].count); 
    } 

    return 0; 
} 
関連する問題