#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_PERSONS 100000 // hash table size - experiment with different values
#define MAX_NAME_LENGTH 50
#define COUNTRY_CODE_LENGTH 4
#define AREA_CODE_LENGTH 4
#define LOCAL_NUMBER_LENGTH 8
// struct to store a persosn name and phone nummber details
typedef struct person {
char *first_name;
char *last_name;
char *fullName;
char *countryCode;
char *areaCode;
char *localNumber;
} person_struct;
// global variables for printing status infomation
int collision = 0;
int longest_probing_sequence = 0;
// The bad hash function! TODO: Implement a better hesh function!
int count_hash(int mod, char* fullName, int fullNameLength) {
int i;
int hash = 0;
for(i=0; i < fullNameLength; i++){
hash = (hash + fullName[i])%mod;
}
return hash;
}
/**
function to insert a person into the hash table
(maybe not the best way to do it, but will have to do for now)
the key used for a person is the first name and last name together
TODO: I this function you will find the probing method
You need to implement a better one.
*/
int insertPerson( person_struct *name_store,
char *fi_name,
char *la_name,
char *c_code,
char *a_code,
char *l_number,
int *allPersons) {
int i;
int hash,firsthash;
// Allocating memory for person information
int fiNameLength = strlen(fi_name);
char *tempFiName = malloc(fiNameLength*sizeof(char)+1);
int laNameLength = strlen(la_name);
char *tempLaName = malloc(laNameLength*sizeof(char)+1);
int coCodeLength = strlen(c_code);
char *tempCoCode = malloc(coCodeLength*sizeof(char)+1);
int arCodeLength = strlen(a_code);
char *tempArCode = malloc(arCodeLength*sizeof(char)+1);
int loNumberLength = strlen(l_number);
char *tempLoNumber = malloc(loNumberLength*sizeof(char)+1);
int fullNameLength = MAX_NAME_LENGTH*2;
char *tempFullName = malloc(fullNameLength*sizeof(char)+1);
strcpy(tempFiName, fi_name);
strcpy(tempLaName, la_name);
strcpy(tempCoCode, c_code);
strcpy(tempArCode, a_code);
strcpy(tempLoNumber, l_number);
//concatenating first name and last name into a variable for the key
strcpy(tempFullName, tempFiName);
strcat(tempFullName, tempLaName);
//changeing the key to capital letters
for (i=0; i < fullNameLength; i++) {
tempFullName[i] = toupper(tempFullName[i]);
}
tempFullName[i] = 0;
// counter for number of persons stored in the hash table
*allPersons = *allPersons+1;
// compute hash for the key
hash = count_hash(MAX_PERSONS, tempFullName, fullNameLength);
// insert person into hash table
firsthash = hash;
int probing = 0;
int stored = 0;
while (stored == 0) {
if (name_store[hash].fullName != 0) {
// Linear probing used a probing method
// TODO: Implement better probing method
hash = (hash+1)%MAX_PERSONS;
collision++;
probing++;
//Check if hashtable full
if (hash == firsthash) {
return 0;
}
}
else {
person_struct person;
person.first_name = tempFiName;
person.last_name = tempLaName;
person.fullName = tempFullName;
person.countryCode = tempCoCode;
person.areaCode = tempArCode;
person.localNumber = tempLoNumber;
name_store[hash] = person;
stored = 1;
}
}
if (probing > longest_probing_sequence) {
longest_probing_sequence = probing;
}
return 1;
}
/**
TODO: Implement function to find person
Note that a character array cannot be directly copied
Note also that you must use the same probing mehtod as in insert in case of collisions
@param name_storage - the the hash table where the persons have been stored
@param personToFind - the full name of person to find
@return 0 if person was not found
*/
int findPerson(person_struct *name_storage, char* personToFind) {
printf("\nSearching...\n");
return 0;
}
// main function
int main() {
FILE *personFile;
//person_struct persons[MAX_PERSONS+1];
// allocate memory for hash table and initialize needed variables
person_struct *persons = (person_struct *)malloc(sizeof(person_struct)*(MAX_PERSONS+1));
char firstName[MAX_NAME_LENGTH];
char lastName[MAX_NAME_LENGTH];
char countryCode[COUNTRY_CODE_LENGTH];
char areaCode[AREA_CODE_LENGTH];
char localNumber[LOCAL_NUMBER_LENGTH];
int personCount = 0;
double totaltime;
clock_t start,end;
int i;
int full = 0;
// get input file name from user
char fileName[500];
printf("Type filename: > ");
scanf("%s",fileName);
personFile = fopen(fileName,"r");
if (personFile == NULL){
printf("NO SUCH FILE!\n");
return 0;
}
char garbage[1000];
// measure time for inserting all persons into the hash table
start = clock();
// reade lines while not end of file
while (fscanf(personFile, "%[^,], %[^,], %[^,], %[^,], %[^\n]", firstName, lastName, countryCode, areaCode, localNumber) != EOF) {
// try to insert person into hash table
if (insertPerson(persons, firstName, lastName, countryCode, areaCode, localNumber, &personCount) == 0){
printf("Hash table full. File not processed completely!\n");
full = 1;
break;
}
if (fscanf(personFile, "%999[^a-zA-Z']", garbage) == EOF) {
// End of file garbage
break;
}
}
fclose(personFile);
end = clock();
// printing status information
if (full == 0) {
printf("Total number persons inserted: %d\n",personCount);
printf("\n\nFillrate is: %f%%\n", (personCount/(double)MAX_PERSONS)*100.00);
printf("Total number of collisions: %d\n", collision);
printf("Longest probing sequece: %d\n", longest_probing_sequence);
}
totaltime = (double)(end-start)/CLOCKS_PER_SEC;
printf("\n\nTime for inserting %f seconds\n", totaltime);
// measure time to find a person
start = clock();
// Searching for one person
char searchPersonFullName[MAX_NAME_LENGTH*2] = "Tian Alphonse JinchengAnguissola";
// use findPeso() function to get place of the hash table where the person should be
int place = findPerson(persons, searchPersonFullName);
// print detials of person if found, else inform that the person was not found
end = clock();
if (place != 0) {
printf("Found\n");
printf("%s ", persons[place].first_name);
printf("%s ", persons[place].last_name);
printf("%s ", persons[place].countryCode);
printf("%s ", persons[place].areaCode);
printf("%s\n", persons[place].localNumber);
}
else {
printf("Not found\n");
}
totaltime = (double)(end-start)/CLOCKS_PER_SEC;
printf("\n\nTime searching %f seconds\n", totaltime);
// Free all person elements from memory
for (i=0; i <= MAX_PERSONS; i++) {
if (persons[i].fullName != 0) {
free(persons[i].last_name);
free(persons[i].first_name);
free(persons[i].countryCode);
free(persons[i].areaCode);
free(persons[i].localNumber);
}
}
// Free the table
free(persons);
return 0;
}
注意!提交时:
•必要时压缩所有文件
•不要包含可执行文件
文件扩展名为。c
在文件名、注释和代码中使用英文
在Moodle中,你可以找到源代码文件phonebook_for_students.c
你将在这个练习中练习。此外,您将发现包含
不同数量的名字和电话号码(这些不是真实的名字和
数字,它们已经生成了)。您可以运行源代码文件。它要求
您需要输入一个文本文件。使用给定的文件。然后程序尝试全部插入
人员和他们的数字组成一个哈希表结构。一旦完成,它就会尝试
从哈希表中找一个人。这不会起作用,因为它没有
已经实现了。研究代码和注释以了解它是如何实现的
的工作原理。查看关于哈希表的幻灯片和视频来帮助你
理解。
1点-找到计算哈希值的函数。这不是一件好事
解决方案。你的任务是实现一个更好的哈希函数。观察
运行时间!越快越好。也要观察总计的打印输出
碰撞次数和最长探测序列。实验
不同的哈希函数。还可以尝试哈希表的大小。不要尝试
大文件与糟糕的哈希函数!这要花很长时间!
1点-找到探测方法。它使用线性探测作为探测
方法。您的任务是实现一种不同的探测方法。实验
不同的探测方法。你也可以尝试不同的哈希函数
不同的探测方法。观察这些如何影响碰撞的数量
以及探测序列的长度。还可以尝试不同大小的散列
表格观察运行时间。
1点-找到应该从哈希中找到一个人的函数
表格您的任务就是实现这个函数。重复使用已经有的东西
送给你。如果你不知道如何处理字符串,请寻求帮助
(char数组)。
在下一页,你会发现一个表,指定输入文件,数量
每个文件中的人,以及他们是否搜索人应该找到
在文件中。