2017-04-08 81 views
0

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

txt文件看起來是這樣的:

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

,所以我的目標是計算dx和dy(座標每次2線之間的差值),所以我得到的距離之後。

唯一的問題是我被困在如何得到dx和dy(7天現在) 例如第1和第2行的例子是(506-596),第2行是(412-506)等等對於所有與dy的點。

這裏是我一直試圖徒勞(大部分代碼只是從網上覆制/粘貼)。

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 

謝謝你,希望有人會幫我出這個問題的:)

+0

聽說過的[勾股定理]的(https://en.wikipedia.org/維基/ Pythagorean_theorem)?法文:[Théorèmede Pythagore](https://fr.wikipedia.org/wiki/Th%C3%A9or%C3%A8me_de_Pythagore)。您將需要它來計算2-D空間中2個點之間的距離! –

+0

這不是一個如何計算我在模塊中完成論壇的問題,但我需要dx和dy。 dist = Math.Sqrt((dx)^ 2 +(dy)^ 2) – Goraby

+0

你有''dx''和''dy''作爲字符串。要做任何類型的數學與他們,你需要改變他們整數(或單打或雙打) – RPM

回答

0

你應該申報

Dim tabl() As String 

由於Split返回一個字符串數組的功能。你不能用字符串進行數學計算。所以你必須將所有的字符串轉換爲數字。例如。到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 

可以enlcose的StreamReaderUsing聲明。它會在最後自動關閉文件。

使用這個新功能,代碼現在看起來要簡單得多:

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 
+0

好的,謝謝,我這樣做了,現在我在昏暗的dx行中得到這個錯誤:輸入字符串的格式不正確。 – Goraby

+0

我沒有看到你的字符串中有什麼,但我想在文件的末尾有一個空行。 –

+0

txt文件是好的我猜在最後沒有行,它可能是代碼中的另一個錯誤?和我應該刪除以前的功能然後(對不起,我還是很新的編碼) – Goraby

0

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,這就需要增加一個參考項目