ziv999

|
分享:
▲
参考看看...
.h档 //---------------------------------------------------------------------------
#ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; TLabel *Label1; TLabel *Label2; TListBox *ListBox1; void __fastcall Button1Click(TObject *Sender); private: // User declarations int a[10]; int flag; public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
.cpp档 //---------------------------------------------------------------------------
#include <vcl.h> #pragma hdrstop
#include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { // 初始化 Label1->Caption = ""; Label2->Caption = ""; ListBox1->Items->Clear(); Button1->Caption = "产生列数"; randomize(); flag = 0; } //---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) { if (flag == 0) { // 随机产生值 Label1->Caption = ""; Label2->Caption = ""; ListBox1->Items->Clear(); for (int i = 0; i < 10; i++) { a = random(50); Label1->Caption = Label1->Caption + a + ","; } Button1->Caption = "插入排列"; } else { Label2->Caption = ""; // insertion_sort for (int i = 0; i < 10 ; i++) { int next = a ; int j; for (j = i - 1 ; j >= 0 && next < a[j] ; j--) { a[j+1] = a[j]; } a[j + 1] = next ;
Label2->Caption = ""; for (int m = 0; m < 10 ; m++) { Label2->Caption = Label2->Caption + a[m] + ","; } ListBox1->Items->Add(Label2->Caption); } Button1->Caption = "产生列数"; } flag = 1 - flag; }
|