home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / progs / CB / DATA.Z / THSORT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-05  |  3.2 KB  |  104 lines

  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include <stdlib.h>
  6. #include "thsort.h"
  7. #include "sortthd.h"
  8. //---------------------------------------------------------------------
  9. #pragma resource "*.dfm"
  10. TThreadSortForm *ThreadSortForm;
  11. //---------------------------------------------------------------------
  12. Boolean ArraysRandom;
  13. TSortArray BubbleSortArray, SelectionSortArray, QuickSortArray;
  14.  
  15. //---------------------------------------------------------------------
  16. __fastcall TThreadSortForm::TThreadSortForm(TComponent *Owner)
  17.   : TForm(Owner)
  18. {
  19. }
  20. //---------------------------------------------------------------------
  21. void __fastcall TThreadSortForm::PaintArray(TPaintBox *Box, int const *A, 
  22.   int const ASize)
  23. {
  24.   int i;
  25.   TCanvas *canvas;
  26.  
  27.   canvas = Box->Canvas;
  28.   canvas->Pen->Color = clRed;
  29.  
  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.  
  60.   RandomizeArrays();
  61.   ThreadsRunning = 3;
  62.  
  63.   bubble = new TBubbleSort(BubbleSortBox, EXISTINGARRAY(BubbleSortArray));
  64.   bubble->OnTerminate = ThreadDone;
  65.  
  66.   selsort = new TSelectionSort(SelectionSortBox,
  67.     EXISTINGARRAY(SelectionSortArray));
  68.   selsort->OnTerminate = ThreadDone;
  69.  
  70.   qsort = new TQuickSort(QuickSortBox, EXISTINGARRAY(QuickSortArray));
  71.   qsort->OnTerminate = ThreadDone;
  72.  
  73.   StartBtn->Enabled = False;
  74. }
  75. //---------------------------------------------------------------------
  76. void __fastcall TThreadSortForm::RandomizeArrays()
  77. {
  78.   int i;
  79.  
  80.   if (! ArraysRandom)
  81.   {
  82.     Randomize();
  83.     for (i=0; i < ARRAYSIZE(BubbleSortArray); i++)
  84.       BubbleSortArray[i] = random(170);
  85.  
  86.     memcpy(SelectionSortArray, BubbleSortArray, sizeof(SelectionSortArray));
  87.     memcpy(QuickSortArray, BubbleSortArray, sizeof(QuickSortArray));
  88.  
  89.     ArraysRandom = True;
  90.     Repaint();
  91.   }
  92. }
  93. //---------------------------------------------------------------------
  94. void __fastcall TThreadSortForm::ThreadDone(TObject * /*Sender*/)
  95. {
  96.   ThreadsRunning--;
  97.   if (! ThreadsRunning)
  98.   {
  99.     StartBtn->Enabled = True;
  100.     ArraysRandom = False;
  101.   }
  102. }
  103. //---------------------------------------------------------------------
  104.