thsort.cpp
上传用户:lulishicai
上传日期:2010-03-01
资源大小:13202k
文件大小:3k
源码类别:

Delphi/CppBuilder

开发平台:

C++ Builder

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1998-2002 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8. #include <stdlib.h>
  9. #include "thsort.h"
  10. #include "sortthd.h"
  11. //---------------------------------------------------------------------
  12. #pragma resource "*.dfm"
  13. TThreadSortForm *ThreadSortForm;
  14. //---------------------------------------------------------------------
  15. Boolean ArraysRandom;
  16. TSortArray BubbleSortArray, SelectionSortArray, QuickSortArray;
  17. //---------------------------------------------------------------------
  18. __fastcall TThreadSortForm::TThreadSortForm(TComponent *Owner)
  19.   : TForm(Owner)
  20. {
  21. }
  22. //---------------------------------------------------------------------
  23. void __fastcall TThreadSortForm::PaintArray(TPaintBox *Box, int const *A, 
  24.   int const ASize)
  25. {
  26.   int i;
  27.   TCanvas *canvas;
  28.   canvas = Box->Canvas;
  29.   canvas->Pen->Color = clRed;
  30.   for (i=0; i <= ASize; i++)
  31.     PaintLine(canvas, i, A[i]);
  32. }
  33. //---------------------------------------------------------------------
  34. void __fastcall TThreadSortForm::BubbleSortBoxPaint(TObject * /*Sender*/)
  35. {
  36.   PaintArray(BubbleSortBox, EXISTINGARRAY(BubbleSortArray));
  37. }
  38. //---------------------------------------------------------------------
  39. void __fastcall TThreadSortForm::SelectionSortBoxPaint(TObject * /*Sender*/)
  40. {
  41.   PaintArray(SelectionSortBox, EXISTINGARRAY(SelectionSortArray));
  42. }
  43. //---------------------------------------------------------------------
  44. void __fastcall TThreadSortForm::QuickSortBoxPaint(TObject * /*Sender*/)
  45. {
  46.   PaintArray(QuickSortBox, EXISTINGARRAY(QuickSortArray));
  47. }
  48. //---------------------------------------------------------------------
  49. void __fastcall TThreadSortForm::FormCreate(TObject * /*Sender*/)
  50. {
  51.   RandomizeArrays();
  52. }
  53. //---------------------------------------------------------------------
  54. void __fastcall TThreadSortForm::StartBtnClick(TObject * /*Sender*/)
  55. {
  56.   TBubbleSort *bubble;  
  57.   TSelectionSort *selsort;
  58.   TQuickSort *qsort;
  59.   RandomizeArrays();
  60.   ThreadsRunning = 3;
  61.   bubble = new TBubbleSort(BubbleSortBox, EXISTINGARRAY(BubbleSortArray));
  62.   bubble->OnTerminate = ThreadDone;
  63.   selsort = new TSelectionSort(SelectionSortBox,
  64.     EXISTINGARRAY(SelectionSortArray));
  65.   selsort->OnTerminate = ThreadDone;
  66.   qsort = new TQuickSort(QuickSortBox, EXISTINGARRAY(QuickSortArray));
  67.   qsort->OnTerminate = ThreadDone;
  68.   StartBtn->Enabled = False;
  69. }
  70. //---------------------------------------------------------------------
  71. void __fastcall TThreadSortForm::RandomizeArrays()
  72. {
  73.   int i;
  74.   if (! ArraysRandom)
  75.   {
  76.     Randomize();
  77.     for (i=0; i < ARRAYSIZE(BubbleSortArray); i++)
  78.       BubbleSortArray[i] = random(170);
  79.     memcpy(SelectionSortArray, BubbleSortArray, sizeof(SelectionSortArray));
  80.     memcpy(QuickSortArray, BubbleSortArray, sizeof(QuickSortArray));
  81.     ArraysRandom = True;
  82.     Repaint();
  83.   }
  84. }
  85. //---------------------------------------------------------------------
  86. void __fastcall TThreadSortForm::ThreadDone(TObject * /*Sender*/)
  87. {
  88.   ThreadsRunning--;
  89.   if (! ThreadsRunning)
  90.   {
  91.     StartBtn->Enabled = True;
  92.     StartBtn->SetFocus();
  93.     ArraysRandom = False;
  94.   }
  95. }
  96. //---------------------------------------------------------------------