unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    BitBtn1: TBitBtn;
    SaveDialog1: TSaveDialog;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  Type
    TStudent=record
      FIO:string[50];
      Adr:string[50];
      otc:array[1..3] of word;
      sball:extended;
  end;
  var
    Fz:file of TStudent;
    Ft:TextFile;
    Stud:array[1..100] of TStudent;
    nzap:integer;
    FileNameZ, FileNameT:string;
var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Clear; Edit2.Clear; Edit3.Clear; Edit4.Clear; Edit5.Clear; Memo1.Clear;
Button1.Hide;
nzap:=0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  nzap:=nzap+1;
  with stud[nzap] do
  begin
    FIO:=Edit1.Text;
    Adr:=Edit2.Text;
    otc[1]:=StrToInt(Edit3.Text);
    otc[2]:=StrToInt(Edit4.Text);
    otc[3]:=StrToInt(Edit5.Text);
    sball:=(otc[1]+otc[2]+otc[3])/3;
    Memo1.Lines.Add(IntToStr(nzap)+' '+FIO+' ('+Adr+'): '+IntToStr(otc[1])+', '+IntToStr(otc[2])+', '+IntToStr(otc[3]));
  end;
  Write(Fz,Stud[nzap]);
  Edit1.Clear; Edit2.Clear; Edit3.Clear; Edit4.Clear; Edit5.Clear;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  OpenDialog1.Title:='Создать новый файл';
  if OpenDialog1.Execute then
  begin
    FileNameZ:=OpenDialog1.FileName;
    AssignFile(Fz,FileNameZ);
    Rewrite(Fz);
  end;
  Button1.Show;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    FileNameZ:=OpenDialog1.FileName;
    AssignFile(Fz,FileNameZ);
    Reset(Fz);
  end;
  nzap:=0;
  while not Eof(Fz) do
  begin
    nzap:=nzap+1;
    Read(Fz,stud[nzap]);
    with stud[nzap] do
      Memo1.Lines.Add(IntToStr(nzap)+' '+FIO+' ('+Adr+'): '+IntToStr(otc[1])+', '+IntToStr(otc[2])+', '+IntToStr(otc[3]));
  end;
  Button1.Show;
end;

procedure TForm1.Button4Click(Sender: TObject);
var pz,k:integer;
    i,j:word;
    st:TStudent;


begin
  memo1.Clear;
  for i:=1 to nzap-1 do
    for j:=i+1 to nzap do
      if ord(Stud[i].Fio[1]) > ord(Stud[j].Fio[1]) then
      begin
        st:=Stud[i];
        Stud[i]:=Stud[j];
        Stud[j]:=st;
      end;
  k:=0;
  for i:=1 to nzap do
  begin
    pz:=pos('Минск',Stud[i].Adr);
    if pz>0 {and Stud[i].sball > 4.5}then
    begin
      k:=k+1;
      with stud[i] do
      Memo1.Lines.Add(IntToStr(k)+' '+FIO+' ('+Adr+'): '+FloatToStrf(sball,fffixed,4,2));
    end;
  end;

end;

procedure TForm1.Button5Click(Sender: TObject);
var i:word;
begin
  if SaveDialog1.Execute then
  begin
    FileNameT:=SaveDialog1.FileName;
    AssignFile(Ft,FileNameT);
    Rewrite(Ft);
  end;
  for i:=1 to nzap do
    with stud[i] do Writeln(Ft,i:4,'. ',fio,sball:8:2);
    CloseFile(Ft);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
CloseFile(Fz);
end;

end.
