7,762
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Private Sub Command1_Click()
Dim i As Long, point_index As Long, rectangle_index As Long, num_line As Integer, shape_num As Integer
Dim strLine As String, strType As String
Dim x As Double, y As Double, X1 As Double, X2 As Double, Y1 As Double, Y2 As Double
Dim a() As Double, q() As Double, b() As Double, L() As Double
Dim x_tmp() As Double, y_tmp() As Double
Open "c:\test\1.txt" For Input As #1
Open "c:\test\2.txt" For Output As #2
point_index = -1
rectangle_index = -1
Do Until EOF(1)
Line Input #1, strLine
If InStr(strLine, "实体: 圆") Then
Print #2, strLine
strType = "Circle"
point_index = point_index + 1
ElseIf InStr(strLine, "实体: 直线") Then
strType = "Line"
shape_num = Val(Mid(strLine, 2))
End If
If strType = "Circle" And InStr(strLine, "圆心") Then
Print #2, strLine
Call Get_Coordinate(strLine, x, y)
ReDim Preserve x_tmp(point_index)
x_tmp(point_index) = x
ReDim Preserve y_tmp(point_index)
y_tmp(point_index) = y
List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
List1.ItemData(List1.NewIndex) = point_index
End If
If strType = "Circle" And InStr(strLine, "半径") Then
Print #2, strLine & vbCrLf
End If
If strType = "Line" And InStr(strLine, "起点") Then
num_line = num_line + 1
Call Get_Coordinate(strLine, x, y)
If num_line = 1 Then
point_index = point_index + 1
ReDim Preserve x_tmp(point_index)
x_tmp(point_index) = x
ReDim Preserve y_tmp(point_index)
y_tmp(point_index) = y
List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
List1.ItemData(List1.NewIndex) = point_index
X1 = x
Y1 = y
ElseIf num_line = 2 Then
point_index = point_index + 1
ReDim Preserve x_tmp(point_index)
x_tmp(point_index) = x
ReDim Preserve y_tmp(point_index)
y_tmp(point_index) = y
List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
List1.ItemData(List1.NewIndex) = point_index
Y2 = y
ElseIf num_line = 3 Then
point_index = point_index + 1
ReDim Preserve x_tmp(point_index)
x_tmp(point_index) = x
ReDim Preserve y_tmp(point_index)
y_tmp(point_index) = y
List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
List1.ItemData(List1.NewIndex) = point_index
X2 = x
ElseIf num_line = 4 Then
point_index = point_index + 1
ReDim Preserve x_tmp(point_index)
x_tmp(point_index) = x
ReDim Preserve y_tmp(point_index)
y_tmp(point_index) = y
List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
List1.ItemData(List1.NewIndex) = point_index
rectangle_index = rectangle_index + 1
ReDim Preserve b(rectangle_index)
b(rectangle_index) = Abs(Y1 - Y2)
ReDim Preserve L(rectangle_index)
L(rectangle_index) = Abs(X1 - X2)
Print #2, "第 " & shape_num - 3 & " 个实体:方形"
Print #2, Space(4) & "宽度=" & Format(b(rectangle_index), "0.000")
Print #2, Space(4) & "长度=" & Format(L(rectangle_index), "0.000") & vbCrLf
num_line = 0
End If
End If
Loop
Close #2
Close #1
ReDim a(List1.ListCount - 1)
ReDim q(List1.ListCount - 1)
For i = 0 To List1.ListCount - 1
a(i) = x_tmp(List1.ItemData(i))
q(i) = y_tmp(List1.ItemData(i))
Next i
End Sub
Private Sub Get_Coordinate(ByVal strLine As String, ByRef x As Double, ByRef y As Double)
Dim p As Integer
p = InStr(strLine, "X=")
If p Then
x = Val(Mid(strLine, p + 2))
p = InStr(strLine, "Y=")
If p Then y = Val(Mid(strLine, p + 2))
Else
Exit Sub
End If
End Sub
方型的四条直线必须连续,不得与其他方型的直线交错。
四条直线的画线顺序,必须按例子中的顺序。否则要有更多的判断语句,你可以自己加。
此例中,方形的宽度专指 Y 方向高度,长度指 X 方向长度。
输出文件如下:第 1 个实体: 圆
圆心: X=249.287 Y=144.405
半径: R=21.047
第 2 个实体: 圆
圆心: X=196.221 Y=46.844
半径: R=30.660
第 3 个实体:方型
宽度=36.856
长度=83.390
第 7 个实体: 圆
圆心: X=374.913 Y=280.990
半径: R=40.783
坐标点的排序: 196.221 46.844
249.287 144.405
315.349 135.733
315.349 172.589
398.739 135.733
398.739 172.589
374.913 280.990