Private Sub Command1_Click()
Cls
Randomize
Dim MatrixA() As Integer, MatrixB() As Integer, Order
Dim i As Integer, j As Integer
Order = InputBox("请输入方阵的阶数N:", "矩阵运算", 5)
If Order < 1 Or Not IsNumeric(Order) Then
MsgBox "数据错误!", 16
Else
ReDim MatrixA(Order - 1, Order - 1), MatrixB(Order - 1, Order - 1)
For i = LBound(MatrixA) To UBound(MatrixA)
For j = LBound(MatrixA) To UBound(MatrixA)
MatrixA(i, j) = Int(Rnd * 10)
MatrixB(i, j) = Int(Rnd * 10)
Next j
Next i
DisplayMatrix MatrixA, 1
Print
DisplayMatrix MatrixB, 1
Print
MatrixOperation MatrixA, MatrixB, LBound(MatrixA), UBound(MatrixA)
End If
End Sub
Private Sub DisplayMatrix(ByRef a() As Integer, DigitCapacity As Integer)
For i = LBound(a, 1) To UBound(a, 1)
For j = LBound(a, 2) To UBound(a, 2)
If DigitCapacity = 0 Then
Print a(i, j);
Else
Print Format(a(i, j), String(DigitCapacity, "0")) & Space(1);
End If
Next j
Print
Next i
End Sub
Private Sub MatrixOperation(ByRef a() As Integer, ByRef b() As Integer, LowerBound As Integer, UpperBound As Integer)
Dim SunMatrix() As Integer
Dim DifferenceMatrix() As Integer
Dim ProductMatrix() As Integer
Dim i As Integer, j As Integer, k As Integer
ReDim SunMatrix(LowerBound To UpperBound, LowerBound To UpperBound)
ReDim DifferenceMatrix(LowerBound To UpperBound, LowerBound To UpperBound)
ReDim ProductMatrix(LowerBound To UpperBound, LowerBound To UpperBound)
For i = LBound(a) To UBound(a)
For j = LBound(a) To UBound(a)
SunMatrix(i, j) = a(i, j) + b(i, j)
DifferenceMatrix(i, j) = a(i, j) - b(i, j)
For k = LBound(a) To UBound(a)
ProductMatrix(i, j) = ProductMatrix(i, j) + a(i, k) * b(k, j)
Next k
Next j
Next i
DisplayMatrix SunMatrix, 2
Print
DisplayMatrix DifferenceMatrix, 0
Print
DisplayMatrix ProductMatrix, 3
End Sub