Загрузка данных


unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    RadioGroup1: TRadioGroup;
    Memo1: TMemo;
    Label1: TLabel;
    ComboBox1: TComboBox;
    Memo2: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;

    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure ComboBox1KeyPress(Sender: TObject; var Key: Char);
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

function GetPole(S: string; N: Integer): string;
var
  i, k: Integer;
  T: string;
begin
  Result := '';
  k := 1;
  T := '';

  for i := 1 to Length(S) do
  begin
    if S[i] = ';' then
    begin
      if k = N then
      begin
        Result := T;
        Exit;
      end;
      Inc(k);
      T := '';
    end
    else
      T := T + S[i];
  end;

  if k = N then
    Result := T;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  F: TextFile;
  S, Sport, Rost: string;
  R: Real;
begin
  Memo1.Clear;
  Memo2.Clear;

  if not FileExists('Sport.txt') then Exit;

  AssignFile(F, 'Sport.txt');
  Reset(F);

  while not Eof(F) do
  begin
    ReadLn(F, S);

    Sport := GetPole(S, 2);
    Rost := GetPole(S, 4);
    R := StrToFloat(Rost);

    case RadioGroup1.ItemIndex of
      0: Memo1.Lines.Add(S);
      1: if R > 2 then Memo1.Lines.Add(S);
      2: if R < 2 then Memo1.Lines.Add(S);
    end;

    if Sport = ComboBox1.Text then
      Memo2.Lines.Add(S);
  end;

  CloseFile(F);
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  Memo1.Lines.SaveToFile('otbor.txt');
  ShowMessage('Сохранено!');
end;

procedure TForm2.Button3Click(Sender: TObject);
var
  F: TextFile;
  S, MaxS: string;
  R, MaxR: Real;
begin
  Memo1.Clear;

  MaxR := -1;

  AssignFile(F, 'Sport.txt');
  Reset(F);

  while not Eof(F) do
  begin
    ReadLn(F, S);
    R := StrToFloat(GetPole(S, 4));

    if R > MaxR then
    begin
      MaxR := R;
      MaxS := S;
    end;
  end;

  CloseFile(F);

  Memo1.Lines.Add('Самый высокий:');
  Memo1.Lines.Add(MaxS);
end;

procedure TForm2.Button4Click(Sender: TObject);
begin
  Close;
end;

procedure TForm2.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if ComboBox1.Items.IndexOf(ComboBox1.Text) = -1 then
      ComboBox1.Items.Add(ComboBox1.Text);

    Key := #0;
  end;
end;

end.