#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ハッシュテーブル構造定義 */
enum { SUCCESS, FAIL} ;
enum { HASH_FULL, HASH_EMPTY } ;
typedef struct {
char key[ 1024 ] ;
char value[ 1024 ] ;
int empty; /* HASH_FULL:使用中 HASH_EMPTY:未使用 */
} HashRecord;
typedef struct {
int size;
HashRecord * table;
} Hash;
int HashAlloc( Hash*, int ) ;
void HashFree( Hash* ) ;
int HashAdd( Hash*, char *, char * ) ;
void HashDump( Hash* ) ;
int HashDelete( Hash*, char * ) ;
int HashGet( Hash*, char *, char * ) ;
int HashKey( Hash*, char * ) ;
//
( * ここに解答を書き加える * )
//// ハッシュテーブルのレコードを全て表示する(ハッシュが空でも表示).
void HashDump( Hash * hash) {
int i= 0 ;
for ( i = 0 ; i < hash-> size; i++ ) {
if ( hash-> table[ i] .empty == HASH_FULL) {
printf ( "%d: %s=%s\n " , i
, hash
-> table
[ i
] .
key , hash
-> table
[ i
] .
value ) ; } else {
printf ( "%d: %s\n " , i
, hash
-> table
[ i
] .
empty == HASH_EMPTY
? "empty" : "deleted" ) ; }
}
}
int HashKey( Hash * hash, char * key) {
int n= 0 ;
if ( n == 1 ) return key[ 0 ] % hash-> size;
return ( ( key[ 0 ] - 'A' + ( key[ n/ 2 - 1 ] - 'A' ) * 26 + ( key[ n- 2 ] - 'A' ) * 26 * 26 ) % hash-> size) ;
}
int main( void ) {
int size= 0 ;
char cmd, key[ 1024 ] , value[ 1024 ] ;
Hash hash;
// ハッシュテーブルの生成
printf ( "ハッシュテーブルの大きさを入力して下さい:" ) ; if ( size < 1 ) return FAIL; // 入力数エラー
if ( HashAlloc( & hash, size) == FAIL) return FAIL; // メモリ確保失敗
puts ( "* ハッシュテーブルを操作するコマンドを入力して下さい." ) ; puts ( "* キーに対応するデータの取得:g" ) ;
do {
switch ( cmd) {
case 'a' : /* データを格納 */
if ( HashAdd( & hash, key, value) == SUCCESS) {
printf ( "%s=%sを格納しました.\n " , key
, value
) ; } else { /* 衝突 */
printf ( "既に同じキーを持つデータが存在します.\n " ) ; }
break ;
case 'd' : /* データを表示 */
HashDump( & hash) ;
break ;
case 'x' : /* キーを削除 */
if ( HashDelete( & hash, key) == SUCCESS) {
}
else {
printf ( "%sは登録されていません.\n " , key
) ; }
break ;
case 'g' : /* キーに対応するデータを取得 */
if ( HashGet( & hash, key, value) == SUCCESS) {
printf ( "%sの血液型は%sです.\n " , key
, value
) ; } else {
printf ( "%sは登録されていません.\n " , key
) ; }
break ;
case 'q' : /* 終了 */
break ;
case '\n ' : /* 改行 */
case '\r ' : /* 復帰 */
break ;
default : /* 入力エラー */
break ;
}
} while ( cmd != 'q' ) ;
HashDump( & hash) ; // 解放前の出力
HashFree( & hash) ; // メモリ解放
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKLyog44OP44OD44K344Ol44OG44O844OW44Or5qeL6YCg5a6a576pICovCmVudW0ge1NVQ0NFU1MsRkFJTH07CmVudW0geyBIQVNIX0ZVTEwsIEhBU0hfRU1QVFkgfTsKCnR5cGVkZWYgc3RydWN0IHsKCWNoYXIga2V5WzEwMjRdOwoJY2hhciB2YWx1ZVsxMDI0XTsKCWludCBlbXB0eTsJLyogSEFTSF9GVUxMOuS9v+eUqOS4rSBIQVNIX0VNUFRZOuacquS9v+eUqCAqLwp9SGFzaFJlY29yZDsKCnR5cGVkZWYgc3RydWN0IHsKCWludCBzaXplOwoJSGFzaFJlY29yZCAqdGFibGU7Cn1IYXNoOwoKaW50IEhhc2hBbGxvYyhIYXNoKiwgaW50KTsKdm9pZCBIYXNoRnJlZShIYXNoKik7CmludCBIYXNoQWRkKEhhc2gqLCBjaGFyKiwgY2hhciopOwp2b2lkIEhhc2hEdW1wKEhhc2gqKTsKaW50IEhhc2hEZWxldGUoSGFzaCosIGNoYXIqKTsKaW50IEhhc2hHZXQoSGFzaCosIGNoYXIqLCBjaGFyKik7C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vKiDntYLkuoYgKi8KCQkJCXB1dHMoIuODl+ODreOCsOODqeODoOOCkue1guS6huOBl+OBvuOBme+8jiIpOwoJCQkJYnJlYWs7CgkJCWNhc2UgJ1xuJzoJLyog5pS56KGMICovCgkJCWNhc2UgJ1xyJzoJLyog5b6p5biwICovCgkJCQlicmVhazsKCQkJZGVmYXVsdDoJLyog5YWl5Yqb44Ko44Op44O8ICovCgkJCQlwdXRzKCLjgrPjg57jg7Pjg4njgYzmraPjgZfjgY/jgYLjgorjgb7jgZvjgpPvvI4iKTsKCQkJCWJyZWFrOwoJCX0KCX0gd2hpbGUgKGNtZCAhPSAncScpOwoJCglIYXNoRHVtcCgmaGFzaCk7CS8vIOino+aUvuWJjeOBruWHuuWKmwoJSGFzaEZyZWUoJmhhc2gpOwkvLyDjg6Hjg6Ljg6rop6PmlL4JCgoJCglyZXR1cm4gMDsKfQo=
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