电话查询费下载
//设???计?哈t希?ê表À?ª实º?Ì现?电Ì?话?ã号?码?查??询??系?Ì统ª3
//main.cpp
//
//#include "stdio.h"
//#include "stdlib.h"
#include"StdAfx.h"
#include
#include
#include
usingnamespace std;
ifstream fin; ofstream fout;
int D[]={3,5,8,11,13,14,19,21};//预?è定??再??随?机??数ºy
int K;//当Ì?À前?ã数ºy据Y个?数ºy int N;//哈t希?ê表À?ª的Ì?长?è度?? char* sign;//判D断?是º?否??有?D冲?突ª?的Ì?标À?º志?
struct Data
{
string name;
string phone; string address; };
Data* dat;
int Random(int key,int i) {
int h;
if(sign[key]=='1')
{
h=(key+D[i])%N; return h;
}
return -1;
}
void Hashname(string name,string phone,string address)
{
int i=0;
int key;
char *p;
for(key=0, p =&name[0]; *p ; p++)
key = key + *p; key = key % 42; while(sign[key]=='1')
{
key=Random(key,i+1);
}
if(key==-1) exit(1); K++;
dat[key].name=name; dat[key].address=address; dat[key].phone=phone; sign[key] = '1';
}
int Rehash(int key,string str) {
int h;
int num1=(str[0]-'0')*1000+(str[1]-'0')*100+(str[2]-'0')*10+(str[3]-'0');
int num2=(str[4]-'0')*1000+(str[5]-'0')*100+(str[6]-'0')*10+(str[7]-'0');
int num3=(str[8]-'0')*100+(str[9]-'0')*10+(str[10]-'0');
h=num1+num2+num3;
h=(h+key)%N;
return h;
}
void Hashphone(string name,string phone,string address)
{
int key;
char *p;
for(key=0, p =&phone[0]; *p ; p++)
key = key + *p;
key = key % 42;
while(sign[key]=='1')
{
key=Rehash(key,phone); }
K++;
dat[key].name=name; dat[key].address=address; dat[key].phone=phone; sign[key] = '1';
}
void Outfile(string name,int key) {
if((key==-1)||(sign[key]=='0'))
{
fout.open("out.txt");
if(fout.fail())
{
cout<<"文件打开失败~"<122)i++;
for(;str[i]!=' ';i++)
name+=str[i];
while(str[i]==' ')i++;
for(int j=0;str[i]!=' ';j++,i++) phone+=str[i];
while(str[i]==' ')i++;
for(int j=0;str[i]!=',';j++,i++) address+=str[i];
if(n==1)
Hashname(name,phone,address);
else
Hashphone(name,phone,address);
delete []str;
}
fin.close();
}
int Findname(string name) {
int i=0;
int j=1;
int key;
char *p;
for(key=0, p =&name[0]; *p ; p++)
key = key + *p;
key = key % 42;
while(sign[key]=='1'&&(dat[key].name != name))
{
key=Random(key,i+1); j++;
if(j>=K) return -1;
}
return key;
}
int Findphone(string phone)
{
int key;
char *p;
for(key=0, p =&phone[0]; *p ; p++)
key = key + *p; key = key % 42; int j=1;
while(sign[key]=='1'&&(dat[key].phone !=phone))
{
key = Rehash(key,phone);
j++;
if(j>=K) return -1;
}
return key;
}
void main()
{
K = 0;
N = 50;
int k;
int ch;
char* Fname;
sign = newchar[N];
dat = new Data[N]; for(int i=0;i>k;
switch(k)
{
case 0:
return;
case 1:
Fname="old.txt";break;
case 2:
Rafile();
Fname="new.txt";break;
default:
cout<<"输入序号有误,退出程序,"<>ch;
if(ch!=1&&ch!=2)
cout<<"输入序号有误~"<>choice;
switch(choice)
{
case 1:
{
int key1;
string name;
cout<<"请输入姓名:";
cin>>name;
key1 = Findname(name); Outfile(name,key1); Outhash(key1);
}break;
case 2:
{
for(int i=0;i>choice;
switch(choice)
{
case 1:
{
int key2;
string phone;
cout<<"请输入11位电话号码:";
do
{
cin>>phone;
if(strlen(&phone[0])!=11) cout<<"电话号码不正确,请重新输入"; }while(strlen(&phone[0])!=11);
key2=Findphone(phone); Outfile(phone,key2); Outhash(key2);
}break;
case 2:
{
for(int i=0;i