簡體   English   中英

VB.NET .txt文件操作

[英]VB.NET .txt file manipulation

當前正在學校項目上工作,我們必須編碼(在vb.net中)一個程序,該程序讀取具有不同點的x和y坐標的txt文件,並在每兩個點之間進行距離計算,並在文件中覆蓋距離。

txt文件看起來像這樣:

596;226  
506;179  
412;298  
583;291   
...etc

所以我的目標是計算dx和dy(每2條線的坐標之間的差),這樣我就可以得到距離。

唯一的問題是我被困在如何獲得第1行和第2行的dx的dx和dy(現在為7天),是(506-596),第2行是(412-506),以此類推。也是

這是我一直徒勞的嘗試(大多數代碼只是從網絡復制/粘貼)。

Imports System.IO

Public Class Form1


Private Sub FichierTexteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FichierTexteToolStripMenuItem.Click

    OpenFileDialog1.Filter = "fichier texte| *.txt"
    Dim nbLigne As Integer
    nbLigne = 0
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim sr As StreamReader
        sr = New StreamReader(OpenFileDialog1.FileName)
        Dim x, y As String
        Dim dx, dy As String
        Dim txtTotal As Object
        Dim ligne = sr.ReadLine()
        Dim tabl() As Object
        tabl = Split(ligne, ";") 'la taille de tableau represente le nombre de bloc qui ksont séparer par les separateur
        txtTotal = ligne + vbCrLf


        'While Not ligne Is Nothing
        '    ligne = sr.ReadLine
        '    txtTotal = txtTotal + ligne + vbCrLf

        '    tabl = Split(ligne, ";") 'la taille de tableau represente le nombre de bloc qui ksont séparer par les separateur
        '    nbLigne = nbLigne + 1
        'End While
        x = tabl(0)
        y = tabl(1)

        While Not ligne Is Nothing
            ligne = sr.ReadLine

            tabl = Split(ligne, ";")
            dx = tabl(0) - x
            dy = tabl(1) - y

        End While


        'test
        Label2.Text = dx
        Label3.Text = dy

        'Label1.Text = Calculs.distance(dx, dy)

    Else : Close()

    End If

End Sub



End Class

謝謝,希望有人能讓我擺脫這個問題:)

你應該聲明

Dim tabl() As String

由於函數Split返回一個String數組。 您不能使用字符串進行數學計算。 因此,您必須將所有字符串轉換為數字。 例如Double

dxdy必須是數字

Dim dx = Double.Parse(tabl(0)) - Double.Parse(x)
Dim dy = Doubel.Parse(tabl(1)) - Double.Parse(y)

通過將閱讀和轉換部分提取到一個單獨的函數中,可以使代碼更清晰。 在此示例中,我還添加了一些有效性檢查

Private Function TryReadCoords(sr As StreamReader, ByRef x As Double, ByRef y As Double) _
    As Boolean

    Dim ligne = sr.ReadLine()
    If String.IsNullOrWhiteSpace(ligne) Then
        Return False
    End If

    Dim parts = Split(ligne, ";")
    Return _
        parts.Length = 2 AndAlso _
        Double.TryParse(parts(0), x) AndAlso _
        Double.TryParse(parts(1), y)
End Function

您可以將StreamReader簡化為Using語句。 它將在最后自動關閉文件。

使用此新功能,代碼現在看起來更加簡單:

Using sr As New StreamReader(OpenFileDialog1.FileName)
    Dim x0, y0, x1, y1 As Double

    If TryReadCoords(sr, x0, y0) Then
        While TryReadCoords(sr, x1, y1)
            Dim dx = x1 - x0
            Dim dy = y1 - y0

            'Do calculations here ...

            x0 = x1
            y0 = y1
        End While
    End If
End Using

VB.Net提供了Point *結構。 使用它; 在閱讀每一行時,將其解析為Point實例。

當可以做很多事情時,編寫一個接受兩個Point並返回它們之間的距離的方法。 這對您有幫助,因為它可以拉開所有干擾目標或使您困惑的事情:

 Public Function Distance(ByVal p1 As Point, ByVal p2 As Point) As Double
     Return Point.Subtract(p1, p2).Length
 End Function

這看起來像是作業,所以您的教授可能希望您能完成全部工作
√((x₁ - x)² + (y₁ - y₂)²)的公式,但如果你需要寫它你自己是分配點。


*請注意,代碼示例使用System.Windows.Point而不是System.Drawing.Point ,這需要添加對項目的引用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM