unit basisalgorithmus01_unit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  Buttons, Grids;

type

  { TForm1 }

  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    Edit1: TEdit;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    GroupBox3: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Panel1: TPanel;
    StaticText1: TStaticText;
    StringGrid1: TStringGrid;
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private

  public

  end;

const
  size=30000;

var
  Form1: TForm1;
  valuearray:array[1..size]of Int64;
  searchcondition:Int64;
  exist:boolean;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.BitBtn2Click(Sender: TObject);

//startet die Suche im zufälligen Zahlenfeld in der angegebenen Dimension

var
  i:Int64;

begin
  BitBtn2.Enabled:=false;
  i:=0;
  exist:=false;
  searchcondition:=StrToInt(Edit1.Text);
  repeat
    inc(i);
    if searchcondition=valuearray[i]
    then
    begin
      exist:=true;
    end;
  until (exist=true)or(i=size);
  if exist=true
  then
  begin
    Label3.Caption:='JA';
  end
  else
  begin
    Label3.Caption:='NEIN';
  end
end;

procedure TForm1.BitBtn3Click(Sender: TObject);

//legt das zufällige Zahlenfeld in der angegebenen Dimension fest

var
  i:Int64;
  negativ:byte;

begin
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=false;
  BitBtn4.Enabled:=true;
  for i:=1 to size do
  begin
    valuearray[i]:=random(size+1);
    negativ:=random(2);
    if negativ=1
    then
    begin
      valuearray[i]:=valuearray[i]*(-1);
    end;
    StringGrid1.Cells[0,i]:=IntToStr(i);
    StringGrid1.Cells[1,i]:=IntToStr(valuearray[i]);
  end;
end;

procedure TForm1.BitBtn4Click(Sender: TObject);

var
  i:Int64;

begin
  Label3.Caption:='???';
  BitBtn3.Enabled:=true;
  for i:=1 to size do
  begin
    valuearray[i]:=0;
    StringGrid1.Cells[0,i]:=IntToStr(i);
    StringGrid1.Cells[1,i]:=IntToStr(valuearray[i]);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);

var
  i:Int64;

begin
  Randomize;
  StringGrid1.RowCount:=size+1;
  StringGrid1.Cells[0,0]:='Index';
  StringGrid1.Cells[1,0]:='Value';
  for i:=1 to size do
  begin
    valuearray[i]:=0;
    StringGrid1.Cells[0,i]:=IntToStr(i);
    StringGrid1.Cells[1,i]:=IntToStr(valuearray[i]);
  end;
end;

end.

