注册 登录
编程论坛 VB6论坛

vb 将计算出的数据添加到txt文件中

xyzmrsir 发布于 2016-04-15 11:29, 2135 次点击
Private Sub Form_Click()
Dim reg As New RegExp
reg.Pattern = "[\d\.]+"
reg.Global = True: reg.IgnoreCase = True: reg.MultiLine = True
Dim textline As String
Dim temp() As String  '第n行的x和y
Dim temp1() As String  '第n+1行的x和y
Dim temp2() As String  '第n行的时间
Dim temp3() As String  '第n+1行的时间
Dim distance() As String
Dim timeInterval() As String
Dim speed() As String
Open "e:\data.txt" For Input As #1

ReDim temp(1)
ReDim temp1(0)
ReDim temp2(2)
ReDim temp3(0)
ReDim distance(0)
ReDim timeInterval(0)
ReDim speed(0)
Do While Not EOF(1)
Line Input #1, textline
temp(0) = reg.Execute(textline)(3)    'x
temp(1) = reg.Execute(textline)(4)    'y

If (UBound(temp1) >= 1) Then

distance(0) = (temp(0) - temp1(0)) ^ 2 + (temp(1) - temp1(1)) ^ 2

Print textline, distance(0)
End If
ReDim temp1(1)
temp1(0) = reg.Execute(textline)(3)
temp1(1) = reg.Execute(textline)(4)
Loop
Close #1
End Sub

Private Sub Form_Load()

End Sub

计算结果如下:
只有本站会员才能查看附件,请 登录


在要保留原数据(前三列)的情况下,怎么把计算出来的结果distance(最后一列是计算结果distance) ,写入txt的最后一列???????

txt中原数据如下:
14:04:16     31.0042     121.19409
14:04:57     31.004078     121.191348
14:05:08     31.00401     121.190088
14:05:25     31.0039     121.187448
14:05:28     31.003878     121.18694
14:05:39     31.00384     121.18541

最终想要的txt中的数据格式是:
14:04:16     31.0042     121.19409    7.53344799998821E-06(手工录入最后一列,哭)
14:04:57     31.004078     121.191348    1.59222400000498E-06(手工录入最后一列,哭)
14:05:08     31.00401     121.190088    6.98169999999739E-06(手工录入最后一列,哭)
14:05:25     31.0039     121.187448    2.5854799999964E-06(手工录入最后一列,哭)
14:05:28     31.003878     121.18694    2.34234400000757E-06(手工录入最后一列,哭)
14:05:39     31.00384     121.18541   

在要保留原数据(前三列)的情况下,怎么把计算出来的结果distance(最后一列是计算结果distance) ,写入txt的最后一列???????
3 回复
#2
风吹过b2016-04-15 17:39
Open "e:\data.txt" For Input As #1
Open "e:\tmp.txt" For output As #2
ReDim temp(1)
ReDim temp1(0)
ReDim temp2(2)
ReDim temp3(0)
ReDim distance(0)
ReDim timeInterval(0)
ReDim speed(0)
Do While Not EOF(1)
Line Input #1, textline
temp(0) = reg.Execute(textline)(3)    'x
temp(1) = reg.Execute(textline)(4)    'y

If (UBound(temp1) >= 1) Then

distance(0) = (temp(0) - temp1(0)) ^ 2 + (temp(1) - temp1(1)) ^ 2

Print textline, distance(0)
Print #2,textline, distance(0)
End If
ReDim temp1(1)
temp1(0) = reg.Execute(textline)(3)
temp1(1) = reg.Execute(textline)(4)
Loop
Close #1
Close #2

未经测试,你自己试试。
#3
xiangyue05102016-04-16 13:22
楼上说的确实是实情,一方面现在网络比较发达,很多的问题可以找到答案。来问的人可能就不多了
另外一方面也是有些会员实在过分,很简单的问题不去开动脑筋,思路都告诉了,就是说不会,只会伸手。 搞的别人不愿意去帮。
但是想楼主这样的,确实自己写了代码,调试不出来的,我们都是还是愿意帮忙的。毕竟谁都有刚开始学习懵懵懂懂的那个阶段。
1