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.StdCtrls, Vcl.Grids, Vcl.Menus;
- type
- TForm1 = class(TForm)
- ButtonSolve: TButton;
- Memo1: TMemo;
- StringGridPoints: TStringGrid;
- ButtonPoints: TButton;
- EditAmount: TEdit;
- MainMenu1: TMainMenu;
- N1: TMenuItem;
- N2: TMenuItem;
- N3: TMenuItem;
- N5: TMenuItem;
- N6: TMenuItem;
- N7: TMenuItem;
- N8: TMenuItem;
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- procedure ButtonSolveClick(Sender: TObject);
- procedure ButtonPointsClick(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure EditAmountKeyPress(Sender: TObject; var Key: Char);
- procedure EditAmountChange(Sender: TObject);
- procedure StringGridPointsKeyPress(Sender: TObject; var Key: Char);
- procedure StringGridPointsSetEditText(Sender: TObject;
- ACol, ARow: Integer; const Value: string);
- procedure N3Click(Sender: TObject);
- procedure N8Click(Sender: TObject);
- procedure N5Click(Sender: TObject);
- procedure N6Click(Sender: TObject);
- procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- procedure N2Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- TPoint = record
- X, Y: Real;
- end;
- TLine = record
- K, B: Real;
- end;
- var
- Form1: TForm1;
- Points: TArray<TPoint>;
- LineWithMostPoints: TLine;
- IsFileOpen: Boolean;
- Path: String;
- implementation
- {$R *.dfm}
- function FindLineWithMostPoints(const Points: TArray<TPoint>): TLine;
- var
- ResultLine: TLine;
- MaxPointsCount, PointsOnLine, I, J, P: Integer;
- K, B: Real;
- begin
- ResultLine.K := 0;
- ResultLine.B := 0;
- MaxPointsCount := 0;
- for I := 0 to High(Points) do
- begin
- for J := I + 1 to High(Points) do
- begin
- PointsOnLine := 2; // Две уже минимум есть
- if (Points[J].X - Points[I].X) <> 0 then
- K := (Points[J].Y - Points[I].Y) / (Points[J].X - Points[I].X)
- Else
- Dec(PointsOnLine);
- B := Points[I].Y - K * Points[I].X;
- for P := 0 to High(Points) do
- begin
- if (P <> I) and (P <> J) and
- (Abs(Points[P].Y - (K * Points[P].X + B)) < 0.0001) then
- Inc(PointsOnLine);
- end;
- if PointsOnLine > MaxPointsCount then
- begin
- MaxPointsCount := PointsOnLine;
- ResultLine.K := K;
- ResultLine.B := B;
- end;
- end;
- end;
- Result := ResultLine;
- end;
- procedure TForm1.ButtonSolveClick(Sender: TObject);
- var
- Size, I: Integer;
- begin
- Size := StrToInt(EditAmount.Text);
- SetLength(Points, Size);
- for I := 0 to Size - 1 do
- Begin
- Points[I].X := StrToFloat(StringGridPoints.Cells[I + 1, 1]);
- Points[I].Y := StrToFloat(StringGridPoints.Cells[I + 1, 2]);
- End;
- LineWithMostPoints := FindLineWithMostPoints(Points);
- N6.Enabled := True;
- Memo1.Text := 'Уравнение прямой с большинством точек: y = ' +
- FloatToStr(LineWithMostPoints.K) + 'x + ' +
- FloatToStr(LineWithMostPoints.B);
- end;
- procedure TForm1.ButtonPointsClick(Sender: TObject);
- var
- I: Integer;
- begin
- StringGridPoints.Enabled := True;
- StringGridPoints.Rows[1].Clear;
- StringGridPoints.Rows[2].Clear;
- StringGridPoints.Cells[0, 1] := 'X';
- StringGridPoints.Cells[0, 2] := 'Y';
- StringGridPoints.ColCount := StrToInt(EditAmount.Text) + 1;
- For I := 1 to StringGridPoints.ColCount do
- StringGridPoints.Cells[I, 0] := IntToStr(I);
- Memo1.Clear;
- ButtonSolve.Enabled := False;
- N6.Enabled := False;
- end;
- procedure TForm1.EditAmountChange(Sender: TObject);
- var
- Num: Integer;
- IsCorrect: Boolean;
- begin
- IsCorrect := True;
- try
- Num := StrToInt(EditAmount.Text)
- Except
- IsCorrect := False;
- end;
- if IsCorrect And (Num > 1) And (Num <= 20) then
- ButtonPoints.Enabled := True
- Else
- Begin
- ButtonPoints.Enabled := False;
- IsCorrect := False;
- End;
- StringGridPoints.Enabled := False;
- ButtonSolve.Enabled := False;
- Memo1.Clear;
- N6.Enabled := False
- end;
- procedure TForm1.EditAmountKeyPress(Sender: TObject; var Key: Char);
- begin
- If not(Key in ['0' .. '9', #13, #8]) Then
- Key := #0;
- if (ButtonPoints.Enabled = True) And (Key = #13) then
- ButtonPoints.Click
- end;
- procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- CanClose := Application.MessageBox('Вы действительно хотите выйти?',
- 'Выход', MB_ICONQUESTION + MB_YESNO) = ID_YES;
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- StringGridPoints.Cells[0, 0] := '№/№';
- StringGridPoints.Cells[0, 1] := 'X';
- StringGridPoints.Cells[0, 2] := 'Y';
- end;
- procedure TForm1.N2Click(Sender: TObject);
- const
- Info1 = 'На плоскости заданы n точек своими координатами. Найти уравнение'#13#10;
- Info2 = 'прямой, которой принадлежит наибольшее число данных точек.'#13#10;
- Info3 = '1. Минимальное количество точек - 2, максимальная - 20.'#13#10;
- Info4 = '2. Диапазон ввода координат от 0 до 99.'#13#10;
- begin
- Application.MessageBox(Info1 + Info2 + Info3 + Info4, 'Справка', 0);
- end;
- procedure TForm1.N3Click(Sender: TObject);
- begin
- Application.MessageBox('Сымоник Вадим, гр. 251004', 'О разработчике', 0)
- end;
- Function GetSize(var FileInput: TextFile): String;
- Const
- MIN_NUM = 2;
- MAX_NUM = 20;
- Var
- Size, Num: Integer;
- Str: String;
- IsCorrect: Boolean;
- Begin
- Size := 0;
- Str := '';
- If Not Eof(FileInput) Then
- Begin
- IsCorrect := True;
- Try
- Read(FileInput, Size);
- Except
- MessageBox(Form1.Handle, PChar('Недопустимый размер массива!'),
- 'Ошибка', MB_ICONSTOP);
- IsCorrect := False;
- Size := 0;
- End;
- End
- Else
- MessageBox(Form1.Handle, PChar('Недостаточно данных в файле!'),
- 'Ошибка', MB_ICONSTOP);
- If (Size >= MIN_NUM) And (Size <= MAX_NUM) Then
- Str := IntToStr(Size)
- Else
- Application.MessageBox('Проверьте корректность данных в файле',
- 'Ошибка', 0);
- GetSize := Str;
- End;
- Function TakeInformationIntoCell(Var FileInput: TextFile;
- Var IsCorrect: Boolean): String;
- Var
- Temp: Integer;
- Str: String;
- Begin
- If Not Eof(FileInput) Then
- Begin
- Try
- Read(FileInput, Temp);
- Str := IntToStr(Temp);
- Except
- MessageBox(Form1.Handle, PChar('Неверные данные!'), 'Ошибка',
- MB_ICONSTOP);
- IsCorrect := False;
- End;
- If IsCorrect And (Temp < 0) And (Temp >= 100) Then
- Begin
- IsCorrect := False;
- MessageBox(Form1.Handle,
- PChar('Недопустимый диапазон входных данных!'), 'Ошибка',
- MB_ICONSTOP);
- Str := '';
- End;
- End
- Else
- Begin
- IsCorrect := False;
- MessageBox(Form1.Handle, PChar('Недостаточно значений в файле!'),
- 'Ошибка', MB_ICONSTOP);
- Str := '';
- End;
- TakeInformationIntoCell := Str;
- End;
- Procedure InputPointsInMatrix(var FileInput: TextFile);
- Var
- I, J: Integer;
- IsCorrect: Boolean;
- Begin
- IsCorrect := True;
- With Form1 do
- Begin
- For I := 1 to StringGridPoints.ColCount - 1 do
- Begin
- For J := 1 to StringGridPoints.RowCount - 1 do
- Begin
- StringGridPoints.Cells[I, J] :=
- TakeInformationIntoCell(FileInput, IsCorrect);
- End;
- End;
- ButtonSolve.Enabled := IsCorrect;
- End;
- End;
- procedure TForm1.N5Click(Sender: TObject);
- var
- FileInput: TextFile;
- begin
- If OpenDialog1.Execute Then
- Begin
- AssignFile(FileInput, OpenDialog1.FileName);
- Try
- Try
- Reset(FileInput);
- EditAmount.Text := GetSize(FileInput);
- if EditAmount.Text <> '' then
- Begin
- ButtonPoints.Click;
- InputPointsInMatrix(FileInput);
- End;
- Finally
- CloseFile(FileInput);
- End;
- Except
- End;
- End;
- end;
- Function Open(): String;
- begin
- with Form1 Do
- begin
- If SaveDialog1.Execute Then
- begin
- Path := SaveDialog1.FileName;
- IsFileOpen := True;
- end
- Else
- IsFileOpen := False;
- end;
- Open := Path;
- end;
- procedure TForm1.N6Click(Sender: TObject);
- var
- F: TextFile;
- begin
- Path := Open;
- If IsFileOpen Then
- Begin
- AssignFile(F, Path);
- Rewrite(F);
- Writeln(F, Memo1.Text);
- Application.MessageBox('Данные успешно сохранены в файл',
- 'Результат', 0);
- CloseFile(F);
- End;
- end;
- procedure TForm1.N8Click(Sender: TObject);
- begin
- Form1.Close
- end;
- procedure TForm1.StringGridPointsKeyPress(Sender: TObject; var Key: Char);
- begin
- If not(Key in ['0' .. '9', #8, #13]) Then
- Key := #0;
- if (Key = #13) And (ButtonSolve.Enabled = True) then
- ButtonSolve.Click
- end;
- procedure TForm1.StringGridPointsSetEditText(Sender: TObject;
- ACol, ARow: Integer; const Value: string);
- var
- I, J: Integer;
- N: Integer;
- IsCorrect: Boolean;
- begin
- For I := 1 to StringGridPoints.RowCount - 1 do
- Begin
- for J := 1 to StringGridPoints.ColCount - 1 do
- Begin
- IsCorrect := True;
- If StringGridPoints.Cells[I, J] <> '' Then
- Begin
- Try
- N := StrToInt(StringGridPoints.Cells[I, J]);
- Except
- StringGridPoints.Cells[I, J] := '';
- Application.MessageBox
- ('Проверьте корректность введенных данных', 'Ошибка', 0);
- IsCorrect := False;
- End;
- End;
- If IsCorrect And (Length(StringGridPoints.Cells[I, J]) > 2) Then
- Begin
- StringGridPoints.Cells[I, J] := '';
- Application.MessageBox('Диапазон ввода [0; 99]', 'Ошибка', 0);
- End;
- End;
- End;
- For J := 1 to StringGridPoints.RowCount - 1 do
- for I := 1 to StringGridPoints.ColCount - 1 do
- If Length(StringGridPoints.Cells[I, J]) = 0 Then
- IsCorrect := False;
- ButtonSolve.Enabled := IsCorrect;
- Memo1.Clear;
- N6.Enabled := False
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement