📄 cd6_3u.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "cd6_3u.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int sort[9]={0};
void qsort(int*,int,int); //定义快速排序函数
int i,j,idx=0;
int c1=2,z,c2;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton5Click(TObject *Sender)
{
tbin->Visible=true; //控件的Visible属性是true表示显示
tbin->Enabled=true; //控件的Enabled属性是true表示可用
}
//---------------------------------------------------------------------------
void __fastcall TForm1::tbcClick(TObject *Sender) //清除,重新输入
{
for(i=0;i<10;i++) //清除窗体及数组中的排序数据
{
sort[i]=0;
for(j=1;j<10;j++)
sg->Cells[i][j]="";
}
sg->Cells[0][1]=" 内容项";
sbs->Enabled=false; //是开始程序、步骤一、步骤二不可用
sb1->Enabled=false;
sb2->Enabled=false;
c1=2;
idx=0;
z=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::tboClick(TObject *Sender) //登出用户输入的内容
{
sg->Visible=true;
tbo->Enabled=false; //登出功能不可用
sbs->Enabled=true; //开始排序功能可用
for(i=0;i<idx;i++) //列出用户输入的内容
{
sg->Cells[i+1][0]=" 第 "+IntToStr(i+1)+" 笔";
sg->Cells[i+1][1]=sort[i];
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::tbdClick(TObject *Sender) //重新输入
{
ein->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
sg->Cells[0][0]=" 输入项";
sg->Cells[0][1]=" 内容项";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::einKeyUp(TObject *Sender, WORD &Key,
TShiftState Shift) //输入事件
{
if(Key==13)
{
if(idx<9) //限定排序数量
{
sort[idx]=StrToInt(ein->Text); //获取数据
idx++; //计数器加一
ein->Text="";
tbc->Enabled=true;
tbo->Enabled=true;
}
}
tbd->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sbsClick(TObject *Sender) //排序功能
{
qsort(sort,0,idx-1); //调用快速排序函数,传入sort数组,左值:0,右值:排序数-1
sb1->Enabled=true;
sbs->Enabled=false;
}
//---------------------------------------------------------------------------
void qsort(int *sort,int left,int right) //注意:sort是整数数组
{
int temp;
if(left<right)
{
int l=left,r=right+1,P=sort[left];
do{
do{l++;}while(P>sort[l]); //当标的值大于右方值时继续往右
do{r--;}while(P<sort[r]); //当标的值小于左方值时继续往左
if(l<r) //当左索引位置小于右索引时交换其索引的数组内容
{
temp=sort[l];
sort[l]=sort[r];
sort[r]=temp;
for(i=0;i<idx;i++)
Form1->sg->Cells[i+1][c1]=sort[i]; //显示排序动作过程
c1++;
}
}while(l<r);
temp=sort[left]; //交换sort[left]及sort[r]之内容值
sort[left]=sort[r];
sort[r]=temp;
if(z==0)
{
for(i=2;i<c1;i++)
Form1->sg->Cells[0][i]="动作"+IntToStr(i-1);
for(i=0;i<idx;i++)
Form1->sg->Cells[i+1][c1]=sort[i];
Form1->sg->Cells[0][c1]="定位("+IntToStr(temp)+")";
z=1;
c2=c1; //第一阶段显示结束,第二阶段开始
}
else
{
c2++;
for(i=0;i<idx;i++)
Form1->sg->Cells[i+1][c2]=sort[i];
Form1->sg->Cells[0][c2]="循环排序";
}
qsort(sort,left,r-1); //递归调用排序左边的值
qsort(sort,r+1,right); //递归调用排序右边的值
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sb1Click(TObject *Sender) //步骤一
{
sg->RowCount=c1+1; //用步骤来控制显示界面,显示到其标准定位
sg->Visible=true;
sb2->Enabled=true;
sb1->Enabled=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::sb2Click(TObject *Sender) //步骤二
{
sg->RowCount=c2+1;
sb2->Enabled=false;
Form1->sg->Cells[0][c2]="排序结果";
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -