#include<iostream.h>
#include<string.h>
#define N 40
enum ResultCode{Success,Duplicate,NotPresent,Underflow,Overflow,NeverUsed};
class Student
{
public:
int num;
char name[10];
char sex[3];
int age;
Student(int n=0,char *na=" ",char *s=" ",int a=0);
friend istream &operator>>(istream &in,Student &s);
friend ostream &operator<<(ostream &out,const Student &s);
friend bool operator==(Student stu1,Student stu2);
};
Student::Student(int n,char *na,char *s,int a)
{
num=n;
strcpy(name,na);
strcpy(sex,s);
age=a;
}
istream &operator>>(istream &in,Student &s)
{
in>>s.num>>s.name>>s.sex>>s.age;
return in;
}
ostream &operator<<(ostream &out,const Student &s)
{
out<<s.num<<" "<<s.name<<" "<<s.sex<<" "<<s.age<<endl;
return out;
}
bool operator==(Student stu1,Student stu2)
{
if(stu1.num==stu2.num)
return true;
return false;
}
template<class T>
class DynamicSet
{
public:
virtual ResultCode Search(T& x)const=0;
virtual ResultCode Insert(T& x)=0;
virtual ResultCode Remove(T& x)=0;
};
template<class T>
class HashTable:public DynamicSet<T>
{
public:
HashTable(int divitor=N);
~HashTable(){delete []ht;delete []empty;}
ResultCode Search(T& x)const;
ResultCode Insert(T& x);
ResultCode Remove(T& x);
void Print();
private:
ResultCode Find(T& x,int& pos)const;
int M;
T *ht;
bool *empty;
};
template<class T>
HashTable<T>::HashTable(int divitor)
{
M=divitor;
ht=new T[M];
empty=new bool[M];
for(int i=0;i<M;i++)
empty[i]=true;
for(i=0;i<M;i++)
ht[i]=NeverUsed;
}
template<class T>
ResultCode HashTable<T>::Find(T& x,int& pos)const
{
pos=x.num%11;
int i=pos,j=-1;
do{
if(ht[pos]==NeverUsed&&j==-1)
j=pos;
if(empty[pos])
break;
if(ht[pos]==x)
{
x=ht[pos];
return Success;
}
pos=(pos+x.num%9+1)%11;
}while(pos!=i);
if(j==-1)
return Overflow;
pos=j;
return NotPresent;
}
template<class T>
ResultCode HashTable<T>::Search(T& x)const
{
int pos;
if(Find(x,pos)==Success)
return Success;
return NotPresent;
}
template<class T>
ResultCode HashTable<T>::Insert(T& x)
{
int pos;
ResultCode result=Find(x,pos);
if(result==NotPresent)
{
ht[pos]=x;
empty[pos]=false;
return Success;
}
return Overflow;
}
template<class T>
ResultCode HashTable<T>::Remove(T& x)
{
int pos;
if(Find(x,pos)==Success)
{
ht[pos]=NeverUsed;
return Success;
}
return NotPresent;
}
template<class T>
void HashTable<T>::Print()
{
cout<<"学号 姓名 性别 年龄 "<<endl;
for(int i=0;i<N;i++)
{
if(!(ht[i]==NeverUsed))
cout<<ht[i];
}
}
int main()
{
int i,num,n;
ResultCode result;
Student s;
HashTable<Student> ht;
cout<<"你要输入的学生人数n:"<<endl;
cin>>n;
cout<<"输入学生信息:"<<endl<<"学号 姓名 性别 年龄:"<<endl;
for(i=0;i<n;i++)
{
cin>>s;
result=ht.Insert(s);
if(result==Duplicate)
cout<<"已存在"<<endl;
else
if(result==Overflow)
cout<<"表满"<<endl;
else
cout<<"插入成功"<<endl;
}
cout<<"原始学生信息:"<<endl;
ht.Print();
cout<<"输入你要查找的学生的学号"<<endl;
cin>>num;
Student s1(num);
result=ht.Search(s1);
if(result==NotPresent)
cout<<"不存在"<<endl;
else
cout<<"查找成功"<<endl<<s<<endl;
cout<<"输入你要删除的学生的学号"<<endl;
cin>>num;
Student s2(num);
result=ht.Remove(s2);
if(result==Success)
cout<<"删除成功"<<endl;
else
cout<<"不存在"<<endl;
cout<<endl<<"输出删除后学生信息"<<endl<<endl;
ht.Print();
return 0;
}