fork(3) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* ハッシュテーブル構造定義 */
  6. enum {SUCCESS,FAIL};
  7. enum { HASH_FULL, HASH_EMPTY };
  8.  
  9. typedef struct {
  10. char key[1024];
  11. char value[1024];
  12. int empty; /* HASH_FULL:使用中 HASH_EMPTY:未使用 */
  13. }HashRecord;
  14.  
  15. typedef struct {
  16. int size;
  17. HashRecord *table;
  18. }Hash;
  19.  
  20. int HashAlloc(Hash*, int);
  21. void HashFree(Hash*);
  22. int HashAdd(Hash*, char*, char*);
  23. void HashDump(Hash*);
  24. int HashDelete(Hash*, char*);
  25. int HashGet(Hash*, char*, char*);
  26. int HashKey(Hash*, char*);
  27. //
  28. (* ここに解答を書き加える *)
  29.  
  30. //// ハッシュテーブルのレコードを全て表示する(ハッシュが空でも表示).
  31. void HashDump(Hash *hash) {
  32. int i=0;
  33.  
  34. for (i = 0; i < hash->size; i++) {
  35. if (hash->table[i].empty == HASH_FULL) {
  36. printf("%d: %s=%s\n", i, hash->table[i].key, hash->table[i].value);
  37. } else {
  38. printf("%d: %s\n", i, hash->table[i].empty == HASH_EMPTY ? "empty" : "deleted");
  39. }
  40. }
  41. }
  42.  
  43. int HashKey(Hash *hash, char *key){
  44. int n=0;
  45.  
  46. n = strlen(key);
  47. if (n == 1) return key[0] % hash->size;
  48. return ((key[0]-'A'+(key[n/2-1]-'A')*26+(key[n-2]-'A')*26*26) % hash->size);
  49. }
  50.  
  51. int main(void) {
  52. int size=0;
  53. char cmd, key[1024], value[1024];
  54. Hash hash;
  55.  
  56. // ハッシュテーブルの生成
  57. printf("ハッシュテーブルの大きさを入力して下さい:");
  58. scanf("%d", &size);
  59. if (size < 1) return FAIL;// 入力数エラー
  60. if (HashAlloc(&hash, size) == FAIL) return FAIL; // メモリ確保失敗
  61.  
  62. puts("* ハッシュテーブルを操作するコマンドを入力して下さい.");
  63. puts("* データを格納:a");
  64. puts("* キーを削除:x");
  65. puts("* キーに対応するデータの取得:g");
  66. puts("* ハッシュテーブルを表示:d");
  67. puts("* 終了:q");
  68.  
  69. do {
  70. printf(":");
  71. scanf("%c", &cmd);
  72.  
  73. switch (cmd) {
  74. case 'a': /* データを格納 */
  75. printf("名前を入力して下さい:");
  76. scanf("%s", key);
  77. printf("血液型を入力して下さい:");
  78. scanf("%s", value);
  79.  
  80. if (HashAdd(&hash, key, value) == SUCCESS) {
  81. printf("%s=%sを格納しました.\n", key, value);
  82. } else { /* 衝突 */
  83. printf("既に同じキーを持つデータが存在します.\n");
  84. }
  85. break;
  86. case 'd': /* データを表示 */
  87. HashDump(&hash);
  88. break;
  89. case 'x': /* キーを削除 */
  90. printf("誰を削除しますか?:");
  91. scanf("%s", key);
  92. if(HashDelete(&hash, key) == SUCCESS) {
  93. printf("%sを削除しました.\n", key);
  94. }
  95. else {
  96. printf("%sは登録されていません.\n", key);
  97. }
  98. break;
  99. case 'g': /* キーに対応するデータを取得 */
  100. printf("名前を入力して下さい:");
  101. scanf("%s", key);
  102. if (HashGet(&hash, key, value) == SUCCESS) {
  103. printf("%sの血液型は%sです.\n", key, value);
  104. } else {
  105. printf("%sは登録されていません.\n", key);
  106. }
  107. break;
  108. case 'q': /* 終了 */
  109. puts("プログラムを終了します.");
  110. break;
  111. case '\n': /* 改行 */
  112. case '\r': /* 復帰 */
  113. break;
  114. default: /* 入力エラー */
  115. puts("コマンドが正しくありません.");
  116. break;
  117. }
  118. } while (cmd != 'q');
  119.  
  120. HashDump(&hash); // 解放前の出力
  121. HashFree(&hash); // メモリ解放
  122.  
  123.  
  124. return 0;
  125. }
  126.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:28: error: stray ‘\343’ in program
prog.c:28: error: stray ‘\201’ in program
prog.c:28: error: stray ‘\223’ in program
prog.c:28: error: stray ‘\343’ in program
prog.c:28: error: stray ‘\201’ in program
prog.c:28: error: stray ‘\223’ in program
prog.c:28: error: stray ‘\343’ in program
prog.c:28: error: stray ‘\201’ in program
prog.c:28: error: stray ‘\253’ in program
prog.c:28: error: stray ‘\350’ in program
prog.c:28: error: stray ‘\247’ in program
prog.c:28: error: stray ‘\243’ in program
prog.c:28: error: stray ‘\347’ in program
prog.c:28: error: stray ‘\255’ in program
prog.c:28: error: stray ‘\224’ in program
prog.c:28: error: stray ‘\343’ in program
prog.c:28: error: stray ‘\202’ in program
prog.c:28: error: stray ‘\222’ in program
prog.c:28: error: stray ‘\346’ in program
prog.c:28: error: stray ‘\233’ in program
prog.c:28: error: stray ‘\270’ in program
prog.c:28: error: stray ‘\343’ in program
prog.c:28: error: stray ‘\201’ in program
prog.c:28: error: stray ‘\215’ in program
prog.c:28: error: stray ‘\345’ in program
prog.c:28: error: stray ‘\212’ in program
prog.c:28: error: stray ‘\240’ in program
prog.c:28: error: stray ‘\343’ in program
prog.c:28: error: stray ‘\201’ in program
prog.c:28: error: stray ‘\210’ in program
prog.c:28: error: stray ‘\343’ in program
prog.c:28: error: stray ‘\202’ in program
prog.c:28: error: stray ‘\213’ in program
prog.c:28: error: expected identifier or ‘(’ before ‘)’ token
prog.c: In function ‘main’:
prog.c:58: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:71: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:76: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:78: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:91: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:101: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
stdout
Standard output is empty