Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- implementation
- {$R *.dfm}
- uses UnitAbout, UnitError, UnitExit, UnitTask_5_1, UnitInstruction_5_1;
- type
- TPointer = ^TElement;
- TElement = record
- Data: Integer;
- Next: TPointer;
- end;
- procedure AddElementInList(var Prev: TPointer; Data: Integer); stdcall; external 'Lab_5_1_Lib';
- function ChangeList(Start: TPointer): TPointer; stdcall; external 'Lab_5_1_Lib';
- const
- MAX_VALUE = 9999;
- MIN_VALUE = -9999;
- MIN_SIZE = 1;
- var
- Start: TPointer;
- IsEmpty: Boolean;
- procedure TFormMain.ButtonAddElementClick(Sender: TObject);
- var
- Data, I: Integer;
- IsCorrect: Boolean;
- Error: String;
- begin
- IsCorrect := true;
- try
- Data := StrToInt(EditAddNewElement.Text);
- if (Data > MAX_VALUE) or (Data < MIN_VALUE) then
- begin
- IsCorrect := false;
- Error := 'Значение элемента не входит в допустимый диапазон. ';
- end;
- except
- IsCorrect := false;
- Error := 'Некорректное значение элемента списка. ';
- end;
- if (IsCorrect) then
- begin
- EditAddNewElement.Text := '';
- I := List.ColCount;
- List.ColCount := List.ColCount + 1;
- List.Cells[I, 0] := '№' + IntToStr(I);
- List.Cells[I, 1] := IntToStr(Data);
- ButtonMakeNewList.Enabled := true;
- FormMain.Height := 299;
- NSaveFile.Enabled := false;
- 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;
- Number, Index, I: Integer;
- Error: String;
- begin
- IsCorrect := true;
- try
- Number := StrToInt(EditDeleteElement.Text);
- if (Number > List.ColCount - 1) or (Number < MIN_SIZE) then
- begin
- IsCorrect := false;
- Error := 'Не найдено элемента с таким номером. ';
- end;
- if (List.ColCount = 1) then
- begin
- IsCorrect := false;
- Error := 'В списке нет элементов для удаления. ';
- end;
- except
- IsCorrect := false;
- Error := 'Некорректное значение номера элемента списка. ';
- end;
- if (IsCorrect) then
- begin
- EditDeleteElement.Text := '';
- Index := Number + 1;
- for I := Number to List.ColCount - 2 do
- begin
- List.Cells[I, 0] := '№' + IntToStr(I);
- List.Cells[I, 1] := List.Cells[I + 1, 1];
- end;
- List.ColCount := List.ColCount - 1;
- FormMain.Height := 299;
- NSaveFile.Enabled := false;
- end
- else
- begin
- UnitError.FormError.LabelError.Caption := 'Ошибка! ' + Error;
- UnitError.FormError.ShowModal();
- UnitError.FormError.LabelError.Caption := '';
- end;
- end;
- procedure TFormMain.ButtonMakeNewListClick(Sender: TObject);
- var
- Temp, NewStart: TPointer;
- IsCorrect: Boolean;
- Error: String;
- Element, I: Integer;
- begin
- IsCorrect := true;
- I := 1;
- New(Temp);
- New(NewStart);
- Temp := Start;
- if (List.ColCount = 1) then
- begin
- IsCorrect := false;
- Error := 'Недостаточно элементов в списке. ';
- end;
- while (IsCorrect) and (I < List.ColCount) do
- begin
- try
- Element := StrToInt(List.Cells[I, 1]);
- AddElementInList(Temp, Element);
- Temp := Temp^.Next;
- except
- IsCorrect := false;
- Error := 'Ошибка при добавлении элемента №' + IntToStr(I) + ' в список';
- end;
- Inc(I);
- end;
- if (IsCorrect) then
- begin
- NewList.ColCount := 1;
- NewStart := ChangeList(Start);
- Temp := NewStart^.Next;
- I := 0;
- while (Temp <> nil) do
- begin
- NewList.Cells[I, 0] := IntToStr(Temp^.Data);
- NewList.ColCount := NewList.ColCount + 1;
- Temp := Temp^.Next;
- Inc(I);
- end;
- NewList.ColCount := NewList.ColCount - 1;
- FormMain.Height := 357;
- NSaveFile.Enabled := true;
- end
- else
- begin
- UnitError.FormError.LabelError.Caption := Error;
- UnitError.FormError.ShowModal();
- UnitError.FormError.LabelError.Caption := '';
- end;
- end;
- procedure TFormMain.ButtonShowTaskClick(Sender: TObject);
- begin
- UnitTask_5_1.FormTask.ShowModal();
- end;
- procedure TFormMain.EditAddNewElementChange(Sender: TObject);
- begin
- if (EditAddNewElement.Text = '') or (EditAddNewElement.Text = '-') then
- ButtonAddElement.Enabled := false
- else
- ButtonAddElement.Enabled := true;
- end;
- procedure TFormMain.EditAddNewElementKeyPress(Sender: TObject; var Key: Char);
- begin
- if not(Key in ['0'..'9', '-', #8, #13]) then
- Key := #0;
- if (Length(EditAddNewElement.Text) > 0) and (EditAddNewElement.Text[1] = '-') then
- EditAddNewElement.MaxLength := 5
- else
- EditAddNewElement.MaxLength := 4;
- if (Length(EditAddNewElement.Text) > 0) and (EditAddNewElement.Text[1] = '0') and not(Key in [#8, #13]) and not(Length(EditAddNewElement.SelText) > 0) then
- Key := #0;
- if (Length(EditAddNewElement.Text) > 0) and (Key = '-') and not(Length(EditAddNewElement.SelText) > 0) then
- Key := #0;
- if (Length(EditAddNewElement.Text) = 1) and (EditAddNewElement.Text[1] = '-') and (Key = '0') then
- Key := #0;
- if (Length(EditAddNewElement.Text) > 0) and (Key = #13) then
- ButtonAddElementClick(Sender);
- end;
- procedure TFormMain.EditDeleteElementChange(Sender: TObject);
- begin
- if (EditDeleteElement.Text = '') or (EditDeleteElement.Text = '-') then
- ButtonDeleteElement.Enabled := false
- else
- ButtonDeleteElement.Enabled := true;
- end;
- procedure TFormMain.EditDeleteElementKeyPress(Sender: TObject; var Key: Char);
- begin
- if not(Key in ['0'..'9', #8, #13]) then
- Key := #0;
- if (Length(EditDeleteElement.Text) > 0) and (EditDeleteElement.Text[1] = '0') and not(Key in [#8, #13]) and not(Length(EditDeleteElement.SelText) > 0) then
- Key := #0;
- if (Length(EditDeleteElement.Text) > 0) and (Key = #13) then
- ButtonDeleteElementClick(Sender);
- end;
- procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
- var
- Res: Integer;
- begin
- Res := UnitExit.FormExit.ShowModal();
- if Res = mrOk then
- CanClose := true
- else
- CanClose := false;
- end;
- procedure TFormMain.FormCreate(Sender: TObject);
- begin
- New(Start);
- Start.Next := nil;
- List.ColWidths[0] := 80;
- List.RowHeights[0] := 20;
- List.Cells[0, 0] := '№ элемента';
- List.Cells[0, 1] := 'Элемент';
- FormMain.Height := 299;
- end;
- procedure TFormMain.NAboutClick(Sender: TObject);
- begin
- UnitAbout.FormAbout.ShowModal();
- end;
- procedure TFormMain.NInstructionClick(Sender: TObject);
- begin
- UnitInstruction_5_1.FormInstruction.ShowModal();
- end;
- procedure TFormMain.NOpenFileClick(Sender: TObject);
- var
- Path, Error, InputStr: String;
- IsCorrect: Boolean;
- F: TextFile;
- Element, I: Integer;
- begin
- IsCorrect := true;
- Error := '';
- if (OpenDialog.Execute) then
- begin
- List.ColCount := 1;
- 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_VALUE) or (Element > MAX_VALUE) then
- begin
- IsCorrect := false;
- Error := 'Найден элемент, не входящий в допустимый диапазон. ';
- end
- else
- begin
- I := List.ColCount;
- List.ColCount := List.ColCount + 1;
- List.Cells[I, 0] := '№' + IntToStr(I);
- List.Cells[I, 1] := IntToStr(Element);
- end;
- except
- IsCorrect := false;
- Error := 'Найден некорректный элемент списка. ';
- end;
- end;
- end;
- if (IsCorrect) then
- begin
- if (List.ColCount = 1) then
- begin
- IsCorrect := false;
- Error := 'В файле не найдено элементов. ';
- ButtonMakeNewList.Enabled := false;
- end
- else
- begin
- ButtonMakeNewList.Enabled := true;
- FormMain.Height := 299;
- NSaveFile.Enabled := false;
- end;
- end;
- finally
- CloseFile(F);
- end;
- except
- IsCorrect := false;
- Error := 'Ошибка при считывании данных из файла. ';
- end;
- end;
- if not(IsCorrect) then
- begin
- List.ColCount := 1;
- UnitError.FormError.LabelError.Caption := 'Ошибка! ' + Error;
- UnitError.FormError.ShowModal();
- UnitError.FormError.LabelError.Caption := '';
- end;
- end;
- procedure TFormMain.NSaveFileClick(Sender: TObject);
- var
- IsCorrect: Boolean;
- Error, Path: String;
- I: Integer;
- F: TextFile;
- begin
- IsCorrect := true;
- Error := '';
- if (SaveDialog.Execute) then
- begin
- Path := SaveDialog.FileName;
- AssignFile(F, Path);
- try
- Rewrite(F);
- I := 0;
- try
- Writeln(F, 'Полученный список: ');
- while (I < NewList.ColCount) do
- begin
- Write(F, NewList.Cells[I, 0], ' ');
- Inc(I);
- end;
- finally
- CloseFile(F);
- end;
- except
- IsCorrect := false;
- Error := 'Оишбка открытия файла для записи. ';
- end;
- end;
- if not(IsCorrect) then
- begin
- UnitError.FormError.LabelError.Caption := 'Ошибка! ' + Error;
- UnitError.FormError.ShowModal();
- UnitError.FormError.LabelError.Caption := ''
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement