Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Windows.Forms
- Module Module1
- ' 源路徑
- Private sourcePath As String = Application.StartupPath & "\SourceTxt.txt"
- ' 目標路徑
- Private targetPath As String = Application.StartupPath & "\TargetTxt.txt"
- Sub Main()
- ' 初始化訊息
- AppTitle() : Console.WriteLine() : OutputMessage()
- ' 紀錄開始時間與程序結束時間
- Dim startTime As DateTime = DateTime.Now,
- overTime As DateTime = DateTime.Now
- ' 刪除舊有檔案
- LogRecoder("Delete old file")
- If My.Computer.FileSystem.FileExists(targetPath) Then My.Computer.FileSystem.DeleteFile(targetPath)
- ' 建立紀錄陣列與讀取檔案
- LogRecoder("Create array and read file")
- Dim numArray(2000000) As Long
- Dim comment As String = My.Computer.FileSystem.ReadAllText(sourcePath)
- ' 循序讀取數值並記錄至陣列內
- LogRecoder("Read all data form file")
- Dim minValue As Long = Long.MaxValue, ' 數值範圍下限
- maxValue As Long = 0 ' 數值範圍上限
- For Each line As String In comment.Split(vbNewLine)
- If String.Equals(line, vbCr) Or String.Equals(line, vbLf) Then Continue For
- For Each number As String In line.Split(", ")
- Dim numberValue As Long = Integer.Parse(number)
- numArray(numberValue) += 1
- If numberValue < minValue Then
- minValue = numberValue
- End If
- If numberValue > maxValue Then
- maxValue = numberValue
- End If
- Next
- Next
- ' 搜尋
- LogRecoder("Search the value of max count")
- Dim getMaxValue As Long = 0,
- maxValueCount As Long = 0
- For i As Long = minValue To maxValue Step 1
- If (numArray(i) > maxValueCount) Then
- getMaxValue = i
- maxValueCount = numArray(i)
- End If
- Next : LogRecoder(String.Format("MaxValue : {0}, Counter : {1}", getMaxValue, maxValueCount))
- '' 輸出為檔案
- 'LogRecoder("Write the array to file")
- 'For i As Long = 0 To 2000000 Step 1
- ' My.Computer.FileSystem.WriteAllText(targetPath,
- ' i.ToString & " : " & numArray(i) & vbNewLine,
- ' True)
- 'Next : My.Computer.FileSystem.WriteAllText(targetPath, String.Format("MaxValue : {0}, Counter : {1}", getMaxValue, maxValueCount), True)
- ' 程序結束
- overTime = DateTime.Now
- LogRecoder(String.Format("Process exited after {0} seconds", Format(Math.Ceiling((overTime.Ticks - startTime.Ticks) / 1000 / 100) / 100, "0.00")))
- LogRecoder("Press any key to continue ..")
- Console.ReadKey()
- End Sub
- Private Function LogRecoder(ByVal [Context] As String) As Boolean
- Try
- Console.ForegroundColor = ConsoleColor.Green
- Console.WriteLine(Format(Date.Now, "yyyy/MM/dd hh:mm:ss") + vbTab + [Context])
- Return True
- Catch ex As Exception
- Return False
- End Try
- End Function
- End Module
Advertisement
Add Comment
Please, Sign In to add comment