// 165 名前: デフォルトの名無しさん  Mail: sage 投稿日: 2012/12/11(火) 11:07:17.44
// よろしくお願いします。

// [1] 授業単元： Programming C
// [2] 問題文(含コード&リンク)：
// 1. argv[1]を使って wordlist(txtファイル) を読み込み、Hashtableに格納。
// サンプルtxtファイル -> http://s...content-available-to-author-only...s.jp/~c_cpp_homework/cgi-bin/joyful/joyful.cgi の 386.txt, 387.txt

// ※二重ハッシュ法を使う
// ※アポストロフィや .等を含んでいた場合、別の単語として扱う。ただ大文字小文字は区別しない。
// 例) grindstone’s と grindstones は別の単語。Armour と armour は同じ単語として扱う。
// ※単語数の約二倍のサイズのHashTableと素数を使う。ただ、単語数はファイル読み込み事前にはわからないものとする。
// ※単語の長さの最大値をあらかじめ決め打ちしない。

// 2.
// ・それぞれのセルでアクセスされた回数をカウントしていき、表示する。

// [3] 環境
// 　[3.1] Cent OS
// 　[3.2] gcc
// 　[3.3] C
// [4] 2012年12月12日
// [5] その他の制限：
// ・グローバル変数を使わない

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

// prototype
int h(char *);
int p(char *);

int h(char *s)
{
  int r = 0;
  while (*s) {
    *s = tolower(*s);
    r = r * 26 + (*s - 'a');
    r %= 11;
    ++s;
  }
  return r;
}

int p(char *s){

}

int main(int argc, char const *argv[])
{
  FILE *fp;
  int hashTableSize, len = 0, lenMax = 0;
  char c, **hashTable, *buf;

  // 単語数カウント
  fp = fopen(argv[1], "r");
  while ((c = fgetc(fp)) != EOF) {
    if (c == '\n') {
      ++hashTableSize;
      if (lenMax < len)
        lenMax = len;
      len = 0;
    } else ++len;
  }

  // HashTableサイズ決め
  {
    int n;
    hashTableSize *= 2;
    while (1) {
      for (n = 2; (n * n < hashTableSize) && (hashTableSize % n); ++n);
      if (hashTableSize % n) /* 素数 */
        break;
      ++hashTableSize;
    }
  }
  printf("hashTableSize = %d\n", hashTableSize);
  printf("lenMax = %d\n", lenMax);
  hashTable = (char **)malloc(sizeof(char *)*hashTableSize);
  buf = (char *)malloc(sizeof(char ) * (lenMax + 2));

  rewind(fp);
  while ((fgets(buf, lenMax, fp)) != NULL)
  {
      h(buf, hashTableSize);
  }

  return 0;
}
