게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
c언어 해쉬에 관해 질문할게요!!ㅜㅜ
게시물ID : computer_64428짧은주소 복사하기
작성자 : 키큰사람
추천 : 0
조회수 : 1124회
댓글수 : 12개
등록시간 : 2012/11/06 14:15:47

파일을 읽어와서 파일의 단어의 갯수를 세는 프로그램 을 짜보았습니다.

 

#include <string.h>

#include <stdio.h>

 

typedef struct tagWORDCOUNT

{

      char word[20];    // 최대20 자까지의단어

      int count;

} WORDCOUNT;

 

#define MAX_WORDS 200         // 최대단어의수

 

void AddWord(WORDCOUNT words[], int* size, char* token);

void ShowWord(WORDCOUNT words[], int size);

 

int main()

{

      WORDCOUNT words[MAX_WORDS];

      int count = 0;

 

      char str[100];

 

      FILE* file = fopen("test.txt", "rt");

      if ( file == NULL )

      {

            printf("file open error!\n");

            return 1;

      }

 

      // 일단단어를하나읽어온다

      fscanf(file, "%s", str);

      // 파일의끝까지

      while ( !feof(file) )

      {

            // 단어하나추가(단어가추가될경우count 가1 증가됨)

            AddWord(words, &count, str);

 

            // 다음단어읽기

            fscanf(file, "%s", str);

      }

 

      // 파일을닫음

      fclose(file);

 

      // 결과를출력

      printf("결과:\n");

      ShowWord(words, count);

 

      return 0;

}

 

void ShowWord(WORDCOUNT words[], int size)

{

      int i;

 

      for ( i = 0; i < size; ++i )

            printf("%s(%d)\n", words[i].word, words[i].count);

}

 

void AddWord(WORDCOUNT words[], int* size, char* token)

{

      int i;

 

      // 이미저장된곳에token 이있는지확인한다

      for ( i = 0; i < *size; ++i )

      {

            // 이미있는토큰이면

            if ( strcmp(words[i].word, token) == 0 )

            {

                  // 카운트만하나증가하고종료

                  ++words[i].count;

                  return;

            }

      }

 

      // 배열끝까지왔으면종료

      if ( *size == MAX_WORDS - 1 )

            return;

 

      // 끝에추가

      strcpy(words[*size].word, token);

      words[*size].count = 1;

      ++(*size);

}

 

여기서 질문이 있는데요..

교수님이 만약에 텍스트 파일의 크기가 몇십메가라면 일일이 배열에 저장한후에

비교 하는건 너무 무리라고..

 

해쉬함수를 써서 배열에 자리를 찾아서 하나씩 넣으라는데...

이게 먼소린지ㅜㅜ..

 

책에서 해쉬를찾아봤는데 코드는 한개도 없고 무슨 사전구조만 나오는데..

좀 도와주실수 잇으신가요?ㅜㅜ

 

감사합니다

전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호