//功能,利用哈希表制作电话查询系统,根据姓名查询电话
//建立了哈希表之后,我想打印出这个表,但是没输出结果
//主要是C语言,部分有C++,用了ifstream
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
#define MAXSIZE 5
#define namelength 20
#define phonelength 15
typedef struct
{
char name[namelength];
char phone[phonelength];
}Person[MAXSIZE];
typedef struct{
Person per;
int number;
int sign[MAXSIZE];
int size;
}Hash;
int D[12]={1,2,3,4,5,6,7,8,9,10,11,12};
void Create_Hash(char* File_name,Hash &H)
//File_name表示的是文件名字
{
void InsertHash_Name(Hash &H,char name[],char phone[]);
ifstream in;
char name[namelength]=””;
char phone[phonelength]=””;
in.open(File_name);
if(in.fail())
{
printf(“文件打开失败!\n”);
exit(1);
}
while(!in.eof()){
char str[100];
strcpy(name,””);
strcpy(phone,””);
in.getline(str,100,””\n””);
if(str[0]==””*””)
break;//结束文件
for(int i=0;str[i]!=”” “”;i++)
name[i]=str[i];
while(str[i]==”” “”)
i++;
for(int j=0;str[i]!=”” “”;j++,i++)
phone[j] = str[i];
InsertHash_Name(H,name,phone);
delete []str;
}
in.close();
}
void InsertHash_Name(Hash &H,char name[namelength],char phone[phonelength])
{
int collision(Hash H,int key,int i);
int hash(char name[namelength]);
int i=0; //记录冲突的次数
int key = hash(name);
while(H.sign[key] == 1)
{
key=collision(H,key,i++);
}
if(key == -1)
exit(1);
H.number++;
strcpy(H.per[key].name,name);
strcpy(H.per[key].phone,phone);
H.sign[key] = 1;
}
//处理冲突
int collision(Hash H,int key,int i){
int x;
if(H.sign[key])
{
x=(key+D[i]) % MAXSIZE;
return x;
}
return -1;
}
int hash(char name[namelength])
{
int h;
int temp;
char *p;
h=0;
for(p =name; *p; p++){
temp = (int)*p;
h = h + temp;
}
h=h % MAXSIZE;
return h;
}
void main(){
void Create_Hash(char* File_name,Hash &H);
Hash H;
int j,s;
for(j=0;j<MAXSIZE;j++){
H.sign[j] = 0;
}
H.number = 0;
H.size = MAXSIZE;
char * Filename = “1.txt”;
Create_Hash(Filename,H);
for(s = 0;s<H.size;s++){
printf(“%s%s\n”,H.per[s].name,H.per[s].phone);
}
}
//建立了哈希表之后,我想打印出这个表,但是没输出结果
//主要是C语言,部分有C++,用了ifstream
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>
#define MAXSIZE 5
#define namelength 20
#define phonelength 15
typedef struct
{
char name[namelength];
char phone[phonelength];
}Person[MAXSIZE];
typedef struct{
Person per;
int number;
int sign[MAXSIZE];
int size;
}Hash;
int D[12]={1,2,3,4,5,6,7,8,9,10,11,12};
void Create_Hash(char* File_name,Hash &H)
//File_name表示的是文件名字
{
void InsertHash_Name(Hash &H,char name[],char phone[]);
ifstream in;
char name[namelength]=””;
char phone[phonelength]=””;
in.open(File_name);
if(in.fail())
{
printf(“文件打开失败!\n”);
exit(1);
}
while(!in.eof()){
char str[100];
strcpy(name,””);
strcpy(phone,””);
in.getline(str,100,””\n””);
if(str[0]==””*””)
break;//结束文件
for(int i=0;str[i]!=”” “”;i++)
name[i]=str[i];
while(str[i]==”” “”)
i++;
for(int j=0;str[i]!=”” “”;j++,i++)
phone[j] = str[i];
InsertHash_Name(H,name,phone);
delete []str;
}
in.close();
}
void InsertHash_Name(Hash &H,char name[namelength],char phone[phonelength])
{
int collision(Hash H,int key,int i);
int hash(char name[namelength]);
int i=0; //记录冲突的次数
int key = hash(name);
while(H.sign[key] == 1)
{
key=collision(H,key,i++);
}
if(key == -1)
exit(1);
H.number++;
strcpy(H.per[key].name,name);
strcpy(H.per[key].phone,phone);
H.sign[key] = 1;
}
//处理冲突
int collision(Hash H,int key,int i){
int x;
if(H.sign[key])
{
x=(key+D[i]) % MAXSIZE;
return x;
}
return -1;
}
int hash(char name[namelength])
{
int h;
int temp;
char *p;
h=0;
for(p =name; *p; p++){
temp = (int)*p;
h = h + temp;
}
h=h % MAXSIZE;
return h;
}
void main(){
void Create_Hash(char* File_name,Hash &H);
Hash H;
int j,s;
for(j=0;j<MAXSIZE;j++){
H.sign[j] = 0;
}
H.number = 0;
H.size = MAXSIZE;
char * Filename = “1.txt”;
Create_Hash(Filename,H);
for(s = 0;s<H.size;s++){
printf(“%s%s\n”,H.per[s].name,H.per[s].phone);
}
}
解决方案:41分
void Create_Hash(char* File_name,Hash &H)
//File_name表示的是文件名字
{
void InsertHash_Name(Hash &H,char name[],char phone[]);
ifstream in;
char name[namelength]=””;
char phone[phonelength]=””;
in.open(File_name);
if(in.fail())
{
printf(“文件打开失败!\n”);
exit(1);
}
while(!in.eof()){
char str[100];
//strcpy(name,””);//并不能将name全部写为””\0″”
//strcpy(phone,””);//并不能将phone全部写为””\0″”
in.getline(str,100,””\n””);
if(str[0]==””*””)
break;//结束文件
for(int i=0;str[i]!=”” “”;i++)
name[i]=str[i];
name[i+1]=0;//末尾加””\0″”
while(str[i]==”” “”)
i++;
for(int j=0;str[i]!=”” “”&&str[i]!=””\0″”;j++,i++)//str的末尾没有”” “”,但有””\0″”
phone[j] = str[i];
phone[j+1]=0;//末尾加””\0″”
InsertHash_Name(H,name,phone);
//delete []str; //无new,无delete
}
in.close();
}
//File_name表示的是文件名字
{
void InsertHash_Name(Hash &H,char name[],char phone[]);
ifstream in;
char name[namelength]=””;
char phone[phonelength]=””;
in.open(File_name);
if(in.fail())
{
printf(“文件打开失败!\n”);
exit(1);
}
while(!in.eof()){
char str[100];
//strcpy(name,””);//并不能将name全部写为””\0″”
//strcpy(phone,””);//并不能将phone全部写为””\0″”
in.getline(str,100,””\n””);
if(str[0]==””*””)
break;//结束文件
for(int i=0;str[i]!=”” “”;i++)
name[i]=str[i];
name[i+1]=0;//末尾加””\0″”
while(str[i]==”” “”)
i++;
for(int j=0;str[i]!=”” “”&&str[i]!=””\0″”;j++,i++)//str的末尾没有”” “”,但有””\0″”
phone[j] = str[i];
phone[j+1]=0;//末尾加””\0″”
InsertHash_Name(H,name,phone);
//delete []str; //无new,无delete
}
in.close();
}