__published: // IDE-managed Components
TButton *Button1;
TImage *Image1;
TImage *Image2;
TImage *Image3;
TShape *Shape1;
TShape *Shape2;
TShape *Shape3;
TImage *Image4;
TShape *Shape4;
TImage *Image5;
TImage *Image6;
TImage *Image7;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TLabel *Label4;
TButton *Button2;
TLabel *Label5;
TLabel *Label6;
TLabel *Label7;
TLabel *Label8;
TLabel *Label9;
TLabel *Label10;
TLabel *Label11;
TLabel *Label12;
TLabel *Label14;
TLabel *Label15;
TButton *Button3;
TLabel *Label13;
TLabel *Label16;
TLabel *Label17;
void __fastcall Button2Click(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
bool stop;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Листинг файла MainUnit.cpp:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "MainUnit.h"
#include "GrafikUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE hThread1;
HANDLE hThread2;
TRTLCriticalSection sect;
double G1; //Îáùàÿ âûðàáîòêà íà äàííûé ÷àñ
double P; //Îáùåå ïîòðåáëåíèå íà äàííûé ÷àñ
int h; //÷àñû
//---------------------------------------------------------------------------
DWORD WINAPI ThreadFunc1(void *p) //1 ïîòîê - ìîäåëèðóåò ðàáîòó ýëåêòðîñòàíöèè
{
double G[4] = {100, 100, 120, 120}; //ìîùíîñòè ãåíåðàòîðîâ
while(1)
{
if(!Form1->stop)
{
Sleep(1000); //Çàäåðæêà 1 ñ
EnterCriticalSection(§); //Âõîäèì â êðèòè÷åñêóþ ñåêöèþ
G1 = 0;
int i = 1;
double s = 0;
for( ;i <= 4; i++)
{ //îïðåäåëÿåì ñêîëüêî ãåíåðàòîðîâ ïîêðîåò òåêóùèå ïîòåðè
G1 += G[i-1];
if(G1 > P) break;
}
//Âûâîäèì âåëè÷èíó òåêóùåé âûðàáîòêè íà ãðàôèê
AnsiString buf;
buf.sprintf("%3.1lf", G1);
Form1->Label15->Caption = buf;
Form2->Series2->AddXY(h, G1, h, clGreen);
LeaveCriticalSection(§);
//"îòêëþ÷àåì" âñå ãåíåðàòîðû
Form1->Shape1->Brush->Color = clWhite;
Form1->Shape2->Brush->Color = clWhite;
Form1->Shape3->Brush->Color = clWhite;
Form1->Shape4->Brush->Color = clWhite;
switch(i) //âêëþ÷àåì íóæíûå
{
case 1: { Form1->Shape1->Brush->Color = clGreen; } break;
case 2: { Form1->Shape1->Brush->Color = clGreen;
Form1->Shape2->Brush->Color = clGreen;
} break;
case 3: { Form1->Shape1->Brush->Color = clGreen;
Form1->Shape2->Brush->Color = clGreen;
Form1->Shape3->Brush->Color = clGreen;
} break;
case 4: { Form1->Shape1->Brush->Color = clGreen;
Form1->Shape2->Brush->Color = clGreen;
Form1->Shape3->Brush->Color = clGreen;
Form1->Shape4->Brush->Color = clGreen;
} break;
};
}
}
}
//---------------------------------------------------------------------------
DWORD WINAPI ThreadFunc2(void *p) //2-îé ïîòîê ìîäåëèðóåò ïîòðåáëåíèå ýíåðãèè
{
while(1)
{
if(!Form1->stop)
{
Form1->Label8->Caption = h;
AnsiString buf;
Sleep(1000);
//ðàñ÷¸ò ïîòðåáëåíèÿ
double P1 = 100 + 20 * sin((M_PI/6) * (h/6 - 1));
buf.sprintf("%3.1lf", P1);
Form1->Label5->Caption = buf;
double P2 = 180 + 50 * sin((M_PI/2) * (h/6 - 1));
buf.sprintf("%3.1lf", P2);
Form1->Label6->Caption = buf;
double P3 = 90 + 20 * sin((M_PI/2) * (h/6 - 1));
buf.sprintf("%3.1lf", P3);
Form1->Label7->Caption = buf;
EnterCriticalSection(§); //âõîäèì â êðèòè÷åñêóþ ñåêöèþ
P = P1 + P2 + P3;
buf.sprintf("%3.1lf", P);
Form1->Label11->Caption = buf;
Form2->Series1->AddXY(h, P, h, clRed);
LeaveCriticalSection(§);
h++; //ïåðåõîä íà ñëåäóþùèé ÷àñ
if(h == 24)
{
h = 0;//"îáíóëÿåì" ÷àñû ïðè íàñòóïëåíèè íîâûõ ñóòîê
Form2->Series1->Clear();
Form2->Series2->Clear();
}
}
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//ñíèìàåì ñ ïàóçû ïîòîêè
if(stop)
{
stop = false;
Button2->Caption = "Ñòîï";
Form2->Button2->Caption = "Ñòîï";
}
else
{
stop = true;
Button2->Caption = "Ïóñê";
Form2->Button2->Caption = "Ïóñê";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
P = 0.0;
G1 = 0.0;
h = 0;
stop = true;
InitializeCriticalSection(§); //èíèöèàëèçèðóåì êðèòè÷åñêóþ ñåêöèþ
hThread1 = CreateThread(0,0,ThreadFunc1,0,0,NULL);
hThread2 = CreateThread(0,0,ThreadFunc2,0,0,NULL);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if(hThread1 != NULL)
TerminateThread(hThread1,0); //Çàâåðøàåì 1-îé ïîòîê
if(hThread2 != NULL)
TerminateThread(hThread1,0); //Çàâåðøàåì 2-îé ïîòîê
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Form2->Show();
}
//---------------------------------------------------------------------------
Список литературы:
1. Программирование в C++ Builder. А.Я. Архангельский, Бином, Москва, 2003.- 1151 с.
2. Конспект лекций по курсу Операционные системы лектор Родников В.В. 2010г.
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.