unit matrdegree; {$mode objfpc}{$H+} interface uses Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids; type { TForm1 } TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Edit1: TEdit; Edit2: TEdit; Label1: TLabel; Label2: TLabel; StringGrid1: TStringGrid; StringGrid2: TStringGrid; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Edit1KeyPress(Sender: TObject; var Key: char); procedure Edit2Change(Sender: TObject); procedure Edit2KeyPress(Sender: TObject; var Key: char); procedure FormActivate(Sender: TObject); procedure StringGrid1KeyPress(Sender: TObject; var Key: char); private { private declarations } public { public declarations } end; var Form1: TForm1; implementation procedure CreateTable; var k: integer; begin if Form1.Edit1.Text='' then begin Form1.Edit1.SetFocus; Showmessage('Введите порядок квадратной матрицы <=20'); exit; end; k:=StrToInt(Form1.Edit1.Text); if k>20 then begin Form1.Edit1.Text:=''; Form1.Edit1.SetFocus; Showmessage('Введите порядок квадратной матрицы <=20'); exit; end; Form1.StringGrid1.ColCount:=k; Form1.StringGrid1.RowCount:=k; Form1.StringGrid1.AutoFillColumns:=true; Form1.StringGrid1.Visible:=True; Form1.StringGrid1.Width:=k*130; Form1.StringGrid1.Height:=k*20+5; Form1.StringGrid2.Top:=k*20+60; Form1.Label2.Caption:='Матрица, возведённая в степень '; Form1.label2.Top:=k*20+45; Form1.StringGrid2.ColCount:=k; Form1.StringGrid2.RowCount:=k; Form1.StringGrid2.AutoFillColumns:=true; Form1.StringGrid2.Width:=k*130; Form1.StringGrid2.Height:=k*20+5; Form1.StringGrid2.Visible:=True; Form1.Button3.SetFocus; end; procedure Rand; var i, j, k: integer; begin Randomize(); k:=StrToInt(Form1.Edit1.Text); for i:= 1 to k do for j:= 1 to k do Form1.StringGrid1.Cells[j-1,i-1]:=FloatToStr((Random(1000000)-500000)/100000); Form1.Edit2.SetFocus; end; procedure degree; var n: integer; m: array[0..10] of integer; i: integer; j: integer; l, s: integer; p: integer; r, q: integer; k: integer; a: array [1..20, 1..20] of real; b: array [1..20, 1..20] of real; c: array [1..20, 1..20] of real; begin if Form1.Edit2.Text='' then begin Showmessage('Введите степень'); Form1.Edit2.SetFocus; exit; end; k:=StrToInt(Form1.Edit1.Text); n:=StrToInt(Form1.Edit2.Text); for p:=0 to 10 do m[p]:=0; p:=0; q:=n; repeat r:=q mod 2; q:=q div 2; m[p]:=r; p:=p+1; until q=0; for i:= 1 to k do for j:=1 to k do if Form1.StringGrid1.Cells[j-1,i-1]='' then Form1.StringGrid1.Cells[j-1,i-1]:='0'; for i:= 1 to k do for j:=1 to k do a[i,j]:=StrToFloat(Form1.StringGrid1.Cells[j-1,i-1]); for i:= 1 to k do for j:=1 to k do if i=j then c[i,j]:=1 else c[i,j]:=0; for i:= 1 to k do for j:=1 to k do b[i,j]:=a[i,j]; for s:=0 to p-1 do begin if s>=1 then begin for i:= 1 to k do for j:= 1 to k do begin b[i,j]:=0; for l:= 1 to k do b[i,j]:=b[i,j]+a[i,l]*a[l,j]; end; for i:= 1 to k do for j:= 1 to k do a[i,j]:=b[i,j]; end; if m[s]=1 then begin for i:= 1 to k do for j:= 1 to k do begin b[i,j]:=0; for l:= 1 to k do b[i,j]:=b[i,j]+a[i,l]*c[l,j]; end; for i:= 1 to k do for j:= 1 to k do c[i,j]:=b[i,j]; end; end; for i:= 1 to k do for j:=1 to k do Form1.StringGrid2.Cells[j-1,i-1]:=FloatToStr(c[i,j]); Form1.Label2.Caption:='Матрица, возведённая в степень '+IntToStr(n); end; { TForm1 } procedure TForm1.FormActivate(Sender: TObject); begin Edit1.SetFocus; end; procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: char); begin case Key of '0'..'9': ; // цифра #8 : ; // ',': if pos(',',StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row])<>0 then Key :=Chr(0); //повторно запятую вводить нельзя '-': if Length(StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row]) <>0 then Key :=Chr(0); //минус вводится только первым символом #13: begin if StringGrid1.Col < StringGrid1.ColCount - 1 then StringGrid1.Col:=StringGrid1.Col+1; end; else Key :=Chr(0); // символ не отображать end; end; procedure TForm1.Button1Click(Sender: TObject); begin CreateTable; end; procedure TForm1.Button2Click(Sender: TObject); begin degree; end; procedure TForm1.Button3Click(Sender: TObject); begin rand; StringGrid2.Clean; end; procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char); begin case Key of '0'..'9': ; // цифра #8 : ; // #13 : CreateTable; //создать таблицу else Key :=Chr(0); // символ не отображать end; end; procedure TForm1.Edit2Change(Sender: TObject); begin StringGrid2.Clean; end; procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: char); begin case Key of '0'..'9': ; // цифра #8 : ; // #13 : degree; // возвести матрицу в степень else Key :=Chr(0); // символ не отображать end; end; initialization {$I matrdegree.lrs} end.