【问题标题】:How can I find the average of all numbers in a textfile如何找到文本文件中所有数字的平均值
【发布时间】:2016-04-29 11:24:25
【问题描述】:

我可笑地坚持这一点。我下面的代码总结了文本文件Dailyfile 中的所有数字,并将总数输出到AverageFile。问题是我不想总结。我希望它找出所有数字中的average

我该怎么做?

Dim AverageFile As String = "C:\xxx\zzz\" & System.DateTime.Now.ToString("yyyyMMdd") & ".txt"
Dim DailyFile As String = "C:\xxx\xxx\" & System.DateTime.Now.ToString("yyyyMMdd") & ".txt"  

            Try
                If System.IO.File.Exists(AverageFile) Then
                    Dim total As double = 0
                    For Each line As String In IO.File.ReadAllLines(DailyFile)

                        total += Double.Parse(line)
                    Next
                    Dim objWriter As New System.IO.StreamWriter(AverageFile, false)
                    objWriter.WriteLine(total.ToString) 
                    objWriter.Close()
                Else
                    'Nothing yet
                End If

            Catch ex As Exception
                lbErrors.Items.Add(String.Concat(TimeOfDay & " Error 98: File or folder might not exist. Restart application... ", ex.Message))
            End Try

Dailyfile 看起来像这样;

我在total 0= double.parse(line) 上尝试了很多变体,因为我觉得这就是问题所在。我也试过diming the total as integer = 0。我是计算新手,所以我不知道情况如何。

【问题讨论】:

  • 试试File.ReadAllLines(path).Select(double.Parse).Average()

标签: vb.net average


【解决方案1】:

平均值只是总数除以您总结的数量。 (假设你想使用arithmetic mean,这可能就是你要找的。)

Dim total As double = 0
Dim numOfLines As Integer = 0
For Each line As String In IO.File.ReadAllLines(DailyFile)
    numOfLines += 1
    total += Double.Parse(line)
Next
Dim average As Double = total / numOfLines
Dim objWriter As New System.IO.StreamWriter(AverageFile, false)
objWriter.WriteLine(average.ToString) 
objWriter.Close()

您的代码中缺少的只是跟踪行数并将总和除以该数字。


举个例子:我们是 3 个人。我23岁,你35岁,我们的朋友40岁。我们的平均年龄为(23 + 35 + 40) / 3,即 32.666...

【讨论】:

  • 谢谢,CherryDT。这确实正是我所缺少的。
  • 这个有点晚了,但是对此有什么小的调整,所以我只得到例如 32 作为输出而不是 32.666?
  • 如果你真的想总是向下取整(就像你的例子),你可以在最后使用Math.floor(average)而不是average(例如objWriter.WriteLine(Math.floor(average).ToString))。如果您想使用银行家四舍五入(即 32.4 将变为 32,但 32.6 将变为 33),请使用 Math.round(average, 0)
【解决方案2】:

要么使用CherryDT's approach 计算行数并将总数除以该数字,要么使用 LINQ 的 Enumerable.Average,例如使用以下简洁查询:

Dim allNumbers = From line In IO.File.ReadLines(DailyFile)
                 Let num = line.TryGetDouble()
                 Where num.HasValue
                 Select num.Value
Dim average As Double = allNumbers.Average()

我使用以下extension method 尝试将字符串解析为Nullable(Of Double)

Imports System.Runtime.CompilerServices

Module StringExtensions
    <Extension()>
    Public Function TryGetDouble(ByVal str As String) As Nullable(Of Double)
        If str Is Nothing Then Return Nothing
        Dim d As Double
        If Double.TryParse(str.Trim(), d) Then
            Return d
        Else
            Return Nothing
        End If
    End Function
End Module

【讨论】:

  • 感谢您的回答并详细说明,蒂姆。我正在抓住你交给我的知识,我会用它来探索可能性。
猜你喜欢
  • 1970-01-01
  • 2018-09-15
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 2021-12-13
  • 2016-05-05
  • 1970-01-01
相关资源
最近更新 更多