Holey_yan

[VB.NET] ValueCounter

May 13th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.24 KB | None | 0 0
  1. Imports System.Windows.Forms
  2.  
  3. Module Module1
  4.  
  5.     ' 源路徑
  6.     Private sourcePath As String = Application.StartupPath & "\SourceTxt.txt"
  7.     ' 目標路徑
  8.     Private targetPath As String = Application.StartupPath & "\TargetTxt.txt"
  9.  
  10.     Sub Main()
  11.         ' 初始化訊息
  12.         AppTitle() : Console.WriteLine() : OutputMessage()
  13.  
  14.         ' 紀錄開始時間與程序結束時間
  15.         Dim startTime As DateTime = DateTime.Now,
  16.             overTime As DateTime = DateTime.Now
  17.  
  18.         ' 刪除舊有檔案
  19.         LogRecoder("Delete old file")
  20.         If My.Computer.FileSystem.FileExists(targetPath) Then My.Computer.FileSystem.DeleteFile(targetPath)
  21.  
  22.         ' 建立紀錄陣列與讀取檔案
  23.         LogRecoder("Create array and read file")
  24.         Dim numArray(2000000) As Long
  25.         Dim comment As String = My.Computer.FileSystem.ReadAllText(sourcePath)
  26.  
  27.         ' 循序讀取數值並記錄至陣列內
  28.         LogRecoder("Read all data form file")
  29.         Dim minValue As Long = Long.MaxValue, ' 數值範圍下限
  30.             maxValue As Long = 0 ' 數值範圍上限
  31.         For Each line As String In comment.Split(vbNewLine)
  32.             If String.Equals(line, vbCr) Or String.Equals(line, vbLf) Then Continue For
  33.             For Each number As String In line.Split(", ")
  34.  
  35.                 Dim numberValue As Long = Integer.Parse(number)
  36.                 numArray(numberValue) += 1
  37.  
  38.                 If numberValue < minValue Then
  39.                     minValue = numberValue
  40.                 End If
  41.  
  42.                 If numberValue > maxValue Then
  43.                     maxValue = numberValue
  44.                 End If
  45.             Next
  46.         Next
  47.  
  48.         ' 搜尋
  49.         LogRecoder("Search the value of max count")
  50.         Dim getMaxValue As Long = 0,
  51.             maxValueCount As Long = 0
  52.         For i As Long = minValue To maxValue Step 1
  53.             If (numArray(i) > maxValueCount) Then
  54.                 getMaxValue = i
  55.                 maxValueCount = numArray(i)
  56.             End If
  57.         Next : LogRecoder(String.Format("MaxValue : {0}, Counter : {1}", getMaxValue, maxValueCount))
  58.  
  59.         '' 輸出為檔案
  60.         'LogRecoder("Write the array to file")
  61.         'For i As Long = 0 To 2000000 Step 1
  62.         '    My.Computer.FileSystem.WriteAllText(targetPath,
  63.         '                                        i.ToString & " : " & numArray(i) & vbNewLine,
  64.         '                                        True)
  65.         'Next : My.Computer.FileSystem.WriteAllText(targetPath, String.Format("MaxValue : {0}, Counter : {1}", getMaxValue, maxValueCount), True)
  66.  
  67.         ' 程序結束
  68.         overTime = DateTime.Now
  69.         LogRecoder(String.Format("Process exited after {0} seconds", Format(Math.Ceiling((overTime.Ticks - startTime.Ticks) / 1000 / 100) / 100, "0.00")))
  70.         LogRecoder("Press any key to continue ..")
  71.         Console.ReadKey()
  72.     End Sub
  73.  
  74.     Private Function LogRecoder(ByVal [Context] As String) As Boolean
  75.         Try
  76.             Console.ForegroundColor = ConsoleColor.Green
  77.             Console.WriteLine(Format(Date.Now, "yyyy/MM/dd hh:mm:ss") + vbTab + [Context])
  78.             Return True
  79.         Catch ex As Exception
  80.             Return False
  81.         End Try
  82.     End Function
  83.  
  84. End Module
Advertisement
Add Comment
Please, Sign In to add comment