Текст Unit1
unit Unit1; //Операции над матрицами Interface
Uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Menus, Spin, Grids; Type TForm1 = class(TForm) mm1: TMainMenu; N1: TMenuItem; N2: TMenuItem; N3: TMenuItem; N4: TMenuItem; N5: TMenuItem; N6: TMenuItem; N7: TMenuItem; N8: TMenuItem; N9: TMenuItem; N10: TMenuItem; N11: TMenuItem; dlgOpen1: TOpenDialog; dlgSave1: TSaveDialog; se1: TSpinEdit; lbl1: TLabel; sd1: TStringGrid; sd11: TStringGrid; sd12: TStringGrid; lbl2: TLabel; lbl3: TLabel; procedure N4Click(Sender: TObject); procedure N9Click(Sender: TObject); procedure N11Click(Sender: TObject); procedure N6Click(Sender: TObject); procedure N7Click(Sender: TObject); procedure N8Click(Sender: TObject); procedure N10Click(Sender: TObject); procedure N2Click(Sender: TObject); procedure N3Click(Sender: TObject); procedure sd1KeyPress(Sender: TObject; var Key: Char); Private { Private declarations } Public { Public declarations } end; tmas2= array [1..10, 1..10] of integer; Var Form1: TForm1;
Implementation
{$R *.dfm} Var a, b, c: tmas2; n: Integer; procedure TForm1.N9Click(Sender: TObject); //Пункт меню " Выход" Begin Close; end; procedure TForm1.N4Click(Sender: TObject); //Пункт меню " Выход" Begin Close; end;
procedure TForm1.N11Click(Sender: TObject); //Формирование StringGrid для ввода с клавиатуры Begin With sd1 do Begin n: =se1.Value; ColCount: =n; RowCount: =n; Width: =n*(DefaultColWidth+2)+4; Height: =n*(DefaultRowHeight+2)+4; end; With sd11 do Begin ColCount: =n; RowCount: =n; Width: =n*(DefaultColWidth+2)+4; Height: =n*(DefaultRowHeight+2)+4; Left: =sd1.Left+sd1.Width+24; end; With sd12 do Begin ColCount: =n; RowCount: =n; Width: =n*(DefaultColWidth+2)+4; Height: =n*(DefaultRowHeight+2)+4; Left: =sd11.Left+sd11.Width+24; end; lbl2.Left: =sd11.Left-22; If Odd(n) then lbl2.Top: =sd1.Top+(n div 2)*(sd1.DefaultRowHeight+2) Else lbl2.Top: =sd1.Top+(n div 2)*(sd1.DefaultRowHeight+2)-12; lbl2.Caption: =''; lbl3.Left: =sd12.Left-22; If Odd(n) then lbl3.Top: =sd11.Top+(n div 2)*(sd11.DefaultRowHeight+2) Else lbl3.Top: =sd11.Top+(n div 2)*(sd11.DefaultRowHeight+2)-12; lbl3.Caption: ='='; Form1.Width: =sd1.Left+sd1.Width+sd11.Width+sd12.Width+70; N6.Enabled: =True; N7.Enabled: =True; N8.Enabled: =True; N10.Enabled: =True; sd1.SetFocus; end;
procedure matr(var a, b: tmas2; n: Integer); //Формирование массивов a и b Var i, j: Integer; Begin for i: =1 to n do for j: =1 to n do Begin a[i, j]: =StrToInt(Form1.sd1.Cells[j-1, i-1]); b[i, j]: =StrToInt(Form1.sd11.Cells[j-1, i-1]); end; end;
procedure res(c: tmas2; n: Integer); //Результат-матрица C помещается в StringGrid Var i, j: Integer; Begin for i: =1 to n do for j: =1 to n do Form1.sd12.Cells[j-1, i-1]: =IntToStr(c[i, j]); end; procedure TForm1.N6Click(Sender: TObject); //Сложение матриц Var i, j: Integer; Begin lbl2.Caption: ='+'; matr(a, b, n); for i: =1 to n do for j: =1 to n do c[i, j]: =a[i, j]+b[i, j]; Res(c, n); end;
procedure TForm1.N7Click(Sender: TObject); //Вычитание матриц Var i, j: Integer; Begin lbl2.Caption: ='-'; matr(a, b, n); for i: =1 to n do for j: =1 to n do c[i, j]: =a[i, j]-b[i, j]; Res(c, n); end;
procedure TForm1.N8Click(Sender: TObject); //Произведение матриц Var i, j, k, s: Integer; Begin lbl2.Caption: ='x'; matr(a, b, n); for i: =1 to n do for j: =1 to n do Begin s: =0; for k: =1 to n do s: =s+a[i, k]*b[k, j]; c[i, j]: =s; end; Res(c, n);
end;
procedure TForm1.N10Click(Sender: TObject); //Очистка StrigGrid Var i, j: Integer; Begin for i: =1 to n do for j: =1 to n do Begin Form1.sd1.Cells[j-1, i-1]: =''; Form1.sd11.Cells[j-1, i-1]: =''; Form1.sd12.Cells[j-1, i-1]: =''; end; end;
procedure TForm1.N2Click(Sender: TObject); //Чтение исходных матриц из файла Var i, j, s: Integer; fin: TextFile; Begin
|