矩阵相乘 VB代码

2024-11-13 10:11:20
推荐回答(1个)
回答1:

'建立4个label1(),index 0--3。6个label2(),index 0--5。6个text1(),index 0--5
'建立1个label3,两个command1和2
'Form1.MaxButton = False

Dim I As Byte, J As Byte, A As Byte, B As Byte
Dim M As Byte, N As Byte, MN As Byte
Dim JZ1() As Single, JZ2() As Single, JZ3() As Single
Dim SS As String

Private Sub Command1_Click()
If Command1.Caption = "确定" Then
Command1.Caption = "完成"
ReDim JZ1(1 To M, 1 To MN) As Single
ReDim JZ2(1 To MN, 1 To N) As Single
ReDim JZ3(1 To M, 1 To N) As Single
For I = 0 To 5
Text1(I).Locked = True
Text1(I).BackColor = &HF0F0F0
Text1(I).BorderStyle = 0
Next I
Label3.Caption = "请输入相乘的两个矩阵,输入完成后按<完成>键"
For I = 1 To M
For J = 1 To MN
JZ1(I, J) = Val(InputBox("第一个矩阵____第" + Str(I) + "行 第" + Str(J) + "列元素:", "输入矩阵", 3))
CurrentX = 100 + (J - 1) * 350
CurrentY = 1000 + (I - 1) * 200
Print JZ1(I, J)
Next
Next
For I = 1 To MN
For J = 1 To N
JZ2(I, J) = Val(InputBox("第二个矩阵____第" + Str(I) + "行 第" + Str(J) + "列元素:", "输入矩阵", 3))
CurrentX = 3000 + (J - 1) * 350
CurrentY = 1000 + (I - 1) * 200
Print JZ2(I, J)
Next
Next
'Select Case M
ElseIf Command1.Caption = "完成" Then
Command1.Caption = "重新开始"
For I = 1 To M
For J = 1 To N
JZ3(I, J) = 0
For A = 1 To MN
JZ3(I, J) = JZ3(I, J) + JZ1(I, A) * JZ2(A, J)
Next
CurrentX = 6000 + (J - 1) * 350
CurrentY = 1000 + (I - 1) * 200
Print JZ3(I, J)
Next
Next
Else
Form_Load
End If
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
Cls
Form1.Width = 9000
Form1.Height = 7000

For I = 0 To 2
Label1(I).Width = 1000
Label1(I).Height = 200
Label1(I).Top = 0
Label1(I).Left = 700 + I * 3000
Label1(I).Caption = "第" + Str(I + 1) + "个矩阵"
Next
For I = 0 To 5
Text1(I).Width = 400
Text1(I).Height = 200
Text1(I).Top = 300
Label2(I).Width = 200
Label2(I).Height = Text1(I).Height
Label2(I).Top = Text1(I).Top + 50
Text1(I).Text = 2
Text1(I).Alignment = 1
If I Mod 2 Then
Text1(I).Left = I * 1500 - 300
Label2(I).Caption = "列"
Else
Text1(I).Left = 300 + I * 1500
Label2(I).Caption = "行"
End If
Label2(I).Left = Text1(I).Left + 450
Next
M = 2
N = 2
MN = 2
Text1(2).Locked = True
Text1(4).Locked = True
Text1(5).Locked = True
Text1(0).Locked = False
Text1(1).Locked = False
Text1(3).Locked = False
For I = 0 To 5
Text1(I).BackColor = &HFFFFFF
Text1(I).BorderStyle = 1
Next I
Command1.Caption = "确定"
Command2.Caption = "退出"
Label3.Caption = "请输入矩阵的行和列,完成后按<确定>键"
End Sub
Private Sub Text1_Change(Index As Integer)
Text1(2).Text = Text1(1).Text
Text1(4).Text = Text1(0).Text
Text1(5).Text = Text1(3).Text
M = Val(Text1(0).Text)
MN = Val(Text1(1).Text)
N = Val(Text1(3).Text)
End Sub
已经运行过。