Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {$R *.dfm}
- uses UnitAbout, UnitError, UnitExit, UnitDrawTree, UnitNodesAndLeaves,
- UnitInstruction_5_2, UnitTask_5_2;
- const
- MAX_DATA = 99;
- MIN_DATA = -99;
- SIZE_NODE = 28;
- HALF_SIZE_NODE = 14;
- TEXT_INDENT = 6;
- type
- TPointer = ^TElement;
- TElement = record
- Data: Integer;
- NextLeft: TPointer;
- NextRight: TPointer;
- end;
- procedure AddElement(var Curr: TPointer; Data: Integer); stdcall; external 'Lab_5_2_Lib';
- procedure DeleteElement(var Curr: TPointer; Data: Integer); stdcall; external 'Lab_5_2_Lib';
- function Power(Numb, Pow: Integer): Integer; stdcall; external 'Lab_5_2_Lib';
- function SearchElementInTree(Curr: TPointer; Numb: Integer): TPointer; stdcall; external 'Lab_5_2_Lib';
- procedure CountInTree(Curr: TPointer; var Nodes: Integer; var Leaves: Integer); stdcall; external 'Lab_5_2_Lib';
- var
- Head: TPointer;
- procedure TFormMain.ButtonAddElementClick(Sender: TObject);
- var
- IsCorrect: Boolean;
- Error: String;
- Data: Integer;
- Elem: TPointer;
- begin
- IsCorrect := true;
- ButtonAddElement.Enabled := false;
- try
- Data := StrToInt(EditChangeTree.Text);
- if (Data < MIN_DATA) or (Data > MAX_DATA) then
- begin
- IsCorrect := false;
- Error := 'Значение элемента находится вне допустимого диапазона. ';
- end;
- except
- IsCorrect := false;
- Error := 'Введено некорректное значение элемента. ';
- end;
- if (IsCorrect) then
- begin
- AddElement(Head, Data);
- EditChangeTree.Text := '';
- end
- else
- begin
- UnitError.FormError.LabelError.Caption := 'Ошибка! ' + Error;
- UnitError.FormError.ShowModal();
- UnitError.FormError.LabelError.Caption := '';
- end;
- end;
- procedure TFormMain.ButtonDeleteElementClick(Sender: TObject);
- var
- IsCorrect: Boolean;
- Error: String;
- Data: Integer;
- Elem: TPointer;
- begin
- IsCorrect := true;
- ButtonDeleteElement.Enabled := false;
- try
- Data := StrToInt(EditChangeTree.Text);
- if (Data < MIN_DATA) or (Data > MAX_DATA) then
- begin
- IsCorrect := false;
- Error := 'Значение элемента находится вне допустимого диапазона. ';
- end;
- except
- IsCorrect := false;
- Error := 'Введено некорректное значение элемента. ';
- end;
- if (IsCorrect) then
- begin
- if (SearchElementInTree(Head, Data) <> nil) then
- DeleteElement(Head, Data)
- else
- begin
- Error := 'Данный элемент не найден в дереве.';
- UnitError.FormError.LabelError.Caption := 'Ошибка! ' + Error;
- UnitError.FormError.ShowModal();
- UnitError.FormError.LabelError.Caption := '';
- end;
- EditChangeTree.Text := '';
- end
- else
- begin
- UnitError.FormError.LabelError.Caption := 'Ошибка! ' + Error;
- UnitError.FormError.ShowModal();
- UnitError.FormError.LabelError.Caption := '';
- end;
- end;
- procedure DrawNode(Curr: TPointer; Level: Integer; IsLeft: Boolean; X, Y: Integer);
- var
- m, l, i: Integer; //m - середина, l - ширина, i - отступ
- CurrX, CurrY: Integer;
- begin
- if Curr <> nil then
- begin
- l := UnitDrawTree.FormTree.ImageTree.Width;
- m := l div 2;
- i := m div Power(2, Level + 1);
- with (UnitDrawTree.FormTree.ImageTree.Canvas) do
- begin
- CurrX := X - HALF_SIZE_NODE;
- CurrY := Y;
- //Rectangle(CurrX, CurrY, CurrX + SIZE_NODE, CurrY + SIZE_NODE);
- Ellipse(CurrX, CurrY, CurrX + SIZE_NODE, CurrY + SIZE_NODE);
- TextOut(CurrX + TEXT_INDENT, CurrY + TEXT_INDENT, IntToStr(Curr^.Data));
- CurrX := X;
- CurrY := Y + SIZE_NODE;
- MoveTo(CurrX, CurrY);
- if (IsLeft) and (Curr^.NextLeft <> nil) then
- begin
- LineTo(CurrX - i, CurrY + HALF_SIZE_NODE);
- end;
- if not(IsLeft) and (Curr^.NextLeft <> nil) then
- begin
- LineTo(CurrX - i, CurrY + HALF_SIZE_NODE);
- end;
- DrawNode(Curr^.NextLeft, (Level + 1), True, CurrX - i, CurrY + HALF_SIZE_NODE);
- MoveTo(CurrX, CurrY);
- if (Curr^.NextRight <> nil) then
- begin
- LineTo(CurrX + i, CurrY + HALF_SIZE_NODE);
- end;
- DrawNode(Curr^.NextRight, (Level + 1), false, CurrX + i, CurrY + HALF_SIZE_NODE);
- MoveTo(CurrX - 2 * i, CurrY - HALF_SIZE_NODE - SIZE_NODE);
- end;
- end;
- end;
- procedure TFormMain.ButtonDrawTreeClick(Sender: TObject);
- var
- Temp: TPointer;
- begin
- UnitDrawTree.FormTree.ImageTree.Canvas.Pen.Color := clWhite;
- UnitDrawTree.FormTree.ImageTree.Canvas.Brush.Color := clSkyBlue;
- UnitDrawTree.FormTree.ImageTree.Canvas.Font.Color := clWhite;
- UnitDrawTree.FormTree.ImageTree.Canvas.Font.Name := 'Century Gothic';
- UnitDrawTree.FormTree.ImageTree.Canvas.Font.Style := [fsBold];
- UnitDrawTree.FormTree.ImageTree.Canvas.Rectangle(0, 0, UnitDrawTree.FormTree.ImageTree.Width, UnitDrawTree.FormTree.ImageTree.Height);
- DrawNode(Head, 0, True, (UnitDrawTree.FormTree.ImageTree.Width div 2), HALF_SIZE_NODE);
- UnitDrawTree.FormTree.ShowModal();
- end;
- procedure TFormMain.ButtonFindAnswerClick(Sender: TObject);
- var
- Error: String;
- CountNode, CountLeaf: Integer;
- begin
- CountNode := 0;
- CountLeaf := 0;
- if Head <> nil then
- begin
- CountInTree(Head, CountNode, CountLeaf);
- UnitNodesAndLeaves.FormNodesAndLeaves.LabelNodes.Caption := IntToStr(CountNode);
- UnitNodesAndLeaves.FormNodesAndLeaves.LabelLeaves.Caption := IntToStr(CountLeaf);
- UnitNodesAndLeaves.FormNodesAndLeaves.ShowModal();
- UnitNodesAndLeaves.FormNodesAndLeaves.LabelNodes.Caption := '';
- UnitNodesAndLeaves.FormNodesAndLeaves.LabelLeaves.Caption := '';
- end
- else
- begin
- Error := 'Дерево пустое. ';
- UnitError.FormError.LabelError.Caption := 'Ошибка! ' + Error;
- UnitError.FormError.ShowModal();
- UnitError.FormError.LabelError.Caption := '';
- end;
- end;
- procedure TFormMain.ButtonTaskClick(Sender: TObject);
- begin
- UnitTask_5_2.FormTask.ShowModal();
- end;
- procedure TFormMain.EditChangeTreeChange(Sender: TObject);
- begin
- if (EditChangeTree.Text <> '') and (EditChangeTree.Text <> '-') then
- begin
- ButtonAddElement.Enabled := true;
- ButtonDeleteElement.Enabled := true;
- end
- else
- begin
- ButtonAddElement.Enabled := false;
- ButtonDeleteElement.Enabled := false;
- end;
- end;
- procedure TFormMain.EditChangeTreeKeyPress(Sender: TObject; var Key: Char);
- begin
- if not(Key in ['0'..'9', #8, '-']) then
- Key := #0;
- if (Length(EditChangeTree.Text) > 0) and (EditChangeTree.Text[1] = '-') then
- EditChangeTree.MaxLength := 3
- else
- EditChangeTree.MaxLength := 2;
- if (Length(EditChangeTree.Text) > 0) and (Key = '-') then
- Key := #0;
- if (Length(EditChangeTree.Text) = 1) and (EditChangeTree.Text[1] = '-') and (Key = '0') then
- Key := #0;
- if (Length(EditChangeTree.Text) = 1) and (EditChangeTree.Text[1] = '0') and not(Length(EditChangeTree.SelText) > 0) and not(Key = #8) then
- Key := #0;
- end;
- procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- begin
- If UnitExit.FormExit.ShowModal() = mrOk Then
- CanClose := True
- Else
- CanClose := False;
- end;
- procedure TFormMain.FormCreate(Sender: TObject);
- begin
- Head := nil;
- end;
- procedure TFormMain.NAboutClick(Sender: TObject);
- begin
- UnitAbout.FormAbout.ShowModal();
- end;
- procedure TFormMain.NInstructionClick(Sender: TObject);
- begin
- UnitInstruction_5_2.FormInstruction.ShowModal();
- end;
- procedure TFormMain.NOpenFileClick(Sender: TObject);
- var
- Path, Error, InputStr: String;
- IsCorrect, IsEmpty: Boolean;
- F: TextFile;
- Element: Integer;
- begin
- IsCorrect := true;
- Error := '';
- IsEmpty := true;
- if (OpenDialog.Execute) then
- begin
- Path := OpenDialog.FileName;
- AssignFile(F, Path);
- try
- Reset(F);
- try
- while not(EoF(F)) and (IsCorrect) do
- begin
- Readln(F, InputStr);
- if not(InputStr.IsEmpty) then
- begin
- try
- Element := StrToInt(InputStr);
- if (Element < MIN_DATA) or (Element > MAX_DATA) then
- begin
- IsCorrect := false;
- Error := 'Найден элемент, не входящий в допустимый диапазон. ';
- end
- else
- begin
- AddElement(Head, Element);
- IsEmpty := false;
- end;
- except
- IsCorrect := false;
- Error := 'Найден некорректный элемент списка. ';
- end;
- end;
- end;
- if (IsCorrect) then
- begin
- if (IsEmpty) then
- begin
- IsCorrect := false;
- Error := 'В файле не найдено элементов. ';
- end;
- end;
- finally
- CloseFile(F);
- end;
- except
- IsCorrect := false;
- Error := 'Ошибка при считывании данных из файла. ';
- end;
- end;
- if not(IsCorrect) then
- begin
- Head := nil;
- UnitError.FormError.LabelError.Caption := 'Ошибка! ' + Error;
- UnitError.FormError.ShowModal();
- UnitError.FormError.LabelError.Caption := '';
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement