home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / borland / cb / setup / cbuilder / data.z / THSORT.CPP < prev    next >
C/C++ Source or Header  |  1997-02-28  |  4KB  |  108 lines

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