我有每個月多個的.csv文件當中去,如:從讀和操作.csv文件
01/04/2012,00:00,7.521527,80.90972,4.541667,5.774305,7,281.368
02/04/2012,00:00,8.809029,84.59028,6.451389,5.797918,7,274.0764
03/04/2012,00:00,4.882638,77.86806,1.152778,15.13611,33,127.6389
04/04/2012,00:00,5.600694,50.35417,-3.826389,15.27222,33,40.05556
的格式是:日期形式DD/MM/YY,當前時間,當前溫度顯示,當前溼度,當前露點,當前風速,電流陣風,當前風軸承
程序需要計算平均爲 溫度 溼度 風速 風向
並將它們顯示在文本框上。
有什麼想法?
這是我迄今所做的......
Option Strict On
Option Explicit On
Imports System.IO
Imports System
Public Class Form1
Private Sub cmb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb1.SelectedIndexChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
Me.Close()
End Sub
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndata.Click
'This is for August
If cmb1.SelectedIndex = 1 Then
TextBox1.Clear()
Using reader As New StreamReader("c://temp/DailyAug12log.csv")
Dim line As String = reader.ReadLine()
Dim avgTemp As Integer
Dim fields() As String = line.Split(",".ToCharArray())
Dim fileDate = CDate(fields(0))
Dim fileTime = fields(1)
Dim fileTemp = fields(2)
Dim fileHum = fields(3)
Dim fileWindSpeed = fields(4)
Dim fileWindGust = fields(5)
Dim fileWindBearing = fields(6)
While line IsNot Nothing
counter = counter + 1
line = reader.ReadLine()
End While
avgTemp = CInt(fields(2))
avgTemp = CInt(CDbl(avgTemp/counter))
TextBox1.Text = TextBox1.Text & "Month = August" & vbCrLf & "Temperature Average: " & avgTemp & vbCrLf
End Using
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim files() As String
files = Directory.GetFiles("C:\Temp", "*.csv", SearchOption.AllDirectories)
For Each FileName As String In files
cmb1.Items.Add(FileName.Substring(FileName.LastIndexOf("\") + 1, FileName.Length - FileName.LastIndexOf("\") - 1))
Next
End Sub
End Class
您需要另一個變量來跟蹤您讀過的行數。每行增加一個。然後用行數除「total」變量並用計算出的值更新文本框。你可以聲明其他「總」變量來總結其他測量結果...... – 2013-05-07 02:27:18
感謝我的工作!任何想法如何添加此?謝謝! – yolad 2013-05-07 03:39:21
嚴重嗎?...以與「總」變量相同的位置/方式聲明線的計數器。它將是Integer類型。將該變量在與累積總變量相同的地方增加1。你認爲你應該計算while循環內部還是外部的平均值?平均使用哪種數據類型?整數還是雙精度? – 2013-05-07 04:10:20