Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit2;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls, Vcl.Menus, Math;
- type
- TAleksey = class(TForm)
- TaskMemo: TMemo;
- MainMenu: TMainMenu;
- FileMenu: TMenuItem;
- OpenMenu: TMenuItem;
- SaveFile: TMenuItem;
- InformationMenu: TMenuItem;
- TaskMenu: TMenuItem;
- AboutMeMenu: TMenuItem;
- InputNumOfPoint: TEdit;
- Label1: TLabel;
- NumButton: TButton;
- InputPoint: TStringGrid;
- ResultButton: TButton;
- Label2: TLabel;
- AnswerLabel: TLabel;
- StaticTextX: TStaticText;
- StaticTextY: TStaticText;
- SaveFileDialog: TSaveDialog;
- OpenFile: TOpenDialog;
- procedure TaskMenuClick(Sender: TObject);
- procedure AboutMeMenuClick(Sender: TObject);
- procedure ConfirmExit(Sender: TObject; var CanClose: Boolean);
- procedure InputKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
- procedure InputKeyPress(Sender: TObject; var Key: Char);
- procedure InputChange(Sender: TObject);
- procedure InputKeyPressArray(Sender: TObject; var Key: Char);
- procedure NumButtonClick(Sender: TObject);
- procedure ResultButtonClick(Sender: TObject);
- procedure SaveFileClick(Sender: TObject);
- procedure OpenMenuClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Aleksey: TAleksey;
- implementation
- {$R *.dfm}
- Type Point = record
- X,Y: integer;
- end;
- procedure TAleksey.AboutMeMenuClick(Sender: TObject);
- begin
- ShowMessage('Данная программа разработана студентом 951007 группы Вышемирским Алексеем Владиславовичем.');
- end;
- procedure TAleksey.TaskMenuClick(Sender: TObject);
- begin
- ShowMessage('Вычислить A1 + 2A2 + 3A3 + … + NAN');
- end;
- procedure TAleksey.ConfirmExit(Sender: TObject; var CanClose: Boolean);
- begin
- case Application.MessageBox('Вы уверены, что хотите выйти из программы?', 'Выход', MB_YESNO) of ID_YES: ;
- else
- CanClose := False;
- end;
- end;
- procedure TAleksey.InputKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
- begin
- if (Key = VK_INSERT) then
- Key := 0;
- end;
- procedure TAleksey.InputKeyPress(Sender: TObject; var Key: Char);
- begin
- if not (Key in ['0'..'9', #8])then
- Key := #0;
- end;
- procedure TAleksey.InputChange(Sender: TObject);
- begin
- if (Sender as TEdit).Text = '00' then
- (Sender as TEdit).Clear
- end;
- procedure TAleksey.InputKeyPressArray(Sender: TObject; var Key: Char);
- begin
- if not (Key in ['0'..'9', #8, '-'])then
- Key := #0;
- end;
- procedure TAleksey.NumButtonClick(Sender: TObject);
- Var
- NCol: integer;
- begin
- if ((InputNumOfPoint.Text <> '') and (StrToInt(InputNumOfPoint.Text) < 9) and (StrToInt(InputNumOfPoint.Text) > 2)) then
- Begin
- InputPoint.ColCount := StrToInt(InputNumOfPoint.Text);
- InputPoint.EditorMode := True;
- InputPoint.Options:= InputPoint.Options + [goEditing] + [goTabs];
- for NCol := 0 to InputPoint.ColCount - 1 do
- InputPoint.Cells[NCol, 0] := IntToStr(NCol + 1) + ' точка';
- ResultButton.Enabled := True;
- End
- else
- ShowMessage('Ошибка! Указана недопустимая длина массива.');
- end;
- function getAngle(Point1, Point2, Point3: Point): Double;
- var
- Line1, Line2, Line3: Double;
- Begin
- Line1 := sqrt((Point2.y - Point1.y) * (Point2.y - Point1.y) +
- (Point2.x - Point1.x) * (Point2.x - Point1.x));
- line2 := sqrt((Point3.y - Point2.y) * (Point3.y - Point2.y) +
- (Point3.x - Point2.x) * (Point3.x - Point2.x));
- line3 := sqrt((Point1.y - Point3.y) * (Point1.y - Point3.y) +
- (Point1.x - Point3.x) * (Point1.x - Point3.x));
- getAngle := Round(radtodeg(ArcCos((sqr(Line1) + sqr(Line2) - sqr(Line3)) / (2 * Line1 * Line2))));
- end;
- function getPoint(point1, point2: integer): Point;
- begin
- Result.X := point1;
- Result.Y := point2;
- end;
- procedure TAleksey.ResultButtonClick(Sender: TObject);
- Var
- i, Num: Integer;
- PointArr: array of Point;
- AngleArr: array of Real;
- Sum: Real;
- EmptyArray: boolean;
- begin
- Num := StrToInt(InputNumOfPoint.Text);
- setLength(PointArr, Num);
- setLength(AngleArr, Num);
- Sum := 0;
- EmptyArray := False;
- for i := 0 to High(PointArr) do
- begin
- if ((InputPoint.Cells[i,1] = '') or (InputPoint.Cells[i,2] = '')) then
- Begin
- EmptyArray := True;
- InputPoint.Cells[i,1] := IntToStr(RandomRange(-20,20));
- InputPoint.Cells[i,2] := IntToStr(RandomRange(-20,20));
- End;
- pointArr[i] := getPoint(strToInt(InputPoint.Cells[i,1]),strToInt(InputPoint.Cells[i,2]));
- end;
- if(EmptyArray) then
- ShowMessage('Таблица содержит пустые поля, они были заменены рандомными значениями от -20 до 20');
- for i := 0 to High(PointArr) do
- Sum := Sum + getAngle(pointArr[i],
- pointArr[(i + 1) mod Length(PointArr)],
- pointArr[(i + 2) mod Length(PointArr)]);
- if (Sum = 180 * (Num - 2)) then
- AnswerLabel.Caption := 'Выпуклый'
- else
- AnswerLabel.Caption := 'Не выпуклый';
- SaveFile.Enabled := True;
- end;
- function FileExtensionChek(var NameOfFile: string): Boolean;
- var
- Extension: string;
- i, j: Integer;
- begin
- if(pos('.',NameOfFile) = 0)then
- begin
- ShowMessage('Так как во введённом имени файла не указано расширение, автоматически присвоено расширение ".txt".');
- NameOfFile := NameOfFile + '.txt';
- FileExtensionChek := 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('Внимание, произошла ошибка! Файл с данным расширением не может быть использован. Программа поддерживает расширения : ".txt", ".doc", ".text".');
- FileExtensionChek := False;
- end
- else
- FileExtensionChek := True;
- end;
- end;
- procedure TAleksey.SaveFileClick(Sender: TObject);
- var
- FileF: TextFile;
- FileName: string;
- IsInvalidInput: Boolean;
- begin
- if SaveFileDialog.Execute then
- begin
- FileName := SaveFileDialog.FileName;
- IsInvalidInput := True;
- IsInvalidInput := FileExtensionChek(FileName);
- if FileExists(FileName) and IsInvalidInput then
- begin
- AssignFile(FileF, SaveFileDialog.FileName);
- Rewrite(FileF);
- Write(FileF,TaskMemo.Text);
- Write(FileF,'Многоугольник: ' + AnswerLabel.Caption);
- CloseFile(FileF);
- end
- else if IsInvalidInput then
- ShowMessage('Ошибка! Данный файл не найден.');
- end;
- end;
- function CheckSize(FileName: string): String;
- var
- Error: Boolean;
- Temp, Count: Integer;
- TempFile: TextFile;
- begin
- Error := False;
- Count := 0;
- try
- AssignFile(TempFile, FileName);
- Reset(TempFile);
- while (not (EOLn(TempFile))) do
- begin
- Read(TempFile, Temp);
- Inc(Count);
- end;
- except
- Error := True;
- end;
- if Error then
- CheckSize := '0'
- else
- CheckSize := IntToStr(Count);
- end;
- procedure TAleksey.OpenMenuClick(Sender: TObject);
- var
- NumbFile: TextFile;
- Temp, I: Integer;
- Error: Boolean;
- begin
- Error := False;
- if OpenFile.Execute then
- begin
- try
- InputNumOfPoint.Text := CheckSize(OpenFile.FileName);
- NumButtonClick(Sender);
- AssignFile(NumbFile, OpenFile.FileName);
- Reset(NumbFile);
- if SeekEof(NumbFile) then
- MessageDlg('Похоже, файл пустой. Повторите попытку', mtError, [mbRetry], 0)
- else
- begin
- for I := 0 to StrToInt(InputNumOfPoint.Text) - 1 do
- begin
- Read(NumbFile, Temp);
- InputPoint.Cells[I, 1] := IntToStr(Temp);
- end;
- for I := 0 to StrToInt(InputNumOfPoint.Text) - 1 do
- Begin
- Read(NumbFile,Temp);
- InputPoint.Cells[I, 2] := IntToStr(Temp);
- End;
- end;
- except
- Error := True;
- end;
- CloseFile(NumbFile);
- end;
- if (Error)then
- begin
- MessageDlg('Ошибка ввода, повторите попытку', mtError, [mbRetry], 0);
- NumButtonClick(Sender);
- end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement