完美散列函数(一)
来源:互联网 发布:淘宝怎么开店步骤图解 编辑:程序博客网 时间:2024/04/28 10:30
// cxy1.cpp : Defines the entry point for the console application.//#include "stdafx.h"/*--- birthday.c --------------------------- Listing 3-1 -------- * Reads input file of birthdays and lists any duplicates. * Uses a simple hash table to identify the duplicates. * * Input records consists of lines of text of the form * MMDDName where MM = month, DD = day, Name. For example: * 0212Abraham Lincoln * Note: for simplicity, no error checking is done on records. *-------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <string.h>#define TABLE_SIZE 366 /* Maximum days in a year */FILE *fin; /* Birthday file */int NameCount, /* Number of names read */ DupeCount; /* Number of dupes found */int hash_birthday ( char * ); /* Our hash function */#ifdef __STDC__ /* in ANSI C, there's no strdup() *//* the source code for this function is discussed in Ch. 2 */char *strdup ( const char * ); /* string duplication */#endifint main ( ){ char buffer[128]; /* where the records will be read */ int hash_value; /* the hash value we will compute */ char *name; /* pointer to birthday name */ char *Table[TABLE_SIZE]; /* table of birthday folks */ int i; /* subscript to init the table */ if (( fin = fopen ( "rt.txt", "rt" )) == NULL ) { exit ( EXIT_FAILURE ); } for ( i = 0; i < TABLE_SIZE; i++ ) Table[i] = NULL; while ( ! feof ( fin )) { if ( fgets ( buffer, 128, fin ) == NULL ) break; /* get rid of the '\n' at end of record */ buffer [strlen ( buffer ) - 1] = '\0'; NameCount += 1; hash_value = hash_birthday ( buffer ); name = strdup ( buffer + 4 ); if ( Table[hash_value] == NULL ) /* No duplicate, */ { /* so add name. */ Table[hash_value] = name; continue; } else { /* Is duplicate, */ DupeCount += 1; /* so tell 'em. */ printf ( "%s and %s have the same birthday.\n", name, Table[hash_value] ); continue; } } if ( DupeCount == 0 ) { printf ( "No duplicate matches found among %d people.\n", NameCount ); if ( NameCount > 50 ) printf ( "How rare!\n" ); } else printf ( "Among %d people, %d matches were found\n", NameCount, DupeCount ); return ( EXIT_SUCCESS );}/*--------------------------------------------------------------- * A simple hash algorithm. It converts the month and day * to the number of the day in the year. Adds the day in the * month to the number of days elapsed before that month began. *-------------------------------------------------------------*/int hash_birthday ( char *data ){ const int days_elapsed[12] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }; int mm, dd; char record [128]; /* where we store our copy of the data */ strcpy ( record, data ); /* make our own copy */ record [4] = '\0'; /* Mark the spot where the name begins */ dd = atoi ( record + 2 ); /* Convert the day to an int */ record [2] = '\0'; /* Mark off the spot where day begins */ mm = atoi ( record ); /* Convert the month to an int */ /* since mm is going to be an index into days_elapsed, * it must be checked for the correct range. */ if ( mm < 1 || mm > 12 ) { fprintf ( stderr, "Error in record for %s: %02d/%02d\n", (char *) data + 4, mm, dd ); exit ( EXIT_FAILURE ); } return ( days_elapsed[mm - 1] + dd );}
查询生日同一天的代码
1017My sister0212Abraham Lincoln0704USA0707Ringo Starr0214St Valentine0531Johnstown Flood of 18890725Andrea Doria sinks 19560728Plane hits Empire State Bldg 19450902Great Fire of London begins 16660911Hurricane Iniki 19921017San Francisco Quake 19890327My brother
0 0
- 完美散列函数(一)
- 散列学习(一)
- Hash(散列函数)
- 散列表之完美散列
- 散 列-散列函数
- 散列/散列函数
- 散列表基本概念以及常见散列函数构造方法(一)
- 散列表(一):散列表概念、 散列函数构造方法、 常见字符串哈希函数(测试冲突)
- 散列表(一):散列表概念、 散列函数构造方法、 常见字符串哈希函数(测试冲突)
- 散列函数
- 散列函数
- 散列函数
- 关于散列函数
- 单向散列函数
- 单向散列函数
- 构造散列函数
- 散列函数
- 单向散列函数
- java相关环境配置
- 实时监听input值改变
- shell脚本获取系统相关信息
- 调用系统提示音
- 新版一年级上册数学期末试卷
- 完美散列函数(一)
- 二年级语文上册多音字同音字形近字复习练习(北师大版)
- STM32CAN过滤器的作用
- Qt添加右键菜单
- linux下3TB硬盘的分区问题
- Install httpd and mod_ssl on linux
- 一切在慢慢中蜕变
- test(software)
- cocos2dx之文本渲染(CCLabelAtlas,CCLableBMFont,CCLabelTTF)