⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elec.~pas

📁 自己编写有关电路计算方面的DELPHI 程序
💻 ~PAS
📖 第 1 页 / 共 2 页
字号:
unit elec;

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;
    SpinEdit2: TSpinEdit;
    Label5: TLabel;
    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 double;   {自定义矩阵类型}

procedure GAUSSJ(VAR A:matrix; N:integer; VAR B:array of double);  {声明过程}

var
  Form1: TForm1;
  row_select:integer;       { 弹出菜单的单元格行数}
  j,k,f,t,m,n:integer;      {节点及数组下标}
  Y :matrix;              {系数矩阵}
  X,B :array of double;     {未知量数组\常数项数祖}
  D:array of double;          {支路初值数组}


implementation

{$R *.dfm}

procedure GAUSSJ(VAR A:matrix; N:integer; VAR B:array of double);
var
    IPIV,INDXR,INDXC:array[1..200] of integer;
    JJ,II,KK,LL,LLL:integer;
    BIG,PIVINV,DUM:double; IROW,ICOL:integer;
begin
    For JJ:=1 To N do
      IPIV[JJ]:=0;
    For II:=1 To N do
    begin
        BIG:=0;
        For JJ:=1 To N do
        begin
            If IPIV[JJ] <> 1 Then
            begin
                For KK:=1 To N do
                begin
                    If IPIV[KK] = 0 Then
                    begin
                        If Abs(A[JJ, KK]) >= BIG Then
                        begin
                            BIG:=Abs(A[JJ, KK]);
                            IROW:=JJ;
                            ICOL:=KK;
                        end;
                   end
                    Else if IPIV[KK] > 1 Then
                        BEGIN
                        ShowMessage('该线性方程组无解.');
                        EXIT;
                        END;
                end;
            end;
        end;
        IPIV[ICOL]:=IPIV[ICOL] + 1;
        If IROW <> ICOL Then
        begin
            For LL:=1 To N do
            begin
                DUM:=A[IROW, LL];
                A[IROW, LL]:=A[ICOL, LL];
                A[ICOL, LL]:=DUM;
            end;
            DUM:=B[IROW];
            B[IROW]:=B[ICOL];
            B[ICOL]:=DUM;
        end;
        INDXR[II]:=IROW;
        INDXC[II]:=ICOL;
        If A[ICOL, ICOL] = 0 Then
        BEGIN
        ShowMessage('该线性方程组无解.');
        EXIT;
        END;
        PIVINV:=1 / A[ICOL, ICOL];
        A[ICOL, ICOL]:=1;
        For LL:=1 To N do
          A[ICOL, LL]:=A[ICOL, LL] * PIVINV;
        B[ICOL]:=B[ICOL] * PIVINV;
        For LLL:=1 To N do
        begin
            If LLL <> ICOL Then
            begin
                DUM:=A[LLL, ICOL];
                A[LLL, ICOL]:=0;
                For LL:=1 To N do
                    A[LLL, LL]:=A[LLL,LL] - A[ICOL, LL] * DUM;
                B[LLL]:=B[LLL] - B[ICOL] * DUM;
            end;
        end;
    end;
    For LL:=N DownTo 1 do
    begin
        If INDXR[LL] <> INDXC[LL] Then
        begin
            For KK:=1 To N do
            begin
                DUM:=A[KK, INDXR[LL]];
                A[KK, INDXR[LL]]:=A[KK, INDXC[LL]];
                A[KK, INDXC[LL]]:=DUM;
            end;
        end;
    end;
end;



procedure TForm1.Button3Click(Sender: TObject);
var
   i,l:integer;
begin

   {设置支路赋值表格}
   stringgrid1.ColCount:= SpinEdit2.Value;
   stringgrid1.RowCount:= 2;
   stringgrid1.FixedRows:=1;
   stringgrid1.FixedCols:=0;
   for i:=0 to SpinEdit2.Value do
      stringgrid1.cells[i,0]:='X'+inttostr(i+1);

   {设置数据置入表格}
   stringgrid2.RowCount:= SpinEdit2.Value+1;
   stringgrid2.ColCount:=7;
   stringgrid2.FixedRows:=1;
   stringgrid2.FixedCols:=0;
   stringgrid2.cells[1,0]:='节点J';
   stringgrid2.cells[2,0]:='节点K';
   stringgrid2.cells[3,0]:='节点F';
   stringgrid2.cells[4,0]:='节点T';
   stringgrid2.cells[5,0]:='管路m';
   stringgrid2.cells[6,0]:='类型';

   {预制表头}
   for i:=1 to SpinEdit2.Value do
      stringgrid2.cells[0,i]:='X'+inttostr(i);

   {设置矩阵表格}
   stringgrid3.ColCount:= SpinEdit1.Value+5;
   stringgrid3.RowCount:= SpinEdit1.Value+5;
   stringgrid3.FixedRows:=1;
   stringgrid3.FixedCoLs:=1;

   {设置结果数据表格}
   stringgrid4.ColCount:= SpinEdit1.Value+4;
   stringgrid4.RowCount:= 2;
   stringgrid4.FixedRows:=1;
   stringgrid4.FixedCols:=0;
   for i:=1 to SpinEdit1.Value do                {预制表头}
      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';

   {设置数据置入表格}
   stringgrid5.RowCount:= SpinEdit1.Value+4;
   stringgrid5.ColCount:=2;
   stringgrid5.FixedCols:=1;
   for i:=1 to SpinEdit1.Value do                 {预制表头}
      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';

    {预制矩阵数组}
    SetLength(Y,SpinEdit1.Value+5,SpinEdit1.Value+5);    {系数矩阵}
    SetLength(B,SpinEdit1.Value+5);                      {常数项数祖}
    SetLength(X,SpinEdit1.Value+5);                      {未知量数组}
    SetLength(D,SpinEdit2.Value);                        {支路初值数组}

    {系数矩阵赋初值}
     for i:=0 to stringgrid3.ColCount-1 do
       for l:=0 to stringgrid3.rowCount-1 do
         stringgrid3.cells[i,l]:=floattostr(Y[i,l]);
      for i:=0 to stringgrid3.ColCount-1 do
        stringgrid3.cells[0,i]:=IntToStr(i);
      for i:=0 to stringgrid3.RowCount-1 do
        stringgrid3.cells[i,0]:=IntToStr(i);
      stringgrid3.cells[0,0]:='';

     {常数项数祖赋初值}
      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];

     {设置弹出菜单属性}
    if ACol=6 then
      PopupMenu1.AutoPopup :=True
     else
     PopupMenu1.AutoPopup :=False;

     {取焦点所在行的行标}
     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
     {设置弹出菜单属性}
    PopupMenu1.AutoPopup := False;
    StringGrid2.PopupMenu := PopupMenu1;
end;


        {*******弹出菜单的动作***********}


{导纳元件}
procedure TForm1.N1Click(Sender: TObject);
var
   i,u,v:integer;

begin
                                                             {表格中显示选中弹出菜单文本}
     StringGrid2.Cells[6,row_select]:= N1.Caption;
     if TryStrToInt(trim(StringGrid2.Cells[1,row_select]),u) then                                                     {取下标}
        j:=u
     else
        showmessage('节点J标号置入错误,重新输入节点J标号');
     if TryStrToInt(trim(StringGrid2.Cells[2,row_select]),v) then
        k:=v
     else
        showmessage('节点K标号置入错误,重新输入节点K标号');

     if (k < SpinEdit1.Value+5) and (j  < SpinEdit1.Value+5) then
     begin                                                   {对系数矩阵赋值}
     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];
                                                              {系数矩阵显示}

     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]);

     for i:=0 to stringgrid3.ColCount-1 do
        stringgrid3.cells[0,i]:=IntToStr(i);
      for i:=0 to stringgrid3.RowCount-1 do
        stringgrid3.cells[i,0]:=IntToStr(i);
      stringgrid3.cells[0,0]:='';
     end
     else
       showmessage('数组下标越界,重新输入节点标号');
end;

{电流源}
procedure TForm1.N2Click(Sender: TObject);
var
   i,u,v:integer;
begin
    StringGrid2.Cells[6,row_select]:= N2.Caption;
                                                               {取下标}
     if TryStrToInt(trim(StringGrid2.Cells[1,row_select]),u) then                                                     {取下标}
        j:=u
     else
        showmessage('节点J标号置入错误,重新输入节点J标号');
     if TryStrToInt(trim(StringGrid2.Cells[2,row_select]),v) then
        k:=v
     else
        showmessage('节点K标号置入错误,重新输入节点K标号');

     if (k < SpinEdit1.Value+5) and (j  < SpinEdit1.Value+5)then
                                                                {对常数矩阵赋值}
       begin
       B[j]:=B[j]-D[row_select-1];
       B[k]:=B[k]+D[row_select-1];

       if j>0 then                                                {常数矩阵显示}
       StringGrid5.Cells[1,j-1]:=floattostr(B[j]);
       if k>0 then
       StringGrid5.Cells[1,K-1]:=floattostr(B[K]);

       end
       else
       showmessage('数组下标越界,重新输入节点标号');
end;


{理想电压源}
procedure TForm1.N3Click(Sender: TObject);
var
   i,u,v:integer;
begin
      StringGrid2.Cells[6,row_select]:= N3.Caption;
                                                      {取下标}
     if TryStrToInt(trim(StringGrid2.Cells[1,row_select]),u) then                                                     {取下标}
        j:=u
     else
        showmessage('节点J标号置入错误,重新输入节点J标号');
     if TryStrToInt(trim(StringGrid2.Cells[2,row_select]),v) then
        k:=v
     else
        showmessage('节点K标号置入错误,重新输入节点K标号');

     if (k < SpinEdit1.Value+5) and (j  < SpinEdit1.Value+5)then
     begin                                             {对系数矩阵赋值}
       Y[j,SpinEdit1.Value+1]:=Y[j,SpinEdit1.Value+1]+1;
       Y[k,SpinEdit1.Value+1]:=Y[k,SpinEdit1.Value+1]-1;
       Y[SpinEdit1.Value+1,j]:=Y[SpinEdit1.Value+1,j]+1;
       Y[SpinEdit1.Value+1,k]:=Y[SpinEdit1.Value+1,k]-1;
                                                         {对常数矩阵赋值}
       B[SpinEdit1.Value]:=B[SpinEdit1.Value]+D[row_select-1];
                                                         {系数矩阵显示}

       StringGrid3.Cells[j,SpinEdit1.Value+1]:=floattostr(Y[j,SpinEdit1.Value+1]);
       StringGrid3.Cells[SpinEdit1.Value+1,j]:=floattostr(Y[SpinEdit1.Value+1,j]);
       StringGrid3.Cells[k,SpinEdit1.Value+1]:=floattostr(Y[k,SpinEdit1.Value+1]);
       StringGrid3.Cells[SpinEdit1.Value+1,k]:=floattostr(Y[SpinEdit1.Value+1,k]);

                                                             {常数矩阵显示}
      StringGrid5.Cells[1,SpinEdit1.Value]:=floattostr(B[SpinEdit1.Value]);

      for i:=0 to stringgrid3.ColCount-1 do
        stringgrid3.cells[0,i]:=IntToStr(i);
      for i:=0 to stringgrid3.RowCount-1 do
        stringgrid3.cells[i,0]:=IntToStr(i);
        stringgrid3.cells[0,0]:='';
     end
     else
       showmessage('数组下标越界,重新输入节点标号');
end;


{电流控制电流源}
procedure TForm1.N51Click(Sender: TObject);
var
   i,u,v,w,z,h:integer;
begin
   StringGrid2.Cells[6,row_select]:= N51.Caption;
   if TryStrToInt(trim(StringGrid2.Cells[1,row_select]),u) then                                                     {取下标}
        j:=u

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -