Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Unit MainUnit;
- Interface
- Uses
- Winapi.Windows,
- Winapi.Messages,
- System.SysUtils,
- System.Variants,
- System.Classes,
- Vcl.Graphics,
- Vcl.Controls,
- Vcl.Forms,
- Vcl.Dialogs,
- Vcl.StdCtrls,
- Vcl.ComCtrls,
- Vcl.ExtCtrls,
- Vcl.Grids,
- Vcl.Menus,
- TreeUnit,
- BackUnit,
- DevInfUnit,
- ManualUnit,
- Vcl.ExtDlgs;
- Type
- TMainForm = Class(TForm)
- AddButton: TButton;
- NewNodeEdit: TEdit;
- MainPaintBox: TPaintBox;
- ScrollBox: TScrollBox;
- Timer: TTimer;
- FreeButton: TButton;
- StringGrid1: TStringGrid;
- MainMenu: TMainMenu;
- ManualButtonMenu: TMenuItem;
- DeveloperButtonMenu: TMenuItem;
- PopupMenu: TPopupMenu;
- TaskLabel: TLabel;
- ResButton: TButton;
- ResMemo: TMemo;
- SaveDialog1: TSaveDialog;
- FileButton: TMenuItem;
- SaveButton: TMenuItem;
- CloseButton: TMenuItem;
- OpenButton: TMenuItem;
- OpenTextFileDialog1: TOpenTextFileDialog;
- Procedure AddButtonClick(Sender: TObject);
- Procedure FreeButtonClick(Sender: TObject);
- Procedure FormCreate(Sender: TObject);
- Procedure MainPaintBoxPaint(Sender: TObject);
- Procedure NewNodeEditKeyPress(Sender: TObject; Var Key: Char);
- Procedure NewNodeEditChange(Sender: TObject);
- Procedure NewNodeEditKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
- Procedure AddElemToGrid(Grid: TStringGrid; Elem: Integer);
- Procedure ErrMessage();
- Procedure DeveloperButtonMenuClick(Sender: TObject);
- Procedure ManualButtonMenuClick(Sender: TObject);
- Procedure FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
- Procedure ResButtonClick(Sender: TObject);
- Procedure ExitButtonClick(Sender: TObject);
- Procedure SaveButtonClick(Sender: TObject);
- Procedure FileButtonClick(Sender: TObject);
- Procedure OpenButtonClick(Sender: TObject);
- Procedure LoadTreeFromFile(Tree: TTree; Const FileName: String; Sender: TObject);
- Procedure GetPath(Var Path: String);
- Procedure CloseButtonClick(Sender: TObject);
- Private
- { Private declarations }
- Public
- { Public declarations }
- End;
- Function CreateTree(): TTree; StdCall; External 'TreeLib.dll';
- Function Add(Var Node: TNodePointer; Elem: Integer): Boolean; StdCall; External 'TreeLib.dll';
- Function GetHigh(Node: TNodePointer): Integer; StdCall; External 'TreeLib.dll';
- Procedure DeleteTree(Var Node: TNodePointer); StdCall; External 'TreeLib.dll';
- Procedure CheckHeight(Node: TNodePointer; Var Fin: String); StdCall; External 'TreeLib.dll';
- Function GetNodeWithDifferentHeight(Node: TNodePointer): String; StdCall; External 'TreeLib.dll';
- Var
- MainForm: TMainForm;
- Tree: TTree;
- ChoosedNum: Integer;
- Node: TNodePointer;
- IsSaved: Boolean = False;
- Implementation
- {$R *.dfm}
- Procedure ClearGrid(Grid: TStringGrid);
- Var
- J, I: Integer;
- Begin
- Grid.Visible := False;
- For I := 0 To Grid.ColCount - 1 Do
- For J := 0 To Grid.RowCount - 1 Do
- Grid.Cells[I, J] := '';
- Grid.ColCount := 1;
- End;
- Procedure TMainForm.AddButtonClick(Sender: TObject);
- Var
- NewNodeData, MaxCountOfNode, TreeHigh: Integer;
- IsAdded: Boolean;
- Begin
- NewNodeData := StrToInt(NewNodeEdit.Text);
- NewNodeEdit.Text := '';
- If Add(Tree^.Next, NewNodeData) Then
- Begin
- AddElemToGrid(StringGrid1, NewNodeData);
- ChoosedNum := NewNodeData;
- End
- Else
- ErrMessage();
- If Tree^.Next <> Nil Then
- Begin
- FreeButton.Enabled := True;
- ResButton.Enabled := True;
- End;
- TreeHigh := GetHigh(Tree^.Next);
- MaxCountOfNode := Pow(2, TreeHigh - 1);
- MainPaintBox.Width := NODE_SIZE * MaxCountOfNode;
- MainPaintBox.Height := LAYER_SIZE * TreeHigh;
- MainPaintBox.Canvas.MoveTo(MainPaintBox.Width Div 2, NODE_SIZE Div 2);
- PrintTree(Tree^.Next, MainPaintBox.Canvas, 0, MainPaintBox.Width, 0, ChoosedNum);
- End;
- Procedure TMainForm.AddElemToGrid(Grid: TStringGrid; Elem: Integer);
- Begin
- With Grid Do
- Begin
- If Visible Then
- Begin
- ColCount := ColCount + 1;
- End
- Else
- Visible := True;
- Cells[ColCount - 1, 0] := IntToStr(ColCount) + '.';
- Cells[ColCount - 1, 1] := IntToStr(Elem);
- End;
- End;
- Procedure TMainForm.CloseButtonClick(Sender: TObject);
- Begin
- Close();
- End;
- Procedure TMainForm.GetPath(Var Path: String);
- Begin
- If OpenTextFileDialog1.Execute Then
- Begin
- Path := OpenTextFileDialog1.FileName;
- End
- Else
- Begin
- ShowMessage('Открытие файла отменено.');
- End;
- End;
- Procedure TMainForm.LoadTreeFromFile(Tree: TTree; Const FileName: String; Sender: TObject);
- Var
- FileStream: TStreamReader;
- Line: String;
- NodeData, MaxCountOfNode, TreeHigh: Integer;
- NodesAdded: Boolean;
- Begin
- FileStream := TStreamReader.Create(FileName);
- Try
- NodesAdded := False;
- While Not FileStream.EndOfStream Do
- Begin
- Line := FileStream.ReadLine;
- If TryStrToInt(Line, NodeData) Then
- Begin
- If Add(Tree^.Next, NodeData) Then
- Begin
- AddElemToGrid(TStringGrid(Sender), NodeData);
- NodesAdded := True;
- End;
- End;
- End;
- Finally
- FileStream.Free;
- End;
- If NodesAdded Then
- Begin
- FreeButton.Enabled := True;
- ResButton.Enabled := True;
- End;
- TreeHigh := GetHigh(Tree^.Next);
- MaxCountOfNode := Pow(2, TreeHigh - 1);
- MainPaintBox.Width := NODE_SIZE * MaxCountOfNode;
- MainPaintBox.Height := LAYER_SIZE * TreeHigh;
- MainPaintBox.Canvas.MoveTo(MainPaintBox.Width Div 2, NODE_SIZE Div 2);
- PrintTree(Tree^.Next, MainPaintBox.Canvas, 0, MainPaintBox.Width, 0, ChoosedNum);
- End;
- Procedure TMainForm.DeveloperButtonMenuClick(Sender: TObject);
- Var
- DeveloperForm: TDeveloperForm;
- Begin
- DeveloperForm := TDeveloperForm.Create(Self);
- DeveloperForm.ShowModal;
- DeveloperForm.Free;
- End;
- Procedure TMainForm.ErrMessage;
- Begin
- Application.MessageBox('Такой элемент уже существует', 'Ошибочка', MB_OK + MB_ICONASTERISK + MB_DEFBUTTON2)
- End;
- Procedure TMainForm.FormCreate(Sender: TObject);
- Begin
- Tree := CreateTree();
- TaskLabel.Caption := ('Вывести номера вершин, для которых ' + #13#10 + 'высота левого поддерева не равна высоте правого поддерева.');
- End;
- Procedure TMainForm.FreeButtonClick(Sender: TObject);
- Begin
- DeleteTree(Tree^.Next);
- FreeButton.Enabled := False;
- ResButton.Enabled := False;
- ResMemo.Lines.Clear;
- ClearGrid(StringGrid1);
- MainPaintBox.OnPaint(Sender);
- End;
- Procedure TMainForm.MainPaintBoxPaint(Sender: TObject);
- Begin
- With MainPaintBox.Canvas Do
- Begin
- Pen.Width := 3;
- Brush.Color := ClMenu;
- FillRect(MainPaintBox.ClientRect);
- End;
- MainPaintBox.Canvas.MoveTo(MainPaintBox.Width Div 2, NODE_SIZE Div 2);
- PrintTree(Tree^.Next, MainPaintBox.Canvas, 0, MainPaintBox.Width, 0, ChoosedNum);
- End;
- Procedure TMainForm.ManualButtonMenuClick(Sender: TObject);
- Var
- ManualForm: TManualForm;
- Begin
- ManualForm := TManualForm.Create(Self);
- ManualForm.ShowModal;
- ManualForm.Free;
- End;
- Procedure TMainForm.ExitButtonClick(Sender: TObject);
- Begin
- Close();
- End;
- Procedure TMainForm.OpenButtonClick(Sender: TObject);
- Var
- FileName: String;
- Begin
- GetPath(FileName);
- If FileName <> '' Then
- Begin
- If Tree^.Next <> Nil Then
- DeleteTree(Tree^.Next);
- ClearGrid(StringGrid1);
- LoadTreeFromFile(Tree, FileName, StringGrid1);
- End
- Else
- ShowMessage('Выберите файл.');
- End;
- Procedure TMainForm.NewNodeEditChange(Sender: TObject);
- Begin
- With Sender As TEdit Do
- If (Text <> '') And (Text <> '-') Then
- AddButton.Enabled := True
- Else
- AddButton.Enabled := False;
- End;
- Procedure TMainForm.NewNodeEditKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
- Begin
- If ((SsShift In Shift) Or (SsCtrl In Shift)) And Not((Key = VK_RIGHT) Or (Key = VK_LEFT)) Then
- Key := 0;
- If Key = VK_DELETE Then
- Key := 0;
- If (Key = VK_RETURN) And AddButton.Enabled Then
- AddButton.Click();
- End;
- Procedure TMainForm.FileButtonClick(Sender: TObject);
- Begin
- If ResMemo.Lines.Count = 0 Then
- Begin
- SaveButton.Enabled := False;
- End
- Else
- Begin
- SaveButton.Enabled := True;
- End;
- End;
- Procedure TMainForm.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
- Begin
- If SaveButton.Enabled And Not IsSaved Then
- Case Application.MessageBox('Сохранить данные перед выходом?', 'Выход', MB_YESNOCANCEL + MB_ICONQUESTION + MB_DEFBUTTON3) Of
- IDYES:
- Begin
- SaveButton.Click;
- CanClose := True;
- End;
- IDNO:
- CanClose := True;
- IDCANCEL:
- CanClose := False;
- End
- Else
- Case Application.MessageBox('Вы точно хотите выйти?', 'Выход', MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON2) Of
- IDYES:
- CanClose := True;
- IDNO:
- CanClose := False;
- End;
- End;
- Procedure TMainForm.NewNodeEditKeyPress(Sender: TObject; Var Key: Char);
- Begin
- With Sender As TEdit Do
- TotalKeyPress(Key, SelStart, SelLength, MIN_NODE, MAX_NODE, Text);
- End;
- Procedure TMainForm.ResButtonClick(Sender: TObject);
- Var
- NodesWithDifferentHeight, S: String;
- Begin
- NodesWithDifferentHeight := GetNodeWithDifferentHeight(Tree^.Next);
- If NodesWithDifferentHeight = '' Then
- S := ('Такие вершины отсутствуют')
- Else
- S := ('Вершины с разной высотой:' + NodesWithDifferentHeight);
- ResMemo.Lines.Clear;
- ResMemo.Lines.Add(S);
- IsSaved := True;
- End;
- Procedure TMainForm.SaveButtonClick(Sender: TObject);
- Begin
- If SaveDialog1.Execute Then
- Begin
- ResMemo.Lines.SaveToFile(SaveDialog1.FileName);
- ShowMessage('Сохранение прошло успешно.');
- End
- Else
- Begin
- ShowMessage('Сохранение отменено.');
- End;
- End;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement