⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blocksearch.cpp

📁 关于书籍《Borland c++Builder工程实践》的源代码
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "BlockSearch.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TBloForm *BloForm;
//---------------------------------------------------------------------------
__fastcall TBloForm::TBloForm(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TBloForm::FormCreate(TObject *Sender)
{
    Randomize();
    Destination = rand()%50+1;        //目标值最大为50
    value->Text = IntToStr(Destination);
    DisplayInterval = 400;
    BlockMax[0] = 17;
    BlockMax[1] = 34;
    BlockMax[2] = 50;
    DataGrid->Cells[0][0] = "元素编号";
    DataGrid->Cells[1][0] = " 元素值";
    for(int i = 1;i < 16;i ++)
        DataGrid->Cells[0][i] = IntToStr(i);
    DataGeneration();
}
//---------------------------------------------------------------------------


void __fastcall TBloForm::DataGeneration()
{
    randomize();
    Clean(120,25,280,380);
    int i,temp;
    for(int j = 1;j <= 3;j ++)
    {
        for(i = 1;i <= 5;i ++)
        {
            switch(j)
            {
                case 1:
                    do
                    {
                        temp =  rand()%50+1;    //最大值为50
                    }while(temp > 17);
                    Array[(j-1)*5+i-1] = temp;
                    break;
                case 2:
                    do
                    {
                        temp = rand()%50+1;
                    }while((temp < 18) || (temp > 34));
                    Array[(j-1)*5+i-1] = temp;
                    break;
                case 3:
                    do
                    {
                        temp = rand()%50+1;
                    }while((temp < 35) || (temp > 50));
                    Array[(j-1)*5+i-1] = temp;
                    break;
                default:
                    break;
            }
        }
    }
    for(int m = 0;m < 15;m ++)
    {
        DataGrid->Cells[1][m+1] = IntToStr(Array[m]);
    }
    DataGrid->Refresh();
}
void __fastcall TBloForm::ExitClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

void __fastcall TBloForm::DisplayClick(TObject *Sender)
{
    int k,index,low,high;
    bool flag;
    Clean(120,25,280,380);
    flag = false;
    Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
    pBitmap->Transparent = true;
    pBitmap->TransparentMode = tmAuto;
    pBitmap->LoadFromFile("hand1.bmp");
    for(int j = 1;j <= 3;j ++)          //顺序查找块
    {
        if(Destination <= BlockMax[j-1])
        {
            low = 5*(j-1);
            high = 5*(j-1) + 4;
            Canvas->Draw(120,low*21+30,pBitmap);
            Canvas->TextOutA(160,low*21+32,"目标值位于第"+IntToStr(j)+"块");
            Sleep(5*DisplayInterval);
            Clean(120,24+low*21,280,24+(low+1)*21);
            break;
        }
    }
    for(int i = 1;i <= 5;i ++)         //块内顺序查找目标值
    {
        index = low + i - 1;
        if(Array[index] != Destination)
        {
            if(index != high)
            {
                Canvas->Draw(120,index*21+30,pBitmap);
                Sleep(2*DisplayInterval);
                Canvas->TextOutA(170,index*21+32,"与目标值不等");
                Sleep(2*DisplayInterval);
                Clean(120,24+index*21,280,24+(index+1)*21);
            }
            else if(flag)
            {
                Canvas->Draw(120,index*21+30,pBitmap);
                Sleep(2*DisplayInterval);
                Canvas->TextOutA(170,index*21+32,"搜索完毕!");
                Sleep(2*DisplayInterval);
                Clean(120,24+index*21,280,24+(index+1)*21);
            }
            else
            {
                Canvas->Draw(120,index*21+30,pBitmap);
                Sleep(2*DisplayInterval);
                Canvas->TextOutA(170,index*21+32,"没有目标值!");
                Sleep(5*DisplayInterval);
                Clean(120,24+index*21,280,24+(index+1)*21);
            }
        }
        else
        {
            flag = true;
            for(k = 0;k < 4;k ++)           //闪烁效果
            {
                Canvas->Draw(120,index*21+30,pBitmap);
                Sleep(DisplayInterval);
                Clean(120,24+index*21,280,24+(index+1)*21);
                Sleep(DisplayInterval);
            }
            Canvas->Draw(120,index*21+28,pBitmap);
            Canvas->TextOutA(170,index*21+32,"找到目标值!");
            Sleep(5*DisplayInterval);
            Clean(120,24+index*21,280,24+(index+1)*21);
            pBitmap->LoadFromFile("ok1.bmp");
            Canvas->Draw(120,index*21+30,pBitmap);
            Sleep(2*DisplayInterval);
            pBitmap->LoadFromFile("hand1.bmp");
        }
    }
    delete pBitmap;
}
//---------------------------------------------------------------------------


void __fastcall TBloForm::Clean(int x1, int y1, int x2, int y2)
{
    Canvas->Pen->Mode = pmCopy;
    Canvas->Brush->Color = clBtnFace;
    Canvas->Pen->Color = clBtnFace;
    Canvas->Rectangle(x1,y1,x2,y2);    //覆盖画图区域
}
void __fastcall TBloForm::DataGenerateClick(TObject *Sender)
{
    DataGeneration();
    Randomize();
    Destination = rand()%50+1;        //目标值最大为50
    value->Text = IntToStr(Destination);
}
//---------------------------------------------------------------------------

void __fastcall TBloForm::TrackBar1Change(TObject *Sender)
{
    DisplayInterval = 1100-TrackBar1->Position;
}
//---------------------------------------------------------------------------

void TBloForm::Run()
{
    int k,index,low,high;
    bool flag;
    flag = false;
    Graphics::TBitmap *pBitmap = new Graphics::TBitmap();
    pBitmap->Transparent = true;
    pBitmap->TransparentMode = tmAuto;
    pBitmap->LoadFromFile("hand1.bmp");
    for(int j = 1;j <= 3;j ++)          //顺序查找块
    {
        if(Destination <= BlockMax[j-1])
        {
            low = 5*(j-1);
            high = 5*(j-1) + 4;
            Canvas->Draw(120,low*21+30,pBitmap);
            Canvas->TextOutA(160,low*21+32,"目标值位于第"+IntToStr(j)+"块");
            Sleep(5*DisplayInterval);
            Clean(120,24+low*21,280,24+(low+1)*21);
            break;
        }
    }
    for(int i = 1;i <= 5;i ++)         //块内顺序查找目标值
    {
        index = low + i - 1;
        if(Array[index] != Destination)
        {
            if(index != high)
            {
                Canvas->Draw(120,index*21+30,pBitmap);
                Sleep(2*DisplayInterval);
                Canvas->TextOutA(170,index*21+32,"与目标值不等");
                Sleep(2*DisplayInterval);
                Clean(120,24+index*21,280,24+(index+1)*21);
            }
            else if(flag)
            {
                Canvas->Draw(120,index*21+30,pBitmap);
                Sleep(2*DisplayInterval);
                Canvas->TextOutA(170,index*21+32,"搜索完毕!");
                Sleep(2*DisplayInterval);
                Clean(120,24+index*21,280,24+(index+1)*21);
            }
            else
            {
                Canvas->Draw(120,index*21+30,pBitmap);
                Sleep(2*DisplayInterval);
                Canvas->TextOutA(170,index*21+32,"没有目标值!");
                Sleep(5*DisplayInterval);
                Clean(120,24+index*21,280,24+(index+1)*21);
            }
        }
        else
        {
            flag = true;
            for(k = 0;k < 4;k ++)           //闪烁效果
            {
                Canvas->Draw(120,index*21+30,pBitmap);
                Sleep(DisplayInterval);
                Clean(120,24+index*21,280,24+(index+1)*21);
                Sleep(DisplayInterval);
            }
            Canvas->Draw(120,index*21+28,pBitmap);
            Canvas->TextOutA(170,index*21+32,"找到目标值!");
            Sleep(5*DisplayInterval);
            Clean(120,24+index*21,280,24+(index+1)*21);
            pBitmap->LoadFromFile("ok1.bmp");
            Canvas->Draw(120,index*21+30,pBitmap);
            Sleep(2*DisplayInterval);
            pBitmap->LoadFromFile("hand1.bmp");
        }
    }
    delete pBitmap;
}



void TBloForm::Transport(int ii)
{
    Destination = ii;                      //目标值最大为50
    value->Text = IntToStr(Destination);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -