Delphi

Stian Søiland, IT 111, øvingsgruppe 15 (stud.ass. Kari Alvheim)

Delphi - Øving 4 - Oppgave 3

Oppgave 3
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    Label1: TLabel;
    StringGrid2: TStringGrid;
    StringGrid3: TStringGrid;
    Label2: TLabel;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
 const
  AntTermer = 6;
  AntDokument = 5;
  AntTermpar = 15;
 type
  Doktermmatrisetype = array[1..AntTermer,1..AntDokument] of integer;
  Similaritetsmatrisetype = array[1..AntTermer,1..AntTermer] of real;
  Termparmatrisetype = array[1..AntTermpar,1..2] of integer;
  Matchmatrisetype = array[1..AntTermpar] of real;
 var
  Doktermmatrise: Doktermmatrisetype;
  Similaritetsmatrise: Similaritetsmatrisetype;
  Termparmatrise: Termparmatrisetype;
  Matchmatrise: Matchmatrisetype;
  i,j,k: integer;

 function sim(ord1,ord2:integer;
                matrise:Doktermmatrisetype):real;
  var
   i,kvadrat1,kvadrat2,produkt:integer;
  begin
   // initiering
   kvadrat1 := 0;
   kvadrat2 := 0;
   produkt := 0;

   // summerer
   for i:=1 to AntDokument do
    begin
     kvadrat1 := kvadrat1 + (matrise[ord1,i] * matrise[ord1,i]);
     kvadrat2 := kvadrat2 + (matrise[ord2,i] * matrise[ord2,i]);
     produkt := produkt + (matrise[ord1,i] * matrise[ord2,i]);
    end;

   // formel
   sim := produkt  (kvadrat1+kvadrat2-produkt);

  end;

 procedure initiering(var Doktermmatrise:Doktermmatrisetype);
  begin
    // initering og innstilling av dingser
   Doktermmatrise[1,1]:=1;
   Doktermmatrise[1,2]:=1;
   Doktermmatrise[1,3]:=1;
   Doktermmatrise[1,4]:=0;
   Doktermmatrise[1,5]:=1;
   Doktermmatrise[2,1]:=1;
   Doktermmatrise[2,2]:=1;
   Doktermmatrise[2,3]:=1;
   Doktermmatrise[2,4]:=1;
   Doktermmatrise[2,5]:=1;
   Doktermmatrise[3,1]:=1;
   Doktermmatrise[3,2]:=0;
   Doktermmatrise[3,3]:=1;
   Doktermmatrise[3,4]:=1;
   Doktermmatrise[3,5]:=0;
   Doktermmatrise[4,1]:=1;
   Doktermmatrise[4,2]:=1;
   Doktermmatrise[4,3]:=1;
   Doktermmatrise[4,4]:=0;
   Doktermmatrise[4,5]:=1;
   Doktermmatrise[5,1]:=1;
   Doktermmatrise[5,2]:=1;
   Doktermmatrise[5,3]:=1;
   Doktermmatrise[5,4]:=0;
   Doktermmatrise[5,5]:=1;
   Doktermmatrise[6,1]:=0;
   Doktermmatrise[6,2]:=1;
   Doktermmatrise[6,3]:=0;
   Doktermmatrise[6,4]:=0;
   Doktermmatrise[6,5]:=1;
  end;

 procedure sortering(var matrise1:Matchmatrisetype;
                     var matrise2:Termparmatrisetype);
  var i,j,pos:integer;
      funnet:boolean;
      temp1:real;
      temp2,temp3:integer;
  begin
   matrise2[1,1] := matrise2[2,1];
   matrise2[2,1] := matrise2[3,1];
   matrise2[1,2] := matrise2[7,2];
   for i:=low(matrise1) to high(matrise1) do
    begin
     funnet := false;
     j := low(matrise1)-1;
     while not funnet do
     begin
      j := j+1;
      if matrise1[i] >= matrise1[j] then
       begin
        funnet := true;
        pos := j;
       end;
     end;
    // flytt elementer nedover
    temp1 := matrise1[i];
    temp2 := matrise2[i,1];
    temp3 := matrise2[i,2];
    for j:=i downto pos do
     begin
      matrise1[j] := matrise1[j-1];
      matrise2[j,1] := matrise2[j-1,1];
      matrise2[j,2] := matrise2[j-1,2];
     end;
    matrise1[pos] := temp1;
    matrise2[pos,1] := temp2;
    matrise2[pos,2] := temp3;
    end;
  end;

 begin
  initiering(Doktermmatrise);
  for i:= 1 to AntTermer do
   begin
    StringGrid1.Cells[0,i] := 't' + IntToStr(i);
    StringGrid1.Cells[i,0] := 't' + IntToStr(i);
   end;
  for i:=1 to AntTermer do
   for j:=1 to AntTermer do
   begin
    Similaritetsmatrise[i,j] := sim(i,j,Doktermmatrise);
    StringGrid1.Cells[i,j] := FloatToStr(sim(i,j,Doktermmatrise));
   end;
  k := 0;
  for i:=1 to AntTermer-1 do
   begin
    for j:=i+1 to AntTermer do
     begin
      k := k+1;
      Termparmatrise[k,1] := i;
      Termparmatrise[k,2] := j;
      Matchmatrise[k] := Similaritetsmatrise[i,j];
     end;
    end;
  for i:=1 to AntTermpar do
   begin
   // utskrift usortert
    StringGrid2.Cells[0,i] := 't' + IntToStr(Termparmatrise[i,1]);
    StringGrid2.Cells[1,i] := 't' + IntToStr(Termparmatrise[i,2]);
    StringGrid2.Cells[2,i] := Formatfloat('0.00',Matchmatrise[i]);
   end;
  sortering(Matchmatrise,Termparmatrise);
  for i:=1 to AntTermpar do
   begin
   // utskrift sortert
    StringGrid3.Cells[0,i] := 't' + IntToStr(Termparmatrise[i,1]);
    StringGrid3.Cells[1,i] := 't' + IntToStr(Termparmatrise[i,2]);
    StringGrid3.Cells[2,i] := Formatfloat('0.00',Matchmatrise[i]);
   end;

 end;

end.

 


© Stian Søiland 1998

Valid HTML 4.0!