Explorar o código

Create 使用字典和排序计算行列.bas

蘭雅sRGB %!s(int64=2) %!d(string=hai) anos
pai
achega
9abb8eb6e9
Modificáronse 1 ficheiros con 47 adicións e 0 borrados
  1. 47 0
      代码练习/使用字典和排序计算行列.bas

+ 47 - 0
代码练习/使用字典和排序计算行列.bas

@@ -0,0 +1,47 @@
+Private Type Coordinate
+    x As Double
+    y As Double
+End Type
+
+Sub 计算行列()   ' 字典使用计算行列
+
+  ActiveDocument.Unit = cdrMillimeter
+  Set xdict = CreateObject("Scripting.dictionary")
+  Set ydict = CreateObject("Scripting.dictionary")
+  Dim dot As Coordinate
+  Dim s As Shape, ssr As ShapeRange
+  Set ssr = ActiveSelectionRange
+  
+  For Each s In ssr
+    dot.x = s.CenterX: dot.y = s.CenterY
+    If xdict.Exists(Int(dot.x)) = False Then xdict.Add Int(dot.x), dot.x
+    If ydict.Exists(Int(dot.y)) = False Then ydict.Add Int(dot.y), dot.y
+  Next s
+  
+  Dim keys() As Variant
+  keys = xdict.keys
+  ' 使用 Sort 函数对数组进行排序
+  ArraySort keys
+  ' 遍历排序后的键,并按照键的顺序访问字典中的元素
+  Dim key As Variant
+  For Each key In keys
+      Debug.Print key, xdict(key)
+  Next key
+
+  Debug.Print "字典使用计算行列:" & xdict.Count, ydict.Count
+  
+End Sub
+
+'// 对数组进行排序[单维]
+Public Function ArraySort(src As Variant) As Variant
+  Dim out As Long, i As Long, tmp As Variant
+  For out = LBound(src) To UBound(src) - 1
+    For i = out + 1 To UBound(src)
+      If src(out) > src(i) Then
+        tmp = src(i): src(i) = src(out): src(out) = tmp
+      End If
+    Next i
+  Next out
+  
+  ArraySort = src
+End Function