我仍在研究這些東西,并一直在瀏覽 google 和 Youtube,但 VB.net 似乎沒有這個問題,但我在 python 或 Java 上看到了一些。這是我必須得到的輸出,但是當我閱讀文本檔案并嘗試找到它的頻率時,它并不像希望的那樣
這是我的代碼。
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim reader As TextReader = New StreamReader("number.txt")
Dim num As Integer = 0
For Each item In reader.ReadToEnd
If item.CompareTo(reader.ReadLine) = True Then
num = 1
End If
Next
rtbshow.Text = "Digit" & " " & " " & " Frequency " & vbNewLine & reader.ReadToEnd() & " " & " " & num
End Sub
End Class
uj5u.com熱心網友回復:
ADictionary
提供了一個方便的物體來存盤 (digit, count_of_digit) 資料。
然后你只需要遍歷檔案中的所有字符并檢查每個字符是否為數字。如果是,則將該數字的 count_of_digit 加一。
這是使用控制臺應用程式的示例:
Imports System.IO
Module Module1
Sub CreateTestFile(s As String)
Dim rand As New Random()
Using sw As New StreamWriter(s)
For i = 1 To 10000
sw.Write(rand.Next(0, 10).ToString())
Next
End Using
End Sub
Function GetNumberFrequencies(s As String) As Dictionary(Of Integer, Integer)
Dim nf As New Dictionary(Of Integer, Integer)
For i = 0 To 9
nf.Add(i, 0)
Next
Using sr As New StreamReader(s)
While Not sr.EndOfStream
Dim line = sr.ReadLine()
For Each c In line
If c >= "0" AndAlso c <= "9" Then
' A quick way to convert the strings "0"-"9" to the numbers 0-9:
Dim index = AscW(c) - AscW("0)")
nf(index) = 1
End If
Next
End While
End Using
Return nf
End Function
Sub Main()
' Always specify a full path to a file that you use.
Dim testFile = "C:\temp\randNums.txt"
CreateTestFile(testFile)
Dim nf = GetNumberFrequencies(testFile)
For Each kvp As KeyValuePair(Of Integer, Integer) In nf
Console.WriteLine(kvp.Key & " - " & kvp.Value)
Next
Console.ReadLine()
End Sub
End Module
示例輸出:
0 - 1013
1 - 963
2 - 991
3 - 1033
4 - 1001
5 - 966
6 - 962
7 - 1006
8 - 1018
9 - 1047
要在表單上使用 GetNumberFrequencies 函式,您可以:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim testFile = "C:\temp\randNums.txt"
Dim nf = GetNumberFrequencies(testFile)
Dim sb As New Text.Stringbuilder("Digit Frequency" & VbNewLine)
For Each kvp As KeyValuePair(Of Integer, Integer) In nf
sb.Append(kvp.Key & " " & kvp.Value & VbNewLine)
Next
rtbshow.Text = sb.ToString()
End Sub
uj5u.com熱心網友回復:
字典可以解決這個問題,但如果只有九位數字,索引陣列也可以這樣做,而且速度可能更快。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim digitCounts = Enumerable.Repeat(0,10).ToArray()
Dim digits = File.ReadAllText("number.txt").
Where(Function(c) Char.IsDigit(c)).
Select(Function(c) Asc(c) - Asc("0"c))
For Each d As Integer In digits
digitCounts(d) = 1
Next
Dim result As New StringBuilder($"Digit{vbTab}Frequency{vbCrLf}")
For i As Integer = 0 To 9
result.AppendLine($"{i,3}{vbTab}{digitCounts(i),7}")
Next
rtbshow.Text = result.ToString()
End Sub
您可以(有點)看到它在這里作業,除了我無法使用 .Net fiddle 進行字串插值:
https://dotnetfiddle.net/FMWnMg
我們也可以通過一個GroupBy()
操作來解決這個問題:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim result As New StringBuilder($"Digit{vbTab}Frequency{vbCrLf}")
Dim digits = File.ReadAllText("number.txt").
Where(Function(c) Char.IsDigit(c)).
OrderBy(Function(d) d).
GroupBy(Function(d) d,
Function(d, g) $"{d,3}{vbTab}{g.Count(),7}{vbCrLf}"
)
For Each d As String in digits
result.Append(d)
Next
Console.WriteLine(result.ToString()
End Sub
看到它在這里作業:
https://dotnetfiddle.net/Xxl2z3
請注意,這最后會跳過丟失的數字。
uj5u.com熱心網友回復:
File.ReadAllText
以字串形式回傳檔案中的所有內容。接下來我們宣告一些變數來保存每個數字的出現次數。String
.net 中的A也是Char
. 我們可以通過使用For Each
回圈來逐個檢查檔案中的每個字符來利用這一點。首先,使用Char
類的方法,我們檢查IsDigit
. 如果True
我們選擇Integer
要使用 a 遞增的變數Select Case
。
Zero = 1
是一種快捷的書寫方式
零 = 零 1
最后,我們撰寫字串來顯示結果。在$
在前面的String
裝置是一個內插字串。您可以將變數直接插入由大括號 , 包圍的字串中{ }
。這取代了String.Format
或令人眼花繚亂的引號和&符號。在 Visual Studio 的最新版本中,您可以按 Enter 指示新行,而無需輸入任何新行字符。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FileContents = File.ReadAllText("numbers.txt")
Dim Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine As Integer
For Each c In FileContents
If Char.IsDigit(c) Then
Select Case c
Case "0"c
Zero = 1
Case "1"c
One = 1
Case "2"c
Two = 1
Case "3"c
Three = 1
Case "4"c
Four = 1
Case "5"c
Five = 1
Case "6"c
Six = 1
Case "7"c
Seven = 1
Case "8"c
Eight = 1
Case "9"c
Nine = 1
End Select
End If
Next
Dim DisplayResults = $"0 {Zero}
1 {One}
2 {Two}
3 {Three}
4 {Four}
5 {Five}
6 {Six}
7 {Seven}
8 {Eight}
9 {Nine}"
MessageBox.Show(DisplayResults)
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/377602.html