Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
- System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.Grids;
- type
- TArray = array of Integer;
- TForm1 = class(TForm)
- mnPanel: TMainMenu;
- File1: TMenuItem;
- Help1: TMenuItem;
- Open1: TMenuItem;
- Save1: TMenuItem;
- N1: TMenuItem;
- Exit1: TMenuItem;
- Aboutprogram1: TMenuItem;
- Aboutthedeveloper1: TMenuItem;
- lbDescription: TLabel;
- lbCoordinates: TLabel;
- lbAnswer: TLabel;
- PopupMenu1: TPopupMenu;
- SaveFileDialog: TSaveDialog;
- edInputN: TEdit;
- lbN: TLabel;
- OpenFileDialog: TOpenDialog;
- bAnswer: TButton;
- sgFillMatrix: TStringGrid;
- procedure edInputAKeyPress(Sender: TObject; var Key: Char);
- procedure Aboutprogram1Click(Sender: TObject);
- procedure Aboutthedeveloper1Click(Sender: TObject);
- procedure Exit1Click(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure Save1Click(Sender: TObject);
- procedure Open1Click(Sender: TObject);
- function FillArrCoords(CoordsArr: TArray; ArrLength: Integer): Boolean;
- procedure bAnswerClick(Sender: TObject);
- procedure sgFillMatrixDblClvarick(Sender: TObject);
- function CheckN(TextN: String): Boolean;
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.Aboutprogram1Click(Sender: TObject);
- begin
- ShowMessage
- ('This program will caluclate the smallest side of a N gone, where N entered from the keyboard');
- end;
- procedure TForm1.Aboutthedeveloper1Click(Sender: TObject);
- begin
- ShowMessage
- ('This program was developed by a student of the group 951007 Alexander Voroshilov');
- end;
- function TForm1.CheckN(TextN: String): Boolean;
- var
- N: Integer;
- IsCorrect: Boolean;
- begin
- IsCorrect := True;
- try
- N := StrToInt(TextN);
- if (N < 3) or (N > 9) then
- begin
- ShowMessage('Incorrect value of N. N should be from 3 to 9');
- IsCorrect := False;
- end;
- except
- ShowMessage('Empty field or not integer value of N.');
- IsCorrect := False;
- end;
- if not IsCorrect then
- edInputN.Text := '';
- CheckN := IsCorrect;
- end;
- function CheckPoint(CoordsArr: TArray; i: Integer): Boolean;
- const
- ErrorMessage = 'Error. Some points are on one point. Try again ';
- var
- j: Integer;
- IsCorrect: Boolean;
- begin
- IsCorrect := True;
- j := 0;
- while (j <= i - 2) do
- begin
- if (CoordsArr[j] = CoordsArr[i]) and (CoordsArr[j + 1] = CoordsArr[i + 1])
- then
- begin
- IsCorrect := False;
- ShowMessage(ErrorMessage);
- end;
- j := j + 2;
- end;
- Result := IsCorrect;
- end;
- function CheckLine(CoordsArr: TArray; i: Integer): Boolean;
- const
- ErrorMessage =
- 'Error. Some points are on one line, that is not correct for polygon. Try again ';
- var
- IsCorrect: Boolean;
- begin
- IsCorrect := True;
- // ((y1-y2)*x3+(x2-x1)*y3+(x1*y2-x2*y1)==0) - one line
- if ((CoordsArr[i - 3] - CoordsArr[i - 1]) * CoordsArr[i] +
- (CoordsArr[i - 2] - CoordsArr[i - 4]) * CoordsArr[i + 1] +
- (CoordsArr[i - 4] * CoordsArr[i - 1] - CoordsArr[i - 2] * CoordsArr[i - 3]
- ) = 0) then
- begin
- IsCorrect := False;
- ShowMessage(ErrorMessage);
- end;
- Result := IsCorrect;
- end;
- function TForm1.FillArrCoords(CoordsArr: TArray; ArrLength: Integer): Boolean;
- var
- IsCorrect1: Boolean;
- IsCorrect2: Boolean;
- i: Integer;
- j: Integer;
- begin
- i := 0;
- j := 1;
- IsCorrect1 := True;
- IsCorrect2 := True;
- while (i < ArrLength * 2 - 1) do
- begin
- if IsCorrect1 and IsCorrect2 then
- begin
- try
- CoordsArr[i] := StrToInt(sgFillMatrix.Cells[j, (i div 2) + 1]);
- CoordsArr[i + 1] :=
- StrToInt(sgFillMatrix.Cells[(j + 1), (i div 2) + 1]);
- if i >= 2 then
- begin
- IsCorrect1 := CheckPoint(CoordsArr, i);
- if i >= 4 then
- IsCorrect2 := CheckLine(CoordsArr, i);
- end;
- except
- ShowMessage('Empty fields or not integer coordinates');
- IsCorrect1 := False;
- end;
- end;
- i := i + 2;
- end;
- if IsCorrect1 and IsCorrect2 then
- FillArrCoords := True
- else
- FillArrCoords := False;
- end;
- function CalculateSize(CoordsFirst: Integer; CoordsSecond: Integer;
- CoordsArr: TArray): Double;
- begin
- // side^2 = (х2 — х1)^2 + (y2 — y1)^2
- Result := Sqrt((CoordsArr[CoordsSecond] * CoordsArr[CoordsSecond] +
- CoordsArr[CoordsFirst] * CoordsArr[CoordsFirst] - 2 * CoordsArr
- [CoordsSecond] * CoordsArr[CoordsFirst]) + (CoordsArr[CoordsSecond + 1] *
- CoordsArr[CoordsSecond + 1] + CoordsArr[CoordsFirst + 1] *
- CoordsArr[CoordsFirst + 1] - 2 * CoordsArr[CoordsFirst + 1] *
- CoordsArr[CoordsSecond + 1]));
- end;
- function FindSide(ArrLength: Integer; CoordsArr: TArray): String;
- var
- Size: Double;
- NumberOfXandY: Integer;
- EqualSides: Integer;
- TempSide: Double;
- LastSide: Double;
- FirstPoint: Integer;
- SecondPoint: Integer;
- i: Integer;
- Answer: String;
- begin
- EqualSides := 0;
- FirstPoint := 0;
- SecondPoint := ArrLength * 2 - 2;
- // LastSide = side between (x1,y1) and (xLast,yLast)
- LastSide := CalculateSize(FirstPoint, SecondPoint, CoordsArr);
- Size := LastSide;
- NumberOfXandY := ArrLength;
- i := 0;
- while i < ArrLength * 2 - 3 do
- begin
- FirstPoint := i;
- SecondPoint := i + 2;
- TempSide := CalculateSize(FirstPoint, SecondPoint, CoordsArr);
- if (TempSide < Size) then
- begin
- Size := TempSide;
- NumberOfXandY := (i + 2) div 2;
- EqualSides := 0;
- end
- else if (TempSide = Size) then
- EqualSides := EqualSides + 1;
- i := i + 2;
- end;
- Answer := 'The smallest side is ';
- if NumberOfXandY = ArrLength then
- Answer := Answer + '(x' + IntToStr(NumberOfXandY) + ', y' +
- IntToStr(NumberOfXandY) + ') and (x1, y1)'
- else
- Answer := Answer + '(x' + IntToStr(NumberOfXandY) + ', y' +
- IntToStr(NumberOfXandY) + ') and (x' + IntToStr(NumberOfXandY + 1) +
- ', y' + IntToStr(NumberOfXandY + 1) + ')';
- Answer := Answer + ' and equal ' + FloatToStr(Size);
- Answer := Answer + #13#10 + IntToStr(EqualSides) +
- ' other sides also have this length';
- FindSide := Answer;
- end;
- procedure TForm1.bAnswerClick(Sender: TObject);
- var
- N: Integer;
- IsCorrect: Boolean;
- CoordsArr: TArray;
- begin
- N := StrToInt(edInputN.Text);
- SetLength(CoordsArr, N * 2);
- IsCorrect := FillArrCoords(CoordsArr, N);
- if IsCorrect then
- lbAnswer.Caption := FindSide(N, CoordsArr);
- end;
- function FileExtensionCheck(var NameOfFile: string): Boolean;
- var
- Extension: string;
- i, j: Integer;
- begin
- if (pos('.', NameOfFile) = 0) then
- begin
- ShowMessage
- ('Since the extension does not appear in the entered file name, the extension ".txt" is automatically assigned.');
- NameOfFile := NameOfFile + '.txt';
- FileExtensionCheck := True;
- end
- else
- begin
- Extension := '';
- j := length(NameOfFile);
- for i := pos('.', NameOfFile) to j do
- Extension := Extension + NameOfFile[i];
- if (Extension <> '.txt') and (Extension <> '.doc') and
- (Extension <> '.text') then
- begin
- ShowMessage
- ('Attention, an error has occurred! A file with this extension cannot be used. The program supports the extensions: ".txt", ".doc", ".text".');
- FileExtensionCheck := False;
- end
- else
- FileExtensionCheck := True;
- end;
- end;
- procedure TForm1.edInputAKeyPress(Sender: TObject; var Key: Char);
- begin
- if not(Key in [#8, #13, ',', '-', '0' .. '9']) then
- begin
- ShowMessage('Enter numbers!');
- Key := #0;
- end;
- end;
- procedure TForm1.Exit1Click(Sender: TObject);
- begin
- Close;
- end;
- procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- case Application.MessageBox('Are you sure you want to exit the program?',
- 'Exit', MB_YESNO) of
- ID_YES:
- ;
- else
- CanClose := False;
- end;
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- end;
- procedure TForm1.Open1Click(Sender: TObject);
- var
- SourceFile: TextFile;
- FileData, FileName: String;
- IsInvalidInput: Boolean;
- i: Integer;
- N: Integer;
- NRow, NCol: Integer;
- begin
- if OpenFileDialog.Execute then
- begin
- FileName := OpenFileDialog.FileName;
- IsInvalidInput := FileExtensionCheck(FileName);
- if FileExists(FileName) and IsInvalidInput then
- begin
- try
- AssignFile(SourceFile, FileName);
- Reset(SourceFile);
- except
- ShowMessage
- ('Error! Impossible to open the file. Please, check the file.');
- end;
- if EoF(SourceFile) then
- ShowMessage('Error! The file is empty')
- else
- begin
- ReadLn(SourceFile, FileData);
- edInputN.Text := FileData;
- try
- sgFillMatrixDblClvarick(Sender);
- if (sgFillMatrix.EditorMode) then
- begin
- N := StrToInt(FileData);
- edInputN.Text := IntToStr(N);
- for NRow := 1 to sgFillMatrix.RowCount do
- begin
- i := 1;
- ReadLn(SourceFile, FileData);
- for NCol := 1 to N - 1 do
- begin
- while (FileData[i] in [',', '0' .. '9']) do
- begin
- sgFillMatrix.Cells[NCol, NRow] :=
- sgFillMatrix.Cells[NCol, NRow] + FileData[i];
- Inc(i);
- end;
- Inc(i);
- end;
- end
- end;
- except
- ShowMessage('Error! Invalid N');
- end
- end;
- CloseFile(SourceFile);
- end
- else if IsInvalidInput then
- ShowMessage('Error! The file is not found.');
- end;
- end;
- procedure TForm1.Save1Click(Sender: TObject);
- var
- AnswerFile: TextFile;
- FileName: string;
- IsInvalidInput: Boolean;
- begin
- if SaveFileDialog.Execute then
- begin
- FileName := SaveFileDialog.FileName;
- IsInvalidInput := FileExtensionCheck(FileName);
- if FileExists(FileName) and IsInvalidInput then
- begin
- AssignFile(AnswerFile, SaveFileDialog.FileName);
- Rewrite(AnswerFile);
- Write(AnswerFile, lbAnswer.Caption);
- CloseFile(AnswerFile);
- end
- else if IsInvalidInput then
- ShowMessage('Error! The file is not found.');
- end;
- end;
- procedure TForm1.sgFillMatrixDblClvarick(Sender: TObject);
- var
- NRow: Integer;
- begin
- if (CheckN(edInputN.Text)) then
- begin
- sgFillMatrix.RowCount := StrToInt(edInputN.Text) + 1;
- sgFillMatrix.EditorMode := True;
- sgFillMatrix.Options := sgFillMatrix.Options + [goEditing] + [goTabs];
- sgFillMatrix.Cells[0, 0] := '=)';
- sgFillMatrix.Cells[1, 0] := 'x';
- sgFillMatrix.Cells[2, 0] := 'y';
- for NRow := 1 to sgFillMatrix.RowCount - 1 do
- sgFillMatrix.Cells[0, NRow] := IntToStr(NRow) + ' coord';
- bAnswer.Visible := True;
- end
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement