0

テキストファイルを開き、ファイルから読み取り、大文字を小文字に変更し、その単語がファイル内で何回出現したかを数え、結果を新しいテキストファイルに出力するプログラムを作成しようとしています。

これまでの私のコードは次のとおりです。

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

int main()
{

    FILE *fileIN;
    FILE *fileOUT;
    char str[255];
    char c;
    int i = 0;

    fileIN = fopen ("input.txt", "r");
    fileOUT = fopen ("output.txt", "w");

    if (fileIN == NULL || fileOUT == NULL)
    {
        printf("Error opening files\n");
    }

    else
    {
        while(! feof(fileIN)) //reading and writing loop
        {
            fscanf(fileIN, "%s", str); //reading file


            i = 0;
            c = str[i];
            if (isupper(c)) //changing any upper case to lower case
            {
                c =(tolower(c));
                str[i] = putchar(c);
            }

            printf("%s ", str); //printing output

                            fprintf(fileOUT, "%s\n", str); //printing into file
        }




        fclose(fileIN);
        fclose(fileOUT);
    }
    getch();
}

input.txt ファイルには、「スペインの雨は主に飛行機に降る」という内容が含まれています。理由は聞かないでください。プログラムをそのまま実行すると、出力は次のようになります。スペインの雨は主に飛行機に降る

大文字の単語を小文字にすることができました。各単語の出現回数を数える方法を理解するのに苦労しています。たとえば、出力では、2 が表示されたことを意味する「the 2」と表示する必要があります。これは、そのファイルにこれ以上「the」を保存したくないことも意味します。

strcmp と strcpy を考えていますが、それらを希望どおりに使用する方法がわかりません。

助けていただければ幸いです

(フォーマットが悪い場合は申し訳ありません)

4

2 に答える 2

1

単語をキーとして、頻度を値としてハッシュ テーブルを作成することができます。

スケッチのアイデア:

  • 単語、つまり空白で区切られた英数字の文字列を認識し、strtok() を使用してみてください
  • 単語ごとに
    • ハッシュ テーブル ベースの辞書で単語を検索する
      • 見つかった場合: 周波数を増やします
      • 見つからない場合: (word, 1) として辞書に新しいエントリを挿入します

最後に、ディクショナリの内容、つまりすべてのエントリを出力しますentry.wordentry.frequency

詳細については、この質問と回答を参照してください。C で辞書を実装する簡単な方法聖書「The C Programming Language」のセクション 6.6 に基づいています。

OPのコメントに基づく更新:

ハッシュ テーブルは単なる効率的なテーブルです。使用したくない場合でも、通常のテーブルを使用できます。ここにいくつかのアイデアがあります。

typedef struct WordFreq {
    char  word[ N ];
    int   freq;
} WordFreq;

WordFreq wordFreqTable[ T ];

(N is the maximum length of a single word, T is the maximum number of unique words)

検索と挿入のために、テーブル内で線形検索を行うことができますfor( int i = 0; i != T; ++i ) {

于 2013-04-04T23:41:04.370 に答える
0

簡単なサンプル (エラー キャッチが必要、メモリを解放する、qsort を使用するための並べ替えなど...)

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

#define BUFFSIZE 1024

typedef struct _wc {
    char *word;
    int count;
} WordCounter;

WordCounter *WordCounters = NULL;
int WordCounters_size = 0;

void WordCount(char *word){
    static int size = 0;
    WordCounter *p=NULL;
    int i;

    if(NULL==WordCounters){
        size = 4;
        WordCounters = (WordCounter*)calloc(size, sizeof(WordCounter));
    }
    for(i=0;i<WordCounters_size;++i){
        if(0==strcmp(WordCounters[i].word, word)){
            p=WordCounters + i;
            break;
        }
    }
    if(p){
        p->count += 1;
    } else {
        if(WordCounters_size == size){
            size += 4;
            WordCounters = (WordCounter*)realloc(WordCounters, sizeof(WordCounter)*size);
        }
        if(WordCounters_size < size){
            p = WordCounters + WordCounters_size++;
            p->word = strdup(word);
            p->count = 1;
        }
    }
}

int main(void){
    char buff[BUFFSIZE];
    char *wordp;
    int i;

    while(fgets(buff, BUFFSIZE, stdin)){
        strlwr(buff);
        for(wordp=buff; NULL!=(wordp=strtok(wordp, ".,!?\"'#$%&()=@ \t\n\\;:[]/*-+<>"));wordp=NULL){
            if(!isdigit(*wordp) && isalpha(*wordp)){
                WordCount(wordp);
            }
        }
    }
    for(i=0;i<WordCounters_size;++i){
        printf("%s:%d\n", WordCounters[i].word, WordCounters[i].count);
    }

    return 0;
}

デモ

>WordCount.exe
The rain in Spain falls mainly in the plane
^Z
the:2
rain:1
in:2
spain:1
falls:1
mainly:1
plane:1
于 2013-04-05T10:54:08.240 に答える