VB关于矩阵的操作问题

    技术2022-06-25  50

    简单的代码,在百度回答的时候顺便写下,可能有人会重复的问到

    添加一个Form,Paste如下代码即可看效果

     

    Option Explicit Private Type MATRIX nRowCount As Long nColCount As Long pMatData() As Double End Type Private Sub Form_Load() '[11 12 13] '[21 22 23] '[31 32 33] Dim d As Double, i As Long, j As Long Dim m As MATRIX, s As String s = "11 12 13" & vbCrLf & "21 22 23" & vbCrLf & "31 32 33" InitMatrix m, s, 3, 3, Asc(vbCrLf), Asc(" ") d = GetMaxItem(m, i, j) MsgBox "最大值: " & CStr(d) & " 位置是 第 " & CStr(i) & " 行,第 " & CStr(j) & " 列." & vbCrLf & "矩阵原始数据:" & vbCrLf & s & vbCrLf 'End End Sub Private Sub InitMatrix(lpMatrix As MATRIX, lpString As String, ByVal nRow As Long, ByVal nCol As Long, Optional ByVal RowChar As Long = &HD, Optional ByVal ColChar = &H20) Dim strLine() As String, strCol() As String Dim cRow As String * 1, cCol As String * 1 Dim i As Long, j As Long Dim n As Long If nRow * nCol = 0 Then Exit Sub lpMatrix.nRowCount = nRow lpMatrix.nColCount = nCol ReDim lpMatrix.pMatData(0 To nRow - 1, 0 To nCol - 1) If lpString = "" Then Exit Sub If RowChar * ColChar = 0 Then Exit Sub cRow = Chr(RowChar) cCol = Chr(ColChar) strLine = Split(lpString, cRow) n = UBound(strLine) On Local Error Resume Next For i = 0 To n strCol = Split(strLine(i), cCol) For j = 0 To UBound(strCol) lpMatrix.pMatData(i, j) = strCol(j) Next j Next i End Sub Private Function GetMaxItem(lpMatrix As MATRIX, nRow As Long, nCol As Long) As Double Dim i As Long, j As Long Dim d As Double For i = 0 To lpMatrix.nRowCount - 1 For j = 0 To lpMatrix.nColCount - 1 If lpMatrix.pMatData(i, j) > d Then d = lpMatrix.pMatData(i, j) nRow = i + 1 nCol = j + 1 End If Next j Next i GetMaxItem = d End Function


    最新回复(0)