永发信息网

VB实现2矩阵相乘

答案:1  悬赏:20  手机版
解决时间 2021-06-02 03:17
  • 提问者网友:疯子也有疯子的情调
  • 2021-06-01 06:55

Dim a(), b(), g(), i As Integer, j As Integer, x As Integer, y As Integer
Dim c As Integer, d As Integer, m As Integer, n As Integer, p As Integer, q As Integer, Temp As Integer, str1 As String, str2 As String
Private Sub Command1_Click()
c = Val(InputBox("输入矩阵行数", "提示"))
d = Val(InputBox("输入矩阵列数", "提示"))
ReDim a(1 To c, 1 To d)
For i = 1 To c
For j = 1 To d
str1 = "输入矩阵的" & i & "行" & j & "列元素"
a(i, j) = Val(InputBox(str1, "提示"))
Next j
Next i
Print
Print Tab(3); "矩阵A的各元素:";
For i = 1 To c
For j = 1 To d
Print Tab(j * 3); a(i, j);
Next j
Print
Next i
End Sub


Private Sub Command2_Click()
m = Val(InputBox("输入矩阵行数", "提示"))
n = Val(InputBox("输入矩阵列数", "提示"))
ReDim b(1 To m, 1 To n)
For x = 1 To m
For y = 1 To n
str2 = "输入矩阵的" & x & "行" & y & "列元素"
b(x, y) = Val(InputBox(str2, "提示"))
Next y
Next x
Print
Print Tab(3); "矩阵B的各元素:";
For x = 1 To m
For y = 1 To n
Print Tab(y * 3); b(x, y);
Next y
Print
Next x
End Sub

Private Sub Command3_Click()
For i = 1 To c
For y = 1 To n
Temp = 0
For j = 1 To d
For x = 1 To m
Temp = Temp + a(i, j) * b(x, y)
Next x
Next j
Print Tab(3); "计算结果:"
g(i, y) = Temp
Print Tab(y * 3); g(i, y);
Next y
Print
Next i
End Sub

各位高手帮忙看看哪出毛病了。。。

最佳答案
  • 五星知识达人网友:七十二街
  • 2021-06-01 07:05
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer, M As Integer, N As Integer
Dim A() As Double, B() As Double, C() As Double
M = Val(Text1.Text) '>0
N = Val(Text2.Text) '>0。错误检验代码自己写
ReDim A(M, N), B(N, M), C(M, M)
k = 500
Randomize
Print "M="; M; "行,N="; N; "列"
Print "A矩阵"
For i = 1 To M
For j = 1 To N
A(i, j) = Int(Rnd * 100)
Print A(i, j);
Next j
Print
Next i
Print "B矩阵"
For i = 1 To N
For j = 1 To M
B(i, j) = Int(Rnd * 100)
Print B(i, j);
Next j
Print
Next i
Print "AxB=C矩阵"

For i = 1 To M
For j = 1 To M
For k = 1 To N
C(i, j) = C(i, j) + A(i, k) * B(k, j)
Next k
Print C(i, j);
Next j
Print
Next i
End Sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯