?? elec_2.~pas
字號(hào):
unit elec_2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Spin, Grids, Menus;
type
TForm1 = class(TForm)
Label1: TLabel;
SpinEdit1: TSpinEdit;
Label3: TLabel;
Label6: TLabel;
Button2: TButton;
Button3: TButton;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
StringGrid3: TStringGrid;
Label2: TLabel;
StringGrid4: TStringGrid;
Label4: TLabel;
StringGrid5: TStringGrid;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N51: TMenuItem;
N61: TMenuItem;
N71: TMenuItem;
N81: TMenuItem;
N91: TMenuItem;
Button4: TButton;
procedure Button3Click(Sender: TObject);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure StringGrid2SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure StringGrid5SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure FormCreate(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N51Click(Sender: TObject);
procedure N4Click(Sender: TObject);
procedure N61Click(Sender: TObject);
procedure N71Click(Sender: TObject);
procedure N81Click(Sender: TObject);
procedure N91Click(Sender: TObject);
procedure StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: String);
procedure StringGrid2SetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: String);
procedure StringGrid5SetEditText(Sender: TObject; ACol, ARow: Integer;
const Value: String);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
matrix = array of array of real; {自定義矩陣類(lèi)型}
procedure GAUSSJ(VAR A:matrix; N:integer; VAR B:array of real); {聲明過(guò)程}
var
Form1: TForm1;
row_select:integer; {彈出菜單的單元格行數(shù)}
j,k,f,t,m:integer; {節(jié)點(diǎn)及數(shù)組下標(biāo)}
Y:matrix; {系數(shù)矩陣}
B:array of real; {常數(shù)項(xiàng)數(shù)祖}
X:array of real; {未知量數(shù)組}
D:array of real; {支路初值數(shù)組}
implementation
{$R *.dfm}
procedure GAUSSJ(VAR A:matrix; N:integer; VAR B:array of real);
var
IPIV,INDXR,INDXC:array[1..50] of integer;
J,I,K,L,LL:integer;
BIG,PIVINV,DUM:real; IROW,ICOL:integer;
begin
For J:=1 To N do
IPIV[J]:=0;
For I:=1 To N do
begin
BIG:=0;
For J:=1 To N do
begin
If IPIV[J] <> 1 Then
begin
For K:=1 To N do
begin
If IPIV[K] = 0 Then
begin
If Abs(A[J, K]) >= BIG Then
begin
BIG:=Abs(A[J, K]);
IROW:=J;
ICOL:=K;
end;
end
Else if IPIV[K] > 1 Then
ShowMessage('Singular matrix.');
end;
end;
end;
IPIV[ICOL]:=IPIV[ICOL] + 1;
If IROW <> ICOL Then
begin
For L:=1 To N do
begin
DUM:=A[IROW, L];
A[IROW, L]:=A[ICOL, L];
A[ICOL, L]:=DUM;
end;
DUM:=B[IROW];
B[IROW]:=B[ICOL];
B[ICOL]:=DUM;
end;
INDXR[I]:=IROW;
INDXC[I]:=ICOL;
If A[ICOL, ICOL] = 0 Then ShowMessage('Singular matrix.');
PIVINV:=1 / A[ICOL, ICOL];
A[ICOL, ICOL]:=1;
For L:=1 To N do
A[ICOL, L]:=A[ICOL, L] * PIVINV;
B[ICOL]:=B[ICOL] * PIVINV;
For LL:=1 To N do
begin
If LL <> ICOL Then
begin
DUM:=A[LL, ICOL];
A[LL, ICOL]:=0;
For L:=1 To N do
A[LL, L]:=A[LL, L] - A[ICOL, L] * DUM;
B[LL]:=B[LL] - B[ICOL] * DUM;
end;
end;
end;
For L:=N DownTo 1 do
begin
If INDXR[L] <> INDXC[L] Then
begin
For K:=1 To N do
begin
DUM:=A[K, INDXR[L]];
A[K, INDXR[L]]:=A[K, INDXC[L]];
A[K, INDXC[L]]:=DUM;
end;
end;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
i,l:integer;
begin
{設(shè)置支路賦值表格}
stringgrid1.ColCount:= SpinEdit1.Value;
stringgrid1.RowCount:= 2;
stringgrid1.FixedRows:=1;
stringgrid1.FixedCols:=0;
for i:=0 to SpinEdit1.Value do
stringgrid1.cells[i,0]:='X'+inttostr(i+1);
{設(shè)置數(shù)據(jù)置入表格}
stringgrid2.RowCount:= SpinEdit1.Value+1;
stringgrid2.ColCount:=7;
stringgrid2.FixedRows:=1;
stringgrid2.FixedCols:=1;
stringgrid2.cells[1,0]:='節(jié)點(diǎn)J';
stringgrid2.cells[2,0]:='節(jié)點(diǎn)K';
stringgrid2.cells[3,0]:='節(jié)點(diǎn)F';
stringgrid2.cells[4,0]:='節(jié)點(diǎn)T';
stringgrid2.cells[5,0]:='管路m';
stringgrid2.cells[6,0]:='類(lèi)型';
{預(yù)制表頭}
for i:=1 to SpinEdit1.Value do
stringgrid2.cells[0,i]:='X'+inttostr(i);
{設(shè)置矩陣表格}
stringgrid3.ColCount:= SpinEdit1.Value+4;
stringgrid3.RowCount:= SpinEdit1.Value+4;
{設(shè)置結(jié)果數(shù)據(jù)表格}
stringgrid4.ColCount:= SpinEdit1.Value+4;
stringgrid4.RowCount:= 2;
stringgrid4.FixedRows:=1;
stringgrid4.FixedCols:=0;
for i:=1 to SpinEdit1.Value do {預(yù)制表頭}
stringgrid4.cells[i-1,0]:='Un'+inttostr(i);
stringgrid4.cells[i-1,0]:='Ie';
stringgrid4.cells[i,0]:='Ivv';
stringgrid4.cells[i+1,0]:='Icv';
stringgrid4.cells[i+2,0]:='Iev';
{設(shè)置數(shù)據(jù)置入表格}
stringgrid5.RowCount:= SpinEdit1.Value+4;
stringgrid5.ColCount:=2;
stringgrid5.FixedCols:=1;
for i:=1 to SpinEdit1.Value do {預(yù)制表頭}
stringgrid5.cells[0,i-1]:='J'+inttostr(i);
stringgrid5.cells[0,i-1]:='E';
stringgrid5.cells[0,i]:='0';
stringgrid5.cells[0,i+1]:='0';
stringgrid5.cells[0,i+2]:='0';
{預(yù)制矩陣數(shù)組}
SetLength(Y,SpinEdit1.Value+4,SpinEdit1.Value+4); {系數(shù)矩陣}
SetLength(B,SpinEdit1.Value+4); {常數(shù)項(xiàng)數(shù)祖}
SetLength(X,SpinEdit1.Value+4); {未知量數(shù)組}
SetLength(D,SpinEdit1.Value); {支路初值數(shù)組}
{系數(shù)矩陣賦初值}
for i:=0 to stringgrid3.ColCount-1 do
for l:=0 to stringgrid3.rowCount-1 do
stringgrid3.cells[i,l]:=floattostr(Y[i,l]);
{常數(shù)項(xiàng)數(shù)祖賦初值}
for i:=0 to stringgrid5.RowCount-1 do
stringgrid5.cells[1,i]:=floattostr(B[i]);
end;
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
{使表格1可編輯}
StringGrid1.options:=StringGrid1.options+[GoEditing];
end;
procedure TForm1.StringGrid2SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
{使表格2可編輯}
StringGrid2.options:=StringGrid2.options+[GoEditing];
{設(shè)置彈出菜單屬性}
if ACol=6 then
PopupMenu1.AutoPopup :=True
else
PopupMenu1.AutoPopup :=False;
{取焦點(diǎn)所在行的行標(biāo)}
row_select:=ARow;
end;
procedure TForm1.StringGrid5SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
{使表格5可編輯}
StringGrid5.options:=StringGrid5.options+[GoEditing];
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
{設(shè)置彈出菜單屬性}
PopupMenu1.AutoPopup := False;
StringGrid2.PopupMenu := PopupMenu1;
end;
{*******彈出菜單的動(dòng)作***********}
{導(dǎo)納元件}
procedure TForm1.N1Click(Sender: TObject);
var
u,v:integer;
begin
{表格中顯示選中彈出菜單文本}
StringGrid2.Cells[6,row_select]:= N1.Caption;
if TryStrToInt(trim(StringGrid2.Cells[1,row_select]),u) then {取下標(biāo)}
j:=u
else
showmessage('節(jié)點(diǎn)J標(biāo)號(hào)置入錯(cuò)誤,重新輸入節(jié)點(diǎn)J標(biāo)號(hào)');
if TryStrToInt(trim(StringGrid2.Cells[2,row_select]),v) then
k:=v
else
showmessage('節(jié)點(diǎn)K標(biāo)號(hào)置入錯(cuò)誤,重新輸入節(jié)點(diǎn)K標(biāo)號(hào)');
if (k < SpinEdit1.Value+4) and (j < SpinEdit1.Value+4) then
begin {對(duì)系數(shù)矩陣賦值}
Y[j,j]:=Y[j,j]+D[row_select-1];
Y[j,k]:=Y[j,k]-D[row_select-1];
Y[k,j]:=Y[k,j]-D[row_select-1];
Y[k,k]:=Y[k,k]+D[row_select-1];
{系數(shù)矩陣顯示}
StringGrid3.Cells[j,j]:=floattostr(Y[j,j]);
StringGrid3.Cells[j,k]:=floattostr(Y[j,k]);
StringGrid3.Cells[k,j]:=floattostr(Y[k,j]);
StringGrid3.Cells[k,k]:=floattostr(Y[k,k]);
end
else
showmessage('數(shù)組下標(biāo)越界,重新輸入節(jié)點(diǎn)標(biāo)號(hào)');
end;
{電流源}
procedure TForm1.N2Click(Sender: TObject);
var
u,v:integer;
begin
StringGrid2.Cells[6,row_select]:= N2.Caption;
{取下標(biāo)}
if TryStrToInt(trim(StringGrid2.Cells[1,row_select]),u) then {取下標(biāo)}
j:=u
else
showmessage('節(jié)點(diǎn)J標(biāo)號(hào)置入錯(cuò)誤,重新輸入節(jié)點(diǎn)J標(biāo)號(hào)');
if TryStrToInt(trim(StringGrid2.Cells[2,row_select]),v) then
k:=v
else
showmessage('節(jié)點(diǎn)K標(biāo)號(hào)置入錯(cuò)誤,重新輸入節(jié)點(diǎn)K標(biāo)號(hào)');
if (k < SpinEdit1.Value+4) and (j < SpinEdit1.Value+4)then
{對(duì)常數(shù)矩陣賦值}
begin
B[j]:=B[j]-D[row_select-1];
B[k]:=B[k]+D[row_select-1];
{常數(shù)矩陣顯示}
StringGrid5.Cells[1,j]:=floattostr(B[J]);
StringGrid5.Cells[1,K]:=floattostr(B[K]);
end
else
showmessage('數(shù)組下標(biāo)越界,重新輸入節(jié)點(diǎn)標(biāo)號(hào)');
end;
{理想電壓源}
procedure TForm1.N3Click(Sender: TObject);
var
u,v:integer;
begin
StringGrid2.Cells[6,row_select]:= N3.Caption;
{取下標(biāo)}
if TryStrToInt(trim(StringGrid2.Cells[1,row_select]),u) then {取下標(biāo)}
j:=u
else
showmessage('節(jié)點(diǎn)J標(biāo)號(hào)置入錯(cuò)誤,重新輸入節(jié)點(diǎn)J標(biāo)號(hào)');
if TryStrToInt(trim(StringGrid2.Cells[2,row_select]),v) then
k:=v
else
showmessage('節(jié)點(diǎn)K標(biāo)號(hào)置入錯(cuò)誤,重新輸入節(jié)點(diǎn)K標(biāo)號(hào)');
if (k < SpinEdit1.Value+4) and (j < SpinEdit1.Value+4)then
begin {對(duì)系數(shù)矩陣賦值}
Y[j,row_select+1]:=1;
Y[k,row_select+1]:=-1;
Y[row_select+1,j]:=1;
Y[row_select+1,k]:=-1;
{對(duì)常數(shù)矩陣賦值}
B[SpinEdit1.Value]:=D[row_select-1];
{系數(shù)矩陣顯示}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -