.


:




:

































 

 

 

 


 

Unit1.h

 

#ifndef Unit1H

#define Unit1H

//--------------------------------------

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

#include <ExtCtrls.hpp>

#include <Buttons.hpp>

#include <AppEvnts.hpp>

//--------------------------------------

class TForm1: public TForm

{

__published: // IDE-managed Components

TComboBox *ComboBox1;

TComboBox *ComboBox2;

TComboBox *ComboBox3;

TComboBox *ComboBox4;

TComboBox *ComboBox5;

TLabel *Label1;

TLabel *Label2;

TLabel *Label3;

TLabel *Label4;

TLabel *Label5;

TLabel *Label6;

TLabel *Label7;

TRadioGroup *RadioGroup1;

TRadioGroup *RadioGroup2;

TRadioGroup *RadioGroup3;

TRadioGroup *RadioGroup4;

TRadioGroup *RadioGroup5;

TButton *Button1;

TButton *Button2;

TButton *Button3;

TButton *Button4;

TButton *Button5;

TImage *Image1;

TImage *Image2;

TCheckBox *CheckBox1;

TCheckBox *CheckBox2;

TCheckBox *CheckBox3;

TCheckBox *CheckBox4;

TCheckBox *CheckBox5;

TCheckBox *CheckBox6;

TCheckBox *CheckBox7;

TCheckBox *CheckBox8;

TEdit *Edit1;

TBevel *Bevel1;

void __fastcall FormCreate(TObject *Sender);

 

void __fastcall Button1Click(TObject *Sender);

void __fastcall Button2Click(TObject *Sender);

void __fastcall Button3Click(TObject *Sender);

void __fastcall Button4Click(TObject *Sender);

void __fastcall Button5Click(TObject *Sender);

void __fastcall ComboBox1Change(TObject *Sender);

void __fastcall ComboBox2Change(TObject *Sender);

void __fastcall ComboBox3Change(TObject *Sender);

void __fastcall ComboBox4Change(TObject *Sender);

void __fastcall ComboBox5Change(TObject *Sender);

void __fastcall RadioGroup1Click(TObject *Sender);

void __fastcall RadioGroup2Click(TObject *Sender);

void __fastcall RadioGroup3Click(TObject *Sender);

void __fastcall RadioGroup4Click(TObject *Sender);

void __fastcall RadioGroup5Click(TObject *Sender);

void __fastcall CheckBox1Click(TObject *Sender);

void __fastcall CheckBox2Click(TObject *Sender);

void __fastcall CheckBox3Click(TObject *Sender);

void __fastcall CheckBox4Click(TObject *Sender);

void __fastcall CheckBox5Click(TObject *Sender);

 

private: // User declarations

public: // User declarations

__fastcall TForm1(TComponent* Owner);

};

//--------------------------------------

extern PACKAGE TForm1 *Form1;

//--------------------------------------

#endif

 

Unit1.cpp

 

#include <vcl.h>

#include <stdlib.h>

#include <math.h>

#include <stdio.h>

#pragma hdrstop

#include "Unit1.h"

//--------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

 

DWORD WINAPI SidorovThread(int pot);

DWORD WINAPI StartThread(void);

void draw1(TColor c,float x[3],float y[3]);

void draw2(int pot, int i,TColor c);

int Random (int nMaxValue);

void ClearBox1(void);

void ClearBox2(void);

void Ris1(float x[3],float y[3],float vx[3],float vy[3],TColor c);

void Ris2(int pot, int i,TColor c);

void WaitAndClose(int i);

void WriteComToFile(char com,int a,int b,int c,int d, int to);

 

HANDLE hThread1[3], hThread2[2], SThread, hFile;

DWORD ID, RealWriteByte = 0, ProcCount = 0;

AnsiString StrCom;

int w, h, a[5], NCP;

int n_pot = 5, n_pot1 = 3, n_pot2 = 2;

TColor clr[5];

bool fl_exit = false, sync = true, fl_wait, fl_write_file;

 

CRITICAL_SECTION CS1, CS2;

//--------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

}

//--------------------------------------

void WaitAndClose(int i)

{

switch(i){

case 0: Form1->Button1->Enabled = false;

Form1->Label6->Caption = " ";

Form1->CheckBox1->Enabled = true;

Form1->CheckBox2->Enabled = true;

Form1->CheckBox3->Enabled = true;

Form1->CheckBox4->Enabled = false;

Form1->CheckBox5->Enabled = false;

if(fl_wait)WaitForMultipleObjects(n_pot1,hThread1,TRUE,INFINITE);

Form1->CheckBox4->Enabled = true;

Form1->CheckBox5->Enabled = true;

Form1->CheckBox1->Enabled = false;

Form1->CheckBox2->Enabled = false;

Form1->CheckBox3->Enabled = false;

break;

case 1: Form1->Label6->Caption =" ";

if(fl_wait)WaitForMultipleObjects(n_pot2,hThread2,TRUE,INFINITE);

Form1->Label6->Caption = " ";

Form1->CheckBox4->Enabled = false;

Form1->CheckBox5->Enabled = false;

break;

}

}

//--------------------------------------

DWORD WINAPI StartThread(void)

{

int i;

 

NCP = StrToInt(Form1->Edit1->Text); //

fl_write_file = Form1->CheckBox8->Checked; //

if(fl_write_file)

hFile = CreateFile("test.txt",GENERIC_WRITE,FILE_SHARE_WRITE,

NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

ClearBox1(); ClearBox2(); //

fl_wait = Form1->CheckBox6->Checked; // true =

//

// ,

// , ,

// .

// , - .

// , // , .

 

// 32

for(i=0;i<n_pot1;i++){ // 1-

hThread1[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)

SidorovThread,(void *) i, 0, &ID);

}

 

WaitAndClose(0); //

ClearBox1(); ClearBox2();

for(i=0;i<n_pot2;i++){ //

hThread2[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)

SidorovThread,(void *)(3+i),0,&ID);

}

WaitAndClose(1); //

ClearBox1(); ClearBox2();

Form1->Button1->Enabled = true;

}

//--------------------------------------

DWORD WINAPI SidorovThread(int pot)

{

int i,a = 5;

float x[3], y[3], vx[3], vy[3];

 

fl_exit = false; //

for(i=0;i<3;i++){ // ,

x[i] = Random(w); y[i] = Random(h);

vx[i] = 0.001+0.05*Random(10); vy[i] = 0.001+0.05*Random(10);

Sleep(5);

}

 

for(i=0;i<NCP;i++){ //

if(fl_exit) ExitThread(0);

sync = Form1->CheckBox7->Checked;

Ris1(x,y,vx,vy,clr[pot]); //

Ris2(pot,(w*i)/NCP,clr[pot]); //

}

}

//--------------------------------------

void Ris1(float x[3],float y[3],float vx[3],float vy[3],TColor c)

{

if(sync){

EnterCriticalSection(&CS1);

draw1(clSilver,x,y);

LeaveCriticalSection(&CS1);

}

else draw1(clSilver,x,y);

 

for(int i=0;i<3;i++){

x[i]+=vx[i]; y[i]+=vy[i];

if((x[i]>w) || (x[i]<0)){

vx[i]=-vx[i]; x[i]+=vx[i];

}

if((y[i]>h) || (y[i]<0)){

vy[i]=-vy[i]; y[i]+=vy[i];

}

}

 

if(sync){

EnterCriticalSection(&CS1);

draw1(c,x,y);

LeaveCriticalSection(&CS1);

}

else draw1(c,x,y);

Sleep(0);

}

//--------------------------------------

void Ris2(int pot, int i,TColor c)

{

if(sync){

EnterCriticalSection(&CS2);

draw2(pot,i,c);

LeaveCriticalSection(&CS2);

}

else draw2(pot,i,c);

Sleep(0);

}

//--------------------------------------

void draw2(int pot, int i,TColor c)

{

if(!fl_write_file){ //

Form1->Image2->Canvas->Pen->Color = c;

Form1->Image2->Canvas->MoveTo(i,10+pot*5);

Form1->Image2->Canvas->LineTo(i,15+pot*5);

Form1->Image2->Refresh();

}

else{ //

WriteComToFile('c',c,0,0,0,2); //

WriteComToFile('m',i,10+pot*5,0,0,2);

WriteComToFile('l',i,15+pot*5,0,0,2);

WriteComToFile('f',0,0,0,0,2); //

}

}

//--------------------------------------

void draw1(TColor c, float x[3], float y[3])

{

if(!fl_write_file){ //

Form1->Image1->Canvas->Pen->Color = c;

Form1->Image1->Canvas->Brush->Color = c;

Form1->Image1->Canvas->MoveTo(x[2],y[2]);

for(int i=0;i<3;i++){

Form1->Image1->Canvas->LineTo(x[i],y[i]);

Form1->Image1->Canvas->Rectangle(x[i],y[i],x[i]+5,y[i]+5);

}

Form1->Image1->Refresh();

}

else{ //

WriteComToFile('c',c,0,0,0,1);

WriteComToFile('m',x[2],y[2],0,0,1);

for(int i=0;i<3;i++){

WriteComToFile('l',x[i],y[i],0,0,1);

WriteComToFile('r',x[i],y[i],x[i]+5,y[i]+5,1);

}

WriteComToFile('f',0,0,0,0,1);

}

}

//--------------------------------------

void WriteComToFile(char com,int a,int b,int c,int d,int no)

{

//

// : :1,2, 3, 4,

// ;/n

// : - , l - LineTo, m - MoveTo,

// r - rectangle, f - Refresh, s - Sleep

CHAR ToFile[64];

sprintf(ToFile,"%c: %d, %d, %d, %d %d\n",com,a,b,c,d,no);

WriteFile(hFile,ToFile,strlen(ToFile),&RealWriteByte,NULL);

}

//--------------------------------------

void __fastcall TForm1::Button5Click(TObject *Sender)

{

DWORD nb1;

int a,b,c,d,ob;

char com;

TCanvas *Cnv;

CHAR FromFile[64];

 

CloseHandle(hFile);

 

hFile = CreateFile("test.txt",GENERIC_READ,0,NULL,

OPEN_EXISTING,0,NULL);

StrCom = "";

do{

ReadFile(hFile,FromFile,sizeof(FromFile),&nb1,NULL);

for(int i=0;i<nb1;i++){

if(FromFile[i]!='\n') StrCom += FromFile[i];

else{ // -

sscanf(StrCom.c_str(),"%c: %d, %d, %d, %d %d\n",

&com,&a,&b,&c,&d,&ob);

StrCom = "";

if(ob == 1) Cnv = Form1->Image1->Canvas;

else Cnv = Form1->Image2->Canvas;

switch(com){

case 'c': Cnv->Pen->Color = a; break;

case 'l': Cnv->LineTo(a,b); break;

case 'm': Cnv->MoveTo(a,b); break;

case 'r': Cnv->Rectangle(a,b,c,d); break;

case 'f': Form1->Image1->Refresh();

Form1->Image2->Refresh(); break;

case 's': Sleep(a); break;

}

}

}

}while(nb1!=0);

 

CloseHandle(hFile);

}

//--------------------------------------

void ClearBox1(void)

{

Form1->Image1->Canvas->Brush->Color = clSilver;

Form1->Image1->Canvas->Rectangle(0,0,w,h);

}

//--------------------------------------

void ClearBox2(void)

{

Form1->Image2->Canvas->Brush->Color = clSilver;

Form1->Image2->Canvas->Rectangle(0,0,w,h);

}

//--------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

{

sync = CheckBox7->Checked;

randomize();

 

a[0] = THREAD_PRIORITY_LOWEST; clr[0] = clRed;

a[1] = THREAD_PRIORITY_BELOW_NORMAL; clr[1] = clBlue;

a[2] = THREAD_PRIORITY_NORMAL; clr[2] = clGreen;

a[3] = THREAD_PRIORITY_ABOVE_NORMAL; clr[3] = clAqua;

a[4] = THREAD_PRIORITY_HIGHEST; clr[4] = clYellow;

for(int i=0;i<5;i++){

ComboBox1->Items->Add(a[i]);

ComboBox2->Items->Add(a[i]);

ComboBox3->Items->Add(a[i]);

ComboBox4->Items->Add(a[i]);

ComboBox5->Items->Add(a[i]);

}

ComboBox1->ItemIndex = 2;

ComboBox2->ItemIndex = 2;

ComboBox3->ItemIndex = 2;

ComboBox4->ItemIndex = 2;

ComboBox5->ItemIndex = 2;

SYSTEM_INFO SI;

GetSystemInfo(&SI);

ProcCount = SI.dwNumberOfProcessors;

Caption = Caption + " : "

+IntToStr(ProcCount)+")";

w = Form1->Image1->Width; h = Form1->Image1->Height;

 

InitializeCriticalSection(&CS1); // .

InitializeCriticalSection(&CS2);

}

//--------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)

{

fl_exit = true;

Sleep(100);

DeleteCriticalSection(&CS1); //

DeleteCriticalSection(&CS2);

CloseHandle(SThread); CloseHandle(hFile);

Close();

}

//--------------------------------------

int Random (int nMaxValue)

{

// random :

// .

// Randomize .

return(abs(nMaxValue*cos(GetCurrentThreadId()*GetTickCount())));

}

//--------------------------------------

void __fastcall TForm1::ComboBox1Change(TObject *Sender)

{

SetThreadPriority(hThread1[0],a[ComboBox1->ItemIndex]);

}

//--------------------------------------

void __fastcall TForm1::ComboBox2Change(TObject *Sender)

{

SetThreadPriority(hThread1[1],a[ComboBox2->ItemIndex]);

}

//--------------------------------------

void __fastcall TForm1::ComboBox3Change(TObject *Sender)

{

SetThreadPriority(hThread1[2],a[ComboBox3->ItemIndex]);

}

//--------------------------------------

void __fastcall TForm1::ComboBox4Change(TObject *Sender)

{

SetThreadPriority(hThread2[0],a[ComboBox2->ItemIndex]);

}

//--------------------------------------

void __fastcall TForm1::ComboBox5Change(TObject *Sender)

{

SetThreadPriority(hThread2[1],a[ComboBox2->ItemIndex]);

}

//--------------------------------------

void __fastcall TForm1::RadioGroup1Click(TObject *Sender)

{

SetThreadIdealProcessor(hThread1[0],RadioGroup1->ItemIndex+1);

SetThreadAffinityMask(hThread1[0],RadioGroup1->ItemIndex+1);

}

//--------------------------------------

void __fastcall TForm1::RadioGroup2Click(TObject *Sender)

{

SetThreadIdealProcessor(hThread1[1],RadioGroup2->ItemIndex+1);

SetThreadAffinityMask(hThread1[1],RadioGroup2->ItemIndex+1);

}

//--------------------------------------

void __fastcall TForm1::RadioGroup3Click(TObject *Sender)

{

SetThreadIdealProcessor(hThread1[2],RadioGroup3->ItemIndex+1);

SetThreadAffinityMask(hThread1[2],RadioGroup3->ItemIndex+1);

}

//--------------------------------------

void __fastcall TForm1::RadioGroup4Click(TObject *Sender)

{

SetThreadIdealProcessor(hThread2[0],RadioGroup3->ItemIndex+1);

SetThreadAffinityMask(hThread2[0],RadioGroup3->ItemIndex+1);

}

//--------------------------------------

void __fastcall TForm1::RadioGroup5Click(TObject *Sender)

{

SetThreadIdealProcessor(hThread2[1],RadioGroup3->ItemIndex+1);

SetThreadAffinityMask(hThread2[1],RadioGroup3->ItemIndex+1);

}

//--------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)

{

fl_exit = true;

}

//--------------------------------------

void __fastcall TForm1::CheckBox1Click(TObject *Sender)

{

EnterCriticalSection(&CS1); // ,

EnterCriticalSection(&CS2); //

// .

if(CheckBox1->Checked) SuspendThread(hThread1[0]);

else ResumeThread(hThread1[0]);

ClearBox1();

LeaveCriticalSection(&CS2);

LeaveCriticalSection(&CS1);

}

//--------------------------------------

void __fastcall TForm1::CheckBox2Click(TObject *Sender)

{

EnterCriticalSection(&CS1);

EnterCriticalSection(&CS2);

if(CheckBox2->Checked) SuspendThread(hThread1[1]);

else ResumeThread(hThread1[1]);

ClearBox1();

LeaveCriticalSection(&CS2);

LeaveCriticalSection(&CS1);

}

//--------------------------------------

void __fastcall TForm1::CheckBox3Click(TObject *Sender)

{

EnterCriticalSection(&CS1);

EnterCriticalSection(&CS2);

if(CheckBox3->Checked) SuspendThread(hThread1[2]);

else ResumeThread(hThread1[2]);

ClearBox1();

LeaveCriticalSection(&CS2);

LeaveCriticalSection(&CS1);

}

//--------------------------------------

void __fastcall TForm1::CheckBox4Click(TObject *Sender)

{

EnterCriticalSection(&CS1);

EnterCriticalSection(&CS2);

if(CheckBox4->Checked) SuspendThread(hThread2[0]);

else ResumeThread(hThread2[0]);

ClearBox1();

LeaveCriticalSection(&CS2);

LeaveCriticalSection(&CS1);

}

//--------------------------------------

void __fastcall TForm1::CheckBox5Click(TObject *Sender)

{

EnterCriticalSection(&CS1);

EnterCriticalSection(&CS2);

if(CheckBox5->Checked) SuspendThread(hThread2[1]);

else ResumeThread(hThread2[1]);

ClearBox1();

LeaveCriticalSection(&CS2);

LeaveCriticalSection(&CS1);

}

//--------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)

{

StartThread(); //

}

//--------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

// .

// .

SThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)

StartThread,NULL,0,&ID);

}

//--------------------------------------

 

1. . .

2. , , .

3. : , , .

4. . .

5. : , .

6. .

7. .

8. .

9. .

 

 

.

  2
1. 3. 2
2. 3 2
3. ... 6
4. ... 7
4.1. ... 7
4.2 . 8
4.3 .. 17
  Unit1.h .. 17
  Unit1.cpp .. 18
  .. 27

 

 



<== | ==>
|
:


: 2018-10-15; !; : 215 |


:

:

, .
==> ...

1549 - | 1341 -


© 2015-2024 lektsii.org - -

: 0.199 .