用递归算法
FORM1中控件名称从上到下,从左到右依次为:
frame2,text1,command3,command4
frame1,txt(index设为0,visible设为false) frame4,command1,command2 frame3,text2
工程属性中,启动对象设置为Sub main
对n值初始化为4
FORM2为“关于”
源程序:
FORM1中:
Option Explicit
Private Sub Command1_Click()
Dim i, j, a, b, temp_n As Integer Dim result As Double Erase MatrixA ReDim MatrixA(1 To n, 1 To n)
temp_n = n
For i = 1 To cindex If i Mod n = 0 Then a = i / n '/为整数除法 b = n
Else
a = i / n + 1 b = i Mod n End If MatrixA(a, b) = Val(txt(i).Text) Next i
result = calcMatrix(MatrixA(), temp_n)
Text2.Text = Str$(result)
End Sub
Private Sub Command2_Click()
Dim i As Integer For i = 1 To cindex txt(i).Text = "0" Next i
End Sub
Private Sub Command3_Click()
Erase MatrixA m = CInt(Text1.Text) n = m 'm列和n行相等 If n > 23 Then MsgBox "阶数大于23后屏幕显示不够!请重新输入!" Text1.SetFocus Exit Sub End If ReDim MatrixA(1 To n, 1 To n) 'MatrixA(a.b)用来存储第a行第b列的元素值 Unload Form1 Form1.Show
End Sub
Private Sub Command4_Click()
Form2.Show
End Sub
Private Sub Form_Load()
Dim i As Integer, j As Integer Dim nTop As Double Dim nLeft As Double Dim moveWidth As Double Dim moveHeight As Double cindex = 0 Text1.Text = n nLeft = 120 nTop = 240
m = CInt(Text1.Text)
n = m 'm列和n行相等
For i = 1 To n
nLeft = 120 For j = 1 To m cindex = i * m + j - m Load txt(cindex) txt(cindex).Move nLeft, nTop nLeft = nLeft + txt(0).Width + 100 txt(cindex).Visible = True txt(cindex).Text = "0" Next j nTop = nTop + txt(0).Height + 100 Next i If n > 4 Then moveWidth = 595 * (n - 4) moveHeight = 415 * (n - 4) Form1.Width = Form1.Width + moveWidth Form1.Height = Form1.Height + moveHeight Frame1.Width = Frame1.Width + moveWidth Frame1.Height = Frame1.Height + moveHeight Frame3.Move 2700 + moveWidth, 2220 + moveHeight Frame4.Move 1200 + moveWidth, 2220 + moveHeight End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer) If Chr(KeyAscii) >= 0 And Chr(KeyAscii) <= 9 Then KeyAscii = KeyAscii ElseIf KeyAscii <> 8 And KeyAscii <> 9 And KeyAscii <> 10 And KeyAscii <> 13 Then MsgBox "请输入正确的整数!" KeyAscii = Asc("0") End If
End Sub
Private Sub txt_KeyPress(Index As Integer, KeyAscii As Integer)
If Chr(KeyAscii) >= 0 And Chr(KeyAscii) <= 9 Or KeyAscii = 46 Or KeyAscii = 45 Then KeyAscii = KeyAscii ElseIf KeyAscii <> 8 And KeyAscii <> 9 And KeyAscii <> 10 And KeyAscii <> 13 Then
MsgBox "请输入正确的实数!"
KeyAscii = Asc("0") End If End Sub
FORM2 中
Option Explicit
Private Sub Form_Load()
Form2.Icon = Form1.Icon
End Sub
标准模块中:
Option Explicit
Public n, m, cindex As Integer
Public MatrixA() As Double Public a() As Double 'temp2()用于求A()的余子式的中间数组, 'temp1()用于计算行列式值中存储行列式原值的中间数组
Sub Main()
n = 4 '对n初始化,初始阶数为4 Form1.Show ReDim MatrixA(1 To 4, 1 To 4) End Sub
Function calcMatrix(a() As Double, k As Integer) As Double '用递归方法求
Dim c, d As Integer Dim temp As Double Dim i, j, x, y As Integer Dim temp1() As Double Dim temp2() As Double
ReDim temp1(1 To k, 1 To k)
For i = 1 To k
For j = 1 To k temp1(i, j) = a(i, j) ' Next j Next i
d = k
For c = 1 To d
ReDim a(1 To d, 1 To d) '在每降一阶的循环中,a()初始化为原传入的数组
For i = 1 To d For j = 1 To d a(i, j) = temp1(i, j) Next j Next i k = d '初始化k,每调用一次ConvertA,k值减1 If k >= 2 Then ''以下为求行列式a()的第一行元素a(1,c)的余子式 ReDim temp2(1 To k - 1, 1 To k - 1) '求行列式a()的第一行元素a(1,c)的余子式
For x = 1 To k - 1
For y = 1 To k - 1 If y < c Then temp2(x, y) = a(x + 1, y) Else temp2(x, y) = a(x + 1, y + 1) End If Next y Next x k = k - 1
ReDim a(1 To k, 1 To k)
For x = 1 To k For y = 1 To k a(x, y) = temp2(x, y) Next y Next x ''以上为求行列式a()的第一行元素a(1,c)的余子式 temp = (-1) ^ (1 + c) * temp1(1, c) * calcMatrix(a(), k) 'k为阶数,c为元素第几列 calcMatrix = calcMatrix + temp ElseIf k = 1 Then calcMatrix = calcMatrix + a(1, 1) End If Next c End Function
另附:简单四阶行列式计算程序:
利用公式:sum=sum+(-1)^t*A1p1*A2p2*A3p3*A4p4 (t为p1,p2,p3,p4的逆序数)
Dim MatrixA(1 To 4, 0 To 3) As Double
Private Sub Command1_Click() Dim k, l, m, n, i, j As Integer Dim product, sum As Double Dim p(0 To 3), t, tt, a, b, c, d As Integer For k = 0 To 3 MatrixA(1, k) = Val(Text1(k).Text) Text5.Text = Str$(MatrixA(1, k)) Next k For l = 0 To 3 MatrixA(2, l) = Val(Text2(l).Text) Text5.Text = Str$(MatrixA(2, l)) Next l For m = 0 To 3 MatrixA(3, m) = Val(Text3(m).Text) Text5.Text = Str$(MatrixA(3, m)) Next m For n = 0 To 3 MatrixA(4, n) = Val(Text4(n).Text) Text5.Text = Str$(MatrixA(4, n)) Next n For a = 0 To 3 For b = 0 To 3 For c = 0 To 3 For d = 0 To 3 If (a <> b And a <> c And a <> d And b <> c And b <> d And c <> d) Then p(0) = a p(1) = b p(2) = c p(3) = d t = 0 '计算每一项的逆序数t For i = 0 To 3 For j = i + 1 To 3 If (p(i) > p(j)) Then t = t + 1 End If Next j Next i tt = (-1) ^ t product = tt * MatrixA(1, a) * MatrixA(2, b) * MatrixA(3, c) * MatrixA(4, d) sum = sum + product End If Next d Next c Next b Next a Text5.Text = Str$(sum) End Sub Private Sub Command2_Click() Dim i As Integer For i = 0 To 3 Step 1 Text1(i).Text = "0" Text2(i).Text = "0" Text3(i).Text = "0" Text4(i).Text = "0" Next i End Sub
Private Sub Form_Load()
Dim i As Integer For i = 0 To 3 Step 1 Text1(i).Text = "0" Text2(i).Text = "0" Text3(i).Text = "0" Text4(i).Text = "0" Next i End Sub |