📄 zjb.cpp
字号:
#include<iostream.h>
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define L 20 /*定义哈希表长*/
int K,m,j=0,p,s,z,t,y,e,low,high;int c;//int t[1]={1};int *e=t;
struct student{char name[20];int num;
char sex[20];int Math;
int English;int score;
}x[L],pivot[L],elem[L];
typedef struct HashTable
{
student elem[L];
}HashTable;
int a[20];
HashTable H;
void zhijie(int low,int high);
//*************************************************************************************
void createHash(HashTable &H,int p,int &c,int K) // 按学号查询
{ int t=0;
for (t=0;t<K;t++) {a[t]=x[t].num %K;}
for(int i=0;i<K;i++)
{ p=a[i];
for(int t=0;t<i;t++)
{ int c=0;
while(a[i]==a[t])
{ c++;
p=(p+c)%K;
a[i]=p;
}
}
H.elem[p].num=x[i].num;
for(int r=0;r<15;r++)
{H.elem[p].name[r]=x[i].name[r];
H.elem[p].sex[r]=x[i].sex[r];
}
H.elem[p].Math=x[i].Math;
H.elem[p].English=x[i].English;
H.elem[p].score=x[i].score;
}
}
int EQ(int m, int s)
{ if(m==s)
return 1;
else return 0;
}
void collision(int c)
{ z=(z+c)%K;
j++;
}
void search_Hash(HashTable H)
{
int m,c=0;
cout<<"请输入要查找学生基本信息的学号: "<<endl;
cin>>m;
z=m%K;
while ( j<K && !EQ(m,H.elem[z].num))
{
collision(++c); // 求得下一探查地址 p
}
if (EQ(m, H.elem[z].num))
{
cout<<"该学生的基本信息是: "<<endl;
cout<<"学号: "; cout<<H.elem[z].num<<endl;
cout<<"姓名: "; cout<<H.elem[z].name<<endl;
cout<<"性别: "; cout<<H.elem[z].sex<<endl;
cout<<"数学: "; cout<<H.elem[z].Math<<endl;
cout<<"英语: "; cout<<H.elem[z].English<<endl;
cout<<"总成绩: "; cout<<H.elem[z].score<<endl;
}
else cout<<"没有该学生的基本信息!"<<endl; // 查找不成功
}
void Search_Bin ( HashTable H, int K ) // 按学号利用折半查找学生基本信息
{
int low,high,mid,n=0;
zhijie(low,high);
int v;
cout<<"请输入要查找学生基本信息的学号: "<<endl;
cin>>v;
for(int i=0;i<K;i++)
{
low=0;high =K-1; // 置区间初值
while (low <= high&&n==0)
{ mid = (low + high)/2;
if (v==pivot[mid].num)
{ cout<<"该学生的基本信息是: "<<endl; cout<<mid<<endl;
cout<<"学号: "; cout<<pivot[mid].num<<endl;
cout<<"姓名: "; cout<<pivot[mid].name<<endl;
cout<<"性别: "; cout<<pivot[mid].sex<<endl;
cout<<"数学: "; cout<<pivot[mid].Math<<endl;
cout<<"英语: "; cout<<pivot[mid].English<<endl;
cout<<"总成绩: "; cout<<pivot[mid].score<<endl;
n++;
}
else if ( v<pivot[mid].num)
high = mid - 1; // 继续在前半区间进行查找
else low = mid + 1; // 继续在后半区间进行查找
} // 顺序表中不存在待查元素
}
if(n==0)
cout<<"没有该学生的基本信息!"<<endl;
}
//***************************************************************************************
void zhijie(int low,int high) /*直接插入排序*/
{ int k1;char g[15];
for(j=0;j<K-1;j++)
for(int i=0;i<K-1-j;i++)
if(pivot[i].num>pivot[i+1].num)
{ k1=pivot[i].num; pivot[i].num=pivot[i+1].num; pivot[i+1].num=k1;
k1=pivot[i].Math; pivot[i].Math=pivot[i+1].Math; pivot[i+1].Math=k1;
k1=pivot[i].English; pivot[i].English=pivot[i+1].English; pivot[i+1].English=k1;
k1=pivot[i].score; pivot[i].score=pivot[i+1].score; pivot[i+1].score=k1;
for(int z=0;z<15;z++)
{ g[z]=pivot[i].name[z]; pivot[i].name[z]=pivot[i+1].name[z];pivot[i+1].name[z]=g[z];
g[z]=pivot[i].sex[z]; pivot[i].sex[z]=pivot[i+1].sex[z];pivot[i+1].sex[z]=g[z];
}
}
}
void InsertSort()//插入排序
{ for (int i = 0; i < K; i++)
{
int t=x[i].num;
int m=x[i].Math;
int n=x[i].English;
int r=x[i].score;
int j = i;
while ((j > 0) && (x[j - 1].num > t)&&x[j - 1].Math>m&&x[j - 1].English>n&&x[j - 1].score>r)
{
x[j].num = x[j - 1].num;//交换顺序
x[j].Math = x[j - 1].Math;
x[j].English = x[j - 1].English;
x[j].score = x[j - 1].score;
--j;
}
x[j].num = t;x[j].Math=m; x[j].English=n; x[j].score=r;
}
cout<<"学生的学号排序: "<<endl;
for (i=0;i<K;i++) cout<<x[i].num<<endl;
cout<<"学生的数学成绩排序: "<<endl;
for (i=0;i<K;i++) cout<<x[i].Math<<endl;
cout<<"学生的英语成绩排序: "<<endl;
for (i=0;i<K;i++) cout<<x[i].English<<endl;
cout<<"学生的总成绩排序: "<<endl;
for (i=0;i<K;i++) cout<<x[i].score<<endl;
}
void main()
{
HashTable H;
int t;
while(1){
cout<<"1.请输入学生基本信息\n";
cout<<"2.哈希表查找\n";
cout<<"3.折半法查找\n";
cout<<"4.起泡排序\n";
cout<<"5.快速排序\n";
cout<<"0.退出\n";
cout<<"\n请选择以上的操作:\n";
cin>>t;
switch(t){
case 0:return;
case 1:
{cout<<"请输入学生的个数:"<<endl;
cin>>K;
for(int i=0;i<K;i++)
{
cout<<"输入学号: "; cin>>x[i].num; pivot[i].num=x[i].num;
cout<<"输入姓名: "; cin>>x[i].name; for(int z=0;z<15;z++) pivot[i].name[z]=x[i].name[z];
cout<<"输入性别: "; cin>>x[i].sex; for(z=0;z<15;z++) pivot[i].sex[z]=x[i].sex[z];
cout<<"输入数学成绩: "; cin>>x[i].Math; pivot[i].Math=x[i].Math;
cout<<"输入英语成绩: "; cin>>x[i].English;pivot[i].English=x[i].English;
x[i].score=x[i].Math+x[i].English;
pivot[i].score=x[i].score;
}
break; }
case 2:
{ createHash(H, p,c,K);
search_Hash(H);
break; }
case 3:
{ createHash(H, p,c,K);
Search_Bin ( H,K);
break; }
case 4:
{ createHash(H, p,c,K);
zhijie( low, high);
cout<<"学生的学号排序: "<<endl;
for(int i=0;i<K;i++) cout<<pivot[i].num<<endl;
cout<<"学生的数学成绩排序: "<<endl;
for( i=0;i<K;i++) cout<<pivot[i].Math<<endl;
cout<<"学生的英语成绩排序: "<<endl;
for( i=0;i<K;i++) cout<<pivot[i].English<<endl;
cout<<"学生的总成绩排序: "<<endl;
for( i=0;i<K;i++) cout<<pivot[i].score<<endl;
break;}
case 5:
{createHash(H, p,c,K);
InsertSort();
break; }
default:
cout<<"请确认选择项:\n";
}//end switch
}//end while
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -