Zadatak 1
Na formi su data dva stringrida i jedno dugme. Klik na prvo dugme popunjava 10 slucajno izabranih celija prvog stringgrida sa '*'.
Levi klik na zuti stringgrid daje broj mina oko te celije u belom stringgridu ili poruku
'Miiiina' ukoliko je u toj celiji '*'. Desni klik postavlja
'F' na celiju tako da se levi klik na nju ne racuna dok se 'F' ne skloni ponovnim desnim klikom.
Resenje:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type niz=array [1..100] of char;
type
TForm1 = class(TForm)
SGPostavi: TStringGrid;
SGPogodi: TStringGrid;
BPostavi: TButton;
procedure BPostaviClick(Sender: TObject);
procedure SGPogodiSelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure SGPogodiMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
function proveri(acol,arow:integer):integer;
procedure Zamena(A,B : Integer);
procedure Brisi(Grid:TStringGrid);
public
{ Public declarations }
end;

var
Form1: TForm1;
niz1:niz;
implementation

{$R *.dfm}

procedure TForm1.BPostaviClick(Sender: TObject);
var i,j:integer;
begin
randomize;
Brisi(SGPogodi);
for i:=1 to 100 do
if i mod 10 =0 then niz1[i]:='*' else niz1[i]:=' ';
for i:=1 to 100 do
zamena(i,random(i)+1); // Mesa indekse niza sa minama
for i:=1 to 10 do
for j:=1 to 10 do
SGPostavi.Cells[i-1,j-1]:=niz1[(i-1)*10+j];
end;

procedure TForm1.Zamena(A,B : Integer);
var
Tmp : char;
begin
Tmp := niz1[A];
niz1[A] := niz1[B];
niz1[B] := Tmp;
end;

procedure TForm1.SGPogodiSelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
begin
if SGpostavi.Cells[acol,arow]<>'*' then
SGPogodi.Cells[acol,arow]:=inttostr(proveri(ACol,Arow))
else
if SGPogodi.Cells[acol,arow]<>'F' then
begin
SGPogodi.Cells[acol,arow]:='*';
showmessage('Miiiina');
end;

end;

function TForm1.proveri(acol,arow:integer):integer;
var bmo:integer;
begin
bmo:=0;
with SGpostavi do
begin
if (acol<10) and (arow<10) then
if Cells[acol+1,arow+1]='*' then bmo:=bmo+1;
if (acol>0) and (arow<10) then
if Cells[acol-1,arow+1]='*' then bmo:=bmo+1;
if (acol<10) and (arow>0) then
if Cells[acol+1,arow-1]='*' then bmo:=bmo+1;
if (acol>0) and (arow>0) then
if Cells[acol-1,arow-1]='*' then bmo:=bmo+1;
if arow<10 then
if Cells[acol,arow+1]='*' then bmo:=bmo+1;
if acol<10 then
if Cells[acol+1,arow]='*' then bmo:=bmo+1;
if arow>0 then
if Cells[acol,arow-1]='*' then bmo:=bmo+1;
if acol>0 then
if Cells[acol-1,arow]='*' then bmo:=bmo+1;
end;
proveri:=bmo;
end;

procedure TForm1.Brisi(Grid:TStringGrid);
var
i: Integer;
begin
i := 0;
while i < Grid.RowCount do
begin
Grid.Rows[i].Clear;
Inc(i);
end;
end;

procedure TForm1.SGPogodiMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
Col, Row: Longint;
begin
SGPogodi.MouseToCell(X, Y, Col, Row);
// Postavi zastavicu na polje desnim klikom
if button=mbright then
begin
if SGPogodi.Cells[col,row]='F' then SGPogodi.Cells[col,row]:=''
else SGPogodi.Cells[col,row]:='F'
end;
end;

end.
Zadatak 2
Na formi su dve Memo kontrole,Stringgrid i jedno Dugme. Klik na dugme postavlja stringgrid prema broju linija
u Memo kontrolama a tekst se prebacuje u fiksni red i kolonu stringgrida.Na SetEditText dogadjaj izracunava se prosek po predmetima.
 
unit Ocene_o;

interface

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

type
TobrOcene = class(TForm)
mreOcene: TStringGrid;
vltVrste: TMemo;
vltKolone: TMemo;
BKreiraj: TButton;
procedure mreOceneSelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure mreOceneSetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: string);
procedure BKreirajClick(Sender: TObject);
private
procedure kreiraj;
public
{ Public declarations }
end;

var
obrOcene: TobrOcene;

implementation

{$R *.DFM}

procedure TobrOcene.kreiraj;
begin
with mreOcene do
begin
ColCount := vltKolone.Lines.Count;
RowCount := vltVrste.Lines.Count;
Rows[0] := vltKolone.Lines;
Cols[0] := vltVrste.Lines;
ColWidths[0] := vltVrste.Width;
ColWidths[ColCount-1] := vltKolone.Width;
Height := RowCount * (DefaultRowHeight + 1) + 3;
Width := ColWidths[0] + ColWidths[ColCount-1] +
(ColCount - 2) * (DefaultColWidth + 1) + 5;
end
end;
procedure TobrOcene.mreOceneSelectCell(Sender: TObject;
ACol, ARow: Integer; var CanSelect: Boolean);
begin
CanSelect := ACol <> mreOcene.ColCount - 1
end;

procedure TobrOcene.mreOceneSetEditText(Sender: TObject;
ACol, ARow: Integer; const Value: string);
var
Oc, Br, ZbBr, ZbOcBr: Integer;
begin
ZbBr := 0;
ZbOcBr := 0;
with mreOcene do
begin
for Oc := 1 to ColCount-2 do
if Cells[Oc,ARow] <> '' then
begin
Br := StrToInt (Cells[Oc,ARow]);
ZbBr := ZbBr + Br;
ZbOcBr := ZbOcBr + Oc * Br;
end;
if ZbBr <> 0 then
Cells[ColCount-1,ARow]:=Format('%5.2f',[ZbOcBr/ZbBr])
else
Cells[ColCount-1,ARow]:=''
end
end;

procedure TobrOcene.BKreirajClick(Sender: TObject);
begin
Kreiraj;
end;

end.
Zadatak 3
Program treba da izlista sve delioce zadatog broja.
Resenje:
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
Label1: TLabel;
CheckBox1: TCheckBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var i,j,br:integer; s:string;
begin
s:=''; br:=0;
val(edit1.text,i,j);
if j<> 0 then Showmessage('Greska na poziciji '+inttostr(j))
else
begin
for j:=i downto 1 do
if i mod j =0 then
begin
if checkBox1.Checked then s:=inttostr(j)+' '+s
else s:=s+inttostr(j)+' ';
br:=br+1
end;
Memo1.text:=s;
Label1.Caption:='Ukupno:'+inttostr(br)
end;
end;

end.
Zadatak 4
Program za odredjivanje zbira celija u odredjenim zonama stringgrida(na glavnoj dijagonali, iznad i ispod i na sporednoj).
Resenje:
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Ebrkolired: TEdit;
BPostavi: TButton;
StringGrid1: TStringGrid;
BRacunaj: TButton;
EOd: TEdit;
EDo: TEdit;
Memo1: TMemo;
CBIznad: TCheckBox;
CBIspod: TCheckBox;
CBGlavna: TCheckBox;
CBSporedna: TCheckBox;
procedure BPostaviClick(Sender: TObject);
procedure BRacunajClick(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure EbrkoliredKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BPostaviClick(Sender: TObject);
var brkolired,i,j,pv,kv:integer;
begin
randomize;
val(ebrkolired.text,brkolired,i);
if i<>0 then ShowMessage('Greska na poziciji '+ inttostr(i));
val(eOd.text,pv,i);
if i<>0 then ShowMessage('Greska na poziciji '+ inttostr(i));
val(eDo.text,kv,i);
if i<>0 then ShowMessage('Greska na poziciji '+ inttostr(i));
stringgrid1.ColCount:=brkolired;
stringgrid1.rowCount:=brkolired;
for i:=0 to brkolired-1 do
for j:=0 to brkolired-1 do
stringgrid1.Cells[i,j]:=inttostr(randomrange(pv,kv));
end;

procedure TForm1.BRacunajClick(Sender: TObject);
var sumaGD, sumaIZGD, sumaISGD,sumaSP,i,j,kr:integer;
s:string;
begin
memo1.Clear;
sumaSP:=0;sumaIZGD:=0; sumaGD:=0;sumaISGD:=0; s:='';
kr:=stringgrid1.colcount;
for i:=0 to kr-1 do
for j:=0 to kr-1 do
begin
if i>j then sumaIZGD:=sumaIZGD+ strtoint( stringgrid1.Cells[i,j]);
if i=j then sumaGD:=sumaGD+ strtoint( stringgrid1.Cells[i,j]);
if i<j then sumaISGD:=sumaISGD+ strtoint( stringgrid1.Cells[i,j]);
if i+j=kr-1 then sumaSP:=sumaSP+ strtoint( stringgrid1.Cells[i,j]);
end;
if CBIznad.checked then s:=s+'Suma Iznad Je:'+inttostr(sumaIZGD)+#13#10;
if CBIspod.Checked then s:=s+'Suma Ispod Je:'+inttostr(sumaISGD)+#13#10;
if CBGlavna.Checked then s:=s+'Suma Na GD Je:'+inttostr(sumaGD)+#13#10;
if CBSporedna.Checked then s:=s+'Suma Na SD Je:'+inttostr(sumaSP)+#13#10;
Memo1.Text:=s;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var Boja:TColor;
S:String;
begin
S := StringGrid1.Cells[ACol, ARow];
if Acol>Arow then Boja:=clYellow;
if Acol=Arow then Boja:=clGreen;
if Acol<Arow then Boja:=clPurple;
StringGrid1.Canvas.Brush.Color := Boja;
StringGrid1.Canvas.FillRect(Rect);
StringGrid1.Canvas.TextOut(Rect.Left + 5, Rect.Top + 5, S);
end;

procedure TForm1.EbrkoliredKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key=vk_return then Bpostavi.click;
end;

end.
Zadatak 5
Program racuna maksimalni, minimalni element stringgrida kao i sumu parnih i broj negativnih elemenata.
Resenje:
unit Unit1;

interface

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

type
TForm1 = class(TForm)
EBkol: TEdit;
EBred: TEdit;
SG1: TStringGrid;
RadioGroup1: TRadioGroup;
Bpostavi: TButton;
Bradi: TButton;
ERez: TEdit;
procedure BpostaviClick(Sender: TObject);
procedure BradiClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BpostaviClick(Sender: TObject);
var bk,br,i,j:integer;
begin
randomize;
bk:=strtoint(Ebkol.Text);
br:=strtoint(Ebred.Text);
SG1.ColCount:=bk;
SG1.RowCount:=br;
for i:=0 to bk-1 do
SG1.Cells[i,0]:=inttostr(i);
for i:=1 to br-1 do
SG1.Cells[0,i]:=inttostr(i);
for i:=1 to bk-1 do
for j:=1 to br-1 do
SG1.Cells[i,j]:=inttostr(randomrange(-random(20),random(20)));

end;

procedure TForm1.BradiClick(Sender: TObject);
var m,mi,sp,bn,i,j:integer;
begin
m:= strtoint(SG1.Cells[1,1]);mi:=m;
sp:=0;
bn:=0;
for i:=1 to sg1.colcount-1 do
for j:=1 to sg1.RowCount-1 do
case radiogroup1.ItemIndex of
0:if m<strtoint(SG1.Cells[i,j]) then m:=strtoint(SG1.Cells[i,j]);
1:if mi>strtoint(SG1.Cells[i,j]) then mi:=strtoint(SG1.Cells[i,j]);
2:if strtoint(SG1.Cells[i,j]) mod 2=0 then sp:=sp+strtoint(SG1.Cells[i,j]);
3:if strtoint(SG1.Cells[i,j])<0 then bn:=bn+1;
end;
case radiogroup1.ItemIndex of
0:eRez.Text:='Maksimum je: '+inttostr(m);
1:eRez.Text:='Minimum je: '+inttostr(mi);
2:eRez.Text:='Suma Parnih je: '+inttostr(sp);
3:eRez.Text:='Broj Negativnih je: '+inttostr(bn);
end;
end;

end.