|
我只会将数据转成3楼的那种结构,反向转换不会;
- Option Explicit
- '一般BOM的展开,暂时不清楚具体数据结构
- '默认数据会根据层级不断向后延展
- '默认数据总是有序排序(如果数据是无序排序,可以用字典以便记录唯一性)
- Private Type Node
- Child As Variant
- Parent As Integer
- Data As String
- Level As Integer
- End Type
- Public arrNode() As Node, iNode&
- Public SS_Level(), SS_Top&
- Public Dic As Object, iDic&
- Public arrOut(), iOut&
- Const Root = 0
- Sub BOM_Extend()
- Dim arr
- Dim i&, j&
- '--------------------------
- '初始化数据
- arr = Sheet1.UsedRange
- ReDim arrNode(0 To 50)
- ReDim SS_Level(0 To UBound(arr, 2)) '堆栈层级
- Set Dic = CreateObject("Scripting.Dictionary")
- ReDim arrOut(1 To 50, 1 To UBound(arr, 2))
- '--------------------------
- iNode = Root
- arrNode(iNode).Data = "Root"
- arrNode(iNode).Level = Root
- 'ReDim arrNode(iNode).Child(0)
- 'Dic("Root") = iNode
- SS_Top = Root
- SS_Level(SS_Top) = "Root"
- For i = 1 To UBound(arr)
- 'If i = 9 Then Stop
- j = 1
- Do While j <= UBound(arr, 2)
- If j = 0 Then j = j + 1
- '边界
- If SS_Top > j Then
- '栈内层>BOM层
- j = j + 1
- 'BOM层后移一位
- ElseIf SS_Top = j Then
- '层级相同
- If SS_Level(SS_Top) = arr(i, j) Then
- '栈=BOM
- j = j + 1
- 'BOM层后移一位
- ElseIf SS_Level(SS_Top) <> arr(i, j) Then
- '内容不同
- SS_Pop
- '出栈
- j = j - 1
- End If
- ElseIf SS_Top < j Then
- '栈内层<BOM层
- SS_Push arr(i, j)
- '压入栈
- NodeIn LongFatherChain(SS_Top - 1), arr(i, j)
- '数据记录结点
- End If
- Loop
- Next i
- 'Stop
- '============================================
- Node2Sheet (Root)
- Sheet2.UsedRange.ClearContents
- Sheet2.[A1].Resize(UBound(arrOut), UBound(arrOut, 2)) = arrOut
- '-------------------------------------------
- ReleaseMemory
- Erase arr
- End Sub
- Sub Node2Sheet(n)
- 'n为Node信息
- Dim i As Long
- iOut = iOut + 1
- arrOut(iOut, 1) = arrNode(n).Level
- If arrNode(n).Parent = 0 Then
- '根结点的父节点问题
- arrOut(iOut, 2) = 0
- Else
- arrOut(iOut, 2) = arrNode(arrNode(n).Parent).Data
- End If
- arrOut(iOut, 3) = arrNode(n).Data
- If IsEmpty(arrNode(n).Child) Then Exit Sub
- For i = 1 To UBound(arrNode(n).Child)
- Node2Sheet (arrNode(n).Child(i))
- Next i
- End Sub
- Function LongFatherChain(a) As String
- 'SK表示为长数据关系链
- Dim i&
- For i = 0 To a
- LongFatherChain = LongFatherChain & SS_Level(i)
- Next i
- End Function
- Sub NodeIn(f, d)
- 'f=father d=data
- Dim i As Long
- If Not Dic.Exists(f) Then
- Dic(f) = iNode
- '记录数据地址
- ReDim arrNode(iNode).Child(0)
- End If
- iNode = iNode + 1
- ReDim Preserve arrNode(Dic(f)).Child(0 To UBound(arrNode(Dic(f)).Child) + 1)
- arrNode(Dic(f)).Child(UBound(arrNode(Dic(f)).Child)) = iNode
- arrNode(iNode).Parent = Dic(f)
- arrNode(iNode).Data = d
- arrNode(iNode).Level = SS_Top
- End Sub
- '数据入栈
- Sub SS_Push(Data)
- If SS_Top + 1 > UBound(SS_Level) Then
- 'If SS_IsFull(SS_List) Then
- MsgBox ("Stack is Full")
- Exit Sub
- Else
- SS_Top = SS_Top + 1 '指针上移
- SS_Level(SS_Top) = Data '数据读入
- End If
- End Sub
- '数据出栈
- Sub SS_Pop()
- If SS_Top = 0 Then
- MsgBox ("Stack is Empty")
- Exit Sub
- Else
- SS_Level(SS_Top) = ""
- SS_Top = SS_Top - 1
- End If
- End Sub
- Sub ReleaseMemory()
- Dic.RemoveAll: Set Dic = Nothing
- Erase arrNode(): iNode = 0
- Erase SS_Level: SS_Top = 0
- Erase arrOut: iOut = 0
- End Sub
复制代码
|
|