unit UnMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TFormMain = class(TForm)
    OpenDialog: TOpenDialog;
    btOk: TButton;
    procedure FormCreate(Sender: TObject);
    procedure CloseForm;
    procedure btOkClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    edt: TEdit;
    lbl: TLabel;
    { Public declarations }
  end;

const code = 777;

var
  FormMain: TFormMain;
  StListQuest, StListAnswer: TStringList;
  MyEdits: array of TEdit;
  rec_question: integer;
  FIO: string;

implementation

{$R *.dfm}

function decode(s: string; kod: integer): string;
var
 i: integer;
begin
for i:= 1 to length(s) do s[i]:= chr(ord(s[i]) xor kod);
 result:= s;
end;

procedure TFormMain.FormCreate(Sender: TObject);
var
 i, pos: integer;
begin
if not InputQuery('Форма заполнения ФИО', 'Пожалуйста, укажите своё имя', FIO) then
 begin
  CloseForm;
  exit;
 end;

if not OpenDialog.Execute then
begin
  CloseForm;
  exit;
 end;
 
StListQuest:= TStringList.Create;
StListQuest.LoadFromFile(OpenDialog.FileName);
rec_question:= StListQuest.Count;
pos:= 10;
SetLength(MyEdits, rec_question);
 for i:= 0 to rec_question-1 do
  begin
  lbl:= TLabel.Create(Self);
  lbl.Parent:= FormMain;
  lbl.Caption:= StListQuest.Strings[i];
  lbl.Top:= pos;

  MyEdits[i]:= TEdit.Create(Self);
   with MyEdits[i] do
    begin
     Parent:= FormMain;
     Top:= pos;
     Left:= 150;
     Width:= 100;
    end;
  inc(pos, 20);
  end;
btOk.Width:= MyEdits[0].Left + MyEdits[0].Width;
btOk.Top:= pos + 20;
end;

procedure TFormMain.CloseForm;
begin
 Application.ShowMainForm := False;
 Application.Terminate;
end;

procedure TFormMain.btOkClick(Sender: TObject);
var
 i: integer;
begin
StListAnswer:= TStringList.Create;
for i:= 0 to rec_question-1 do
 begin
  StListAnswer.Add(decode(MyEdits[i].Text, code));
 end;
StListAnswer.SaveToFile(ExtractFilePath(OpenDialog.FileName) + '\answers_' + FIO + '.txt');
ShowMessage('Done!');
end;

procedure TFormMain.FormClose(Sender: TObject; var Action: TCloseAction);
var
 i: integer;
begin
 for i:= 0 to rec_question-1 do
  begin
   MyEdits[i].Free;
  end;
StListQuest.Free;
StListAnswer.Free;
end;

end.
