Полезное:
Как сделать разговор полезным и приятным
Как сделать объемную звезду своими руками
Как сделать то, что делать не хочется?
Как сделать погремушку
Как сделать так чтобы женщины сами знакомились с вами
Как сделать идею коммерческой
Как сделать хорошую растяжку ног?
Как сделать наш разум здоровым?
Как сделать, чтобы люди обманывали меньше
Вопрос 4. Как сделать так, чтобы вас уважали и ценили?
Как сделать лучше себе и другим людям
Как сделать свидание интересным?
Категории:
АрхитектураАстрономияБиологияГеографияГеологияИнформатикаИскусствоИсторияКулинарияКультураМаркетингМатематикаМедицинаМенеджментОхрана трудаПравоПроизводствоПсихологияРелигияСоциологияСпортТехникаФизикаФилософияХимияЭкологияЭкономикаЭлектроника
|
Лістинг програми ⇐ ПредыдущаяСтр 4 из 4 //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include <math.h> #include "Main.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; unsigned char **D, **D1, **P; unsigned char Size; unsigned char Start, End; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- int min (int value1, int value2); int min(int value1, int value2) { return ((value1 < value2)? value1: value2); } void __fastcall TForm1::Edit1Change(TObject *Sender) { Size = StrToInt(Edit1->Text); MatrixGrid1->RowCount = Size+1; MatrixGrid1->ColCount = Size+1;
MatrixGrid2->RowCount = Size+1; MatrixGrid2->ColCount = Size+1; MatrixGrid3->RowCount = Size+1; MatrixGrid3->ColCount = Size+1; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click (TObject *Sender) { delete D; delete D1; delete P; D = new unsigned char*[Size]; D1 = new unsigned char*[Size]; P = new unsigned char*[Size]; for (int z = 0; z < Size; z++) { D[z] = new unsigned char[Size]; D1[z] = new unsigned char[Size]; P[z] = new unsigned char[Size]; for (int a = 0; a < Size; a++) { if (a == z) D[a][a] = 0; else D[z][a] = 255; P[z][a] = 0; } } } //--------------------------------------------------------------------------- void __fastcall TForm1::ClearSG (TStringGrid *StrGrid) { for (int r = 0; r < StrGrid->RowCount; r++) for (int c = 0; c < StrGrid->ColCount; c++) StrGrid->Cells[c][r] = " "; StrGrid->Canvas->Brush->Color = clWhite; StrGrid->Canvas->FillRect(Rect(0,0, StrGrid->Width, StrGrid->Height)); } void __fastcall TForm1::DrawMatrix (TStringGrid *Matrix, unsigned char **M) { ClearSG(Matrix); int z,a; for (z = 0; z <= Size; z++) { for (a = 0; a <= Size; a++) { if (z == 0) Matrix->Cells[z][a+1] = IntToStr(a+1); if (a == 0) Matrix->Cells[z+1][a] = IntToStr(z+1); else if (z!= 0) { if (M[z-1][a-1] == 255) Matrix->Cells[z][a] = ""; else Matrix->Cells[z][a] = IntToStr(M[z-1][a-1]); } if (a == z) { Matrix->Canvas->Brush->Color = clLtGray; Matrix->Canvas->Ellipse(Matrix->CellRect(z,a)); Matrix->Canvas->FillRect(Matrix->CellRect(z,a)); } } } Matrix->Cells[0][0] = "N"; Matrix->Canvas->Refresh(); } void __fastcall TForm1::ReadMatrix() { for (int z = 1; z <= Size; z++) { for (int a = 1; a <= Size; a++) { if (MatrixGrid1->Cells[z][a] == "") D[z-1][a-1] = 255; else D[z-1][a-1] = StrToInt(MatrixGrid1->Cells[z][a]); } } } void __fastcall TForm1::Button2Click (TObject *Sender) { DrawMatrix(MatrixGrid1, D); DrawGraph(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click (TObject *Sender) { ReadMatrix(); } //--------------------------------------------------------------------------- void __fastcall TForm1::DrawGraph() { Image1->Canvas->Brush->Color = clWhite; Image1->Canvas->FillRect(Rect(0,0, Image1->Width, Image1->Height));
for (int a = 0; a <Size; a++) { double angle = 2*a*M_PI/Size; Image1->Canvas->Ellipse(Image1->Width/2+(-20+Image1->Width)/2*cos(angle)-10,Image1->Height/2-(-20+Image1->Height)/2*sin(angle)-10,Image1->Width/2+(-20+Image1->Width)/2*cos(angle)+10,Image1->Height/2-(-20+Image1->Height)/2*sin(angle)+10); Image1->Canvas->TextOutA(Image1->Width/2- Image1->Canvas->TextWidth(IntToStr(a+1))/2+(-20+Image1->Width)/2*cos(angle),Image1->Height/2-Image1->Canvas->TextHeight(IntToStr(a+1))/2-(-20+Image1->Height)/2*sin(angle), IntToStr(a+1)); for (int z = 0; z < Size; z++) { if ((D[a][z] > 0)&&(D[a][z]!= 0xFF)) { double Zangle = 2*z*M_PI/Size; Image1->Canvas->Pen->Color = clRed; Image1->Canvas->MoveTo(Image1->Width/2+(-20+Image1->Width)/2*cos(angle),Image1->Height/2-(-20+Image1->Height)/2*sin(angle)); Image1->Canvas->LineTo(Image1->Width/2+(-20+Image1->Width)/2*cos(Zangle),Image1->Height/2-(-20+Image1->Height)/2*sin(Zangle)); // Image1->Canvas->Arc(Image1->Width/2+(-20+Image1->Width)/2*cos(angle), Image1->Height/2-(-20+Image1->Height)/2*sin(angle), Image1->Width/2+(-20+Image1->Width)/2*cos(Zangle), Image1->Height/2-(-20+Image1->Height)/2*sin(Zangle), Image1->Width/2+(-20+Image1->Width)/2*cos(angle), Image1->Height/2-(-20+Image1->Height)/2*sin(angle), Image1->Width/2+(-20+Image1->Width)/2*cos(Zangle), Image1->Height/2-(-20+Image1->Height)/2*sin(Zangle)); Image1->Canvas->Pen->Color = clBlack; } } } } void CopyMatrix (unsigned char ** Dest,unsigned char ** Src) { int x,y; for (x = 0; x < Size; x++) for (y = 0; y < Size; y++) Dest[x][y] = Src[x][y]; } void Floyd_Opt() { int i,j,m = 0;
for (i = 0; i < Size; i++) { for (j = 0; j < Size; j++) { P[i][j] = i+1; } }
while (m < Size - 1) { for (j = 0; j < Size; j++) { for (i = 0; i < Size; i++) { if (i!= j) { if (D1[i][j] > D1[i][m+1]+D1[m+1][j]) { D1[i][j] = D1[i][m+1]+D1[m+1][j]; P[i][j] = P[m+1][j]; } } } } m++; } } { int i,j,m = 0;
for (i = 0; i < Size; i++) { for (j = 0; j < Size; j++) { P[i][j] = i+1; } } while (m < Size - 1) { for (i = 0; i < Size; i++) { for (j = 0; j < Size; j++) { if (i == j) D1[i][i] = 0; else { else { D1[i][j] = D[i][m+1]+D[m+1][j]; P[i][j] = P[m+1][j]; } } } } CopyMatrix(D, D1); m++; } } */ void __fastcall TForm1::Button4Click (TObject *Sender) { unsigned int t1; CopyMatrix(D1,D); t1 = GetTickCount(); Floyd_Opt(); Label8->Caption = (GetTickCount() - t1); DrawMatrix(MatrixGrid2, D1); DrawMatrix(MatrixGrid3, P); } //--------------------------------------------------------------------------- void __fastcall TForm1::Click (TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { for (int a = 0; a <Size; a++) { double angle = 2*a*M_PI/Size; int a_x, a_y; a_x = Image1->Width/2+(-20+Image1->Width)/2*cos(angle); a_y = Image1->Height/2-(-20+Image1->Height)/2*sin(angle); if ((X <= a_x + 10)&&(X >= a_x - 10)&&(Y <= a_y + 10)&&(Y >= a_y - 10)) { if (Button == mbLeft) { Start = a; Label1->Caption = IntToStr(Start + 1); } else if (Button == mbRight) { End = a; Label2->Caption = IntToStr(End + 1); } a = Size; } } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button5Click (TObject *Sender) { DrawGraph(); double angle; Label5->Caption = ""; String Path; // Image1->Canvas->Ellipse(Image1->Width/2+(-20+Image1->Width)/2*cos(angle)-10,Image1->Height/2-(-20+Image1->Height)/2*sin(angle)-10,Image1->Width/2+(-20+Image1->Width)/2*cos(angle)+10,Image1->Height/2-(-20+Image1->Height)/2*sin(angle)+10); // Image1->Canvas->TextOutA(Image1->Width/2- Image1->Canvas->TextWidth(IntToStr(a+1))/2+(-20+Image1->Width)/2*cos(angle),Image1->Height/2-Image1->Canvas->TextHeight(IntToStr(a+1))/2-(-20+Image1->Height)/2*sin(angle), IntToStr(a+1)); Image1->Canvas->Pen->Color = clBlue; int b, a = End; Path = IntToStr (a+1); while (a!= Start) { if (D1[Start][a] == 1) { angle = 2*Start*M_PI/Size; Image1->Canvas->MoveTo(Image1->Width/2+(-20+Image1->Width)/2*cos(angle),Image1->Height/2-(-20+Image1->Height)/2*sin(angle)); angle = 2*a*M_PI/Size; Image1->Canvas->LineTo(Image1->Width/2+(-20+Image1->Width)/2*cos(angle),Image1->Height/2-(-20+Image1->Height)/2*sin(angle)); a = Start; } else if (D1[Start][a] == 255) { Label5->Caption = "No path!"; break; } { angle = 2*a*M_PI/Size; Image1->Canvas->MoveTo(Image1->Width/2+(-20+Image1->Width)/2*cos(angle),Image1->Height/2-(-20+Image1->Height)/2*sin(angle)); b = P[Start][a]-1; angle = 2*b*M_PI/Size; Image1->Canvas->LineTo(Image1->Width/2+(-20+Image1->Width)/2*cos(angle),Image1->Height/2-(-20+Image1->Height)/2*sin(angle)); a = b; Path += "<-"; Path += IntToStr (a+1); } Label5->Caption = D1[Start][End]; Edit2->Text = Path; } // Image1->Canvas->MoveTo(Image1->Width/2+(-20+Image1->Width)/2*cos(Zangle),Image1->Height/2-(-20+Image1->Height)/2*sin(Zangle)); // Image1->Canvas->LineTo(Image1->Width/2+(-20+Image1->Width)/2*cos(Zangle),Image1->Height/2-(-20+Image1->Height)/2*sin(Zangle)); // Image1->Canvas->Arc(Image1->Width/2+(-20+Image1->Width)/2*cos(angle), Image1->Height/2-(-20+Image1->Height)/2*sin(angle), Image1->Width/2+(-20+Image1->Width)/2*cos(Zangle), Image1->Height/2-(-20+Image1->Height)/2*sin(Zangle), Image1->Width/2+(-20+Image1->Width)/2*cos(angle), Image1->Height/2-(-20+Image1->Height)/2*sin(angle), Image1->Width/2+(-20+Image1->Width)/2*cos(Zangle), Image1->Height/2-(-20+Image1->Height)/2*sin(Zangle)); Image1->Canvas->Pen->Color = clBlack; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button6Click (TObject *Sender) { if (OpenDialog1->Execute()) { int iFileHandle, iFileLength; unsigned char *pszBuffer; // int iBytesRead; /* iFileHandle = FileOpen(SaveDialog1->FileName, fmOpenRead); iFileLength = FileSeek(iFileHandle,0,2); FileSeek(iFileHandle,0,0); pszBuffer = new unsigned char[iFileLength]; FileRead(iFileHandle, pszBuffer, iFileLength); FileClose(iFileHandle);*/ iFileHandle = FileOpen(OpenDialog1->FileName, fmOpenRead); FileRead(iFileHandle, &Size, 1); FileRead(iFileHandle, &Start, 1); FileRead(iFileHandle, &End, 1); MatrixGrid1->RowCount = Size+1; MatrixGrid1->ColCount = Size+1; MatrixGrid2->RowCount = Size+1; MatrixGrid2->ColCount = Size+1; MatrixGrid3->RowCount = Size+1; MatrixGrid3->ColCount = Size+1; delete D; delete D1; delete P; D = new unsigned char*[Size]; D1 = new unsigned char*[Size]; P = new unsigned char*[Size]; for (int z = 0; z < Size; z++) { D[z] = new unsigned char[Size]; D1[z] = new unsigned char[Size]; P[z] = new unsigned char[Size]; } int x,y; for (x = 0; x < Size; x++) for (y = 0; y < Size; y++) FileRead(iFileHandle, &D[x][y], 1); FileClose(iFileHandle); DrawMatrix(MatrixGrid1, D); DrawGraph(); Label1->Caption = IntToStr(Start + 1); Label2->Caption = IntToStr(End + 1); Edit1->Text = IntToStr(Size); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button7Click (TObject *Sender) { if (SaveDialog1->Execute()) { int iFileHandle, iFileLength; unsigned char *pszBuffer; // int iBytesRead; /* iFileHandle = FileOpen(SaveDialog1->FileName, fmOpenRead); iFileLength = FileSeek(iFileHandle,0,2); FileSeek(iFileHandle,0,0); pszBuffer = new unsigned char[iFileLength]; FileRead(iFileHandle, pszBuffer, iFileLength); FileClose(iFileHandle);*/
iFileHandle = FileCreate(SaveDialog1->FileName); FileWrite(iFileHandle, &Size, sizeof(Size)); FileWrite(iFileHandle, &Start, sizeof(Start)); FileWrite(iFileHandle, &End, sizeof(End)); int x,y; for (x = 0; x < Size; x++) for (y = 0; y < Size; y++) FileWrite(iFileHandle, &D[x][y], sizeof(D[x][y])); FileClose(iFileHandle); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button8Click (TObject *Sender) { if (SaveDialog2->Execute()) { int iFileHandle, iFileLength; unsigned char *pszBuffer; // int iBytesRead; /* iFileHandle = FileOpen(SaveDialog1->FileName, fmOpenRead); iFileLength = FileSeek(iFileHandle,0,2); FileSeek(iFileHandle,0,0); pszBuffer = new unsigned char[iFileLength]; FileRead(iFileHandle, pszBuffer, iFileLength); FileClose(iFileHandle);*/ iFileHandle = FileCreate(SaveDialog2->FileName); String temp; // temp = "Результат выполнения алгоритма Флойда"; char buffer[100]; char num[10]; FileWrite(iFileHandle, "Результат выполнения алгоритма Флойда:\n", 39); StrCopy(buffer, "Путь из вершины "); itoa(Start+1, num, 10); StrCat(buffer, num); StrCat(buffer, " в вершину "); itoa(End+1, num, 10); StrCat(buffer, num); if (D1[Start][End] == 255) { StrCat(buffer, " не существует!"); } else { StrCat(buffer, " найден!\nПуть: "); // StrCat(buffer, Edit2->Text.c_str()); StrCat(buffer, "\nВремя работы алгоритма: "); //StrCat(buffer, Label8->Caption.c_str()); StrCat(buffer, " мс"); } FileWrite(iFileHandle, buffer, StrLen(buffer)); FileClose(iFileHandle); } } //---------------------------------------------------------------------------
|