📄 ch7_3u.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ch7_3u.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
#define max 10 //定义最大值为10
int search[max] = {0};
int hash(int); //定义哈希方法函数
int clsn(int); //定义冲突处理函数
int k,a=0;
int i;
bool sv,t;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
WinExec("cd1_1p.exe",NULL); //打开简易计算器执行文件
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
WinExec("cd2_2p.exe",NULL); //打开工资查询修改执行文件
}
//---------------------------------------------------------------------------
void __fastcall TForm1::einKeyUp(TObject *Sender, WORD &Key,
TShiftState Shift) //数据输入
{
if (Key==13)
{
int idx=0,c=0;
if (a < max) //如果有空间则可存储
{
k=StrToInt(ein->Text);
idx=hash(k); //调用哈希函数以取得索引值
sv=false; //默认为未存
while(sv==false)
{
if(search[idx]==0) //如果索引位置的数组值为空
{
search[idx]=k; //储存新生成数据
sg->Cells[idx+1][1]=search[idx];
if (c!=0)
ShowMessage("造成了 "+IntToStr(c)+" 次冲突");
a++; //存入数据计数
sv=true; //此笔数据已存
}
else
{
idx=clsn(idx); //调用冲突解决函数
c++; //冲突解决累加
}
}
ein->Text="";
}
else
ShowMessage("存储空间已满");
}
}
//---------------------------------------------------------------------------
int hash(int k) //哈希规则函数,取余数
{
return k%max;
}
//---------------------------------------------------------------------------
int clsn(int id) //冲突解决函数,往后移动
{
if(id==max-1) //若冲突处为数组末端,则将目标移至最前端
return 0;
else
return id+1; //解决方案:数值位于冲突处后方
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
sg->Cells[0][0]="存储位";
sg->Cells[0][1]="键入值";
for(i=1;i<=10;i++)
sg->Cells[i][0]="第 "+IntToStr(i-1)+ " 位";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) //哈希搜索
{
int idx=0,sh=1,c1=0;
k=StrToInt(ein->Text); //获取搜索值
idx=hash(k); //调用哈希函数以取得索引值
lout->Caption="";
sout->Caption="";
while(sh<max)
{
if(search[idx]==k)//如果哈希数与搜索值相同
{
lout->Caption="找到关键值( "+IntToStr(k)+" )在第 "
+ IntToStr(idx)+ " 个位置"; //结果表示
sout->Caption="造成了 "+IntToStr(c1)+" 次冲突";
t1->Enabled=true;
return;
}
else
{
idx=clsn(idx); //调用冲突解决函数
sh++; //搜索次数加1
c1++; //冲突加1
}
}
lout->Caption="哈希表中没有找到你要的关键值";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::t1Timer(TObject *Sender) //冲突时的动画显示
{
if(t<68)
{
sout->Caption=" "+sout->Caption; //增加空格符使文字呈现动画效果
t++; //执行次数累计
}
else
{
sout->Caption="";
t1->Enabled=false; //停止timer功能
}
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -