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

📄 ch7_2u.cpp

📁 C++ Builder程序员学习数据结构第7章
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "ch7_2u.h"
#include "cd6_3u.h"              //将范例6-3包括进来
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
Tmain *main;           //二分搜索的窗体名称
extern int sort[9];//定义为外部变量(extern)即可通用于项目之中
extern int idx;
int k,k1=0,s,c;
int i;
//---------------------------------------------------------------------------
__fastcall Tmain::Tmain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall Tmain::N1Click(TObject *Sender)//输入并排序数据
{
  Form1->Show();     //显示快速排序法,Form1为其窗体名称
  Form1->tbc->Click();   //调用清除按钮事件
  k1=0;               //将变量还原为初始值
  s=1;
}
//---------------------------------------------------------------------------
void __fastcall Tmain::N2Click(TObject *Sender)//打开输入搜索值输入框
{
   if(s==1)
   {
        ctb->Visible=true;
        sg->Visible=true;
   }
   else
        ShowMessage("请先输入排序数据~");
}
//---------------------------------------------------------------------------
void __fastcall Tmain::einKeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)             //输入搜索值
{
  if(Key==13)
  {
        k=StrToInt(ein->Text);     //获取搜索值
        k1=1;
        ein->Text="";

        for(i=0;i<10;i++)
          for(int j=0;j<8;j++)
            sg->Cells[i][j]="";


       for(i=1;i<=idx;i++)                 //列出新的已排序数据
       {
          sg->Cells[i][0]="第 "+IntToStr(i)+ " 个值";
          sg->Cells[i][1]=IntToStr(sort[i-1]);
       }
       sg->Cells[0][0]="排序值位置";
       sg->Cells[0][1]="排序内容值";
  }
}
//---------------------------------------------------------------------------
int bsh(int k)                             //二分搜索函数
{
  int left=0,right=idx-1,cpr;              //边界与指针定义
  c=1;

  while(left<=right)
  {
    cpr=(left+right)/2;            //比较位置运算
    if (k<sort[cpr])               //搜索值比数组索引值小
    {
        for(i=left;i<right;i++)          //列出左半部范围
          main->sg->Cells[i+1][c+1]=sort[i];
        right=cpr-1;                   //范围缩小一半
    }
    else if(k>sort[cpr])                  //搜索值比数组索引值大
    {
        left=cpr+1;                        //范围缩小一半
        for(i=left;i<=right;i++)           //列出右半部范围
          main->sg->Cells[i+1][c+1]=sort[i];
    }
    else if (k=sort[cpr])                    //搜索值等于数组索引值
    {
        main->sg->Cells[cpr+1][c+1]=sort[cpr];    //表现搜索结果
        main->sg->Cells[0][c+1]="第 "+IntToStr(c)+" 次范围";
        ShowMessage("搜索到数据("+IntToStr(sort[cpr])+" )在第 "+IntToStr(c)+" 次搜索");
        return 1;                                   //找到搜索值返回1
    }
  main->sg->Cells[0][c+1]="第 "+IntToStr(c)+" 次范围";
  c++;
  }
  return 0;                  //没找到返回0
}
void __fastcall Tmain::N3Click(TObject *Sender)    	//搜索结果
{
  sg->ColCount=idx+1;
  if(k1==1)                   //搜索值输入验证
  {
    if(bsh(k)==0)             //调用二分搜索法
      ShowMessage("没有找到与搜索值相同的数据");
    else
      ctb->Visible=false;
  }
  else
     ShowMessage("请先输入搜索值");
  sg->RowCount=c+2;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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