2016-12-16 42 views
-2

我目前正試圖在vb.net中重新創建一個enigma機器作爲良好的做法。 我正在嘗試爲轉子創建一個自定義類。VB.NET數組的自定義類列表

轉子應該包含一個整數Shift,它計算它經過的轉換次數,如果它達到26,則另一個轉子轉換。

另外的26行,其每一個包含一個轉子的結構的26個插頭 例如列表或數組:

18 16 0 11 23 4 9 19 8 1 17 13 2 24 22 6 15 21 12 7 14 20 5 10 25 3 
19 13 9 0 2 17 12 24 4 14 15 8 7 5 21 18 1 6 25 23 20 3 11 22 10 16 
18 13 23 5 6 16 2 25 11 1 20 17 4 10 14 0 19 3 8 15 22 9 12 21 7 24 
25 10 17 21 3 12 16 4 19 20 6 9 8 13 7 18 1 15 14 2 23 22 11 5 24 0 
18 23 7 12 17 24 25 5 20 8 0 15 14 21 1 3 9 13 22 19 2 4 10 6 11 16 
7 23 0 24 3 25 10 2 15 19 16 14 17 9 11 13 5 20 8 1 21 6 18 12 22 4 
11 12 7 14 24 0 19 2 10 8 22 17 4 15 5 13 3 16 21 6 9 25 18 1 20 23 
11 5 14 7 12 6 15 25 20 23 18 21 9 19 17 8 4 1 22 2 16 10 0 13 3 24 
7 19 10 25 15 13 12 14 4 8 21 17 0 22 11 24 18 2 9 1 20 5 3 16 23 6 
19 23 1 21 0 15 10 2 7 4 22 12 14 9 17 20 11 8 18 13 5 6 16 3 25 24 
19 13 14 12 6 23 18 7 9 17 22 8 15 1 24 5 10 20 16 11 2 21 0 3 25 4 
4 1 5 3 10 13 24 25 21 18 16 6 20 11 17 19 7 14 0 23 9 15 2 12 22 8 
19 18 22 10 17 6 20 13 2 12 14 15 24 1 11 9 8 3 5 4 7 25 0 16 21 23 
16 13 14 1 8 11 9 25 24 3 2 4 0 5 22 18 19 7 17 15 23 20 6 10 12 21 
2 15 20 0 16 17 1 24 8 23 14 21 3 4 11 19 9 5 6 22 7 12 18 25 10 13 
22 10 17 9 4 20 24 2 13 25 8 21 23 0 19 7 11 5 15 1 16 3 14 12 6 18 
5 14 0 10 21 9 3 25 13 1 4 18 8 17 22 2 20 24 15 19 23 16 7 11 12 6 
15 1 13 2 19 7 9 16 11 10 17 14 4 25 6 24 0 23 22 21 8 20 5 12 18 3 
12 8 20 6 11 3 10 23 21 14 13 7 0 22 19 4 16 2 5 15 9 17 1 18 24 25 
4 8 25 5 1 12 10 22 9 24 14 19 2 0 6 20 17 3 23 15 13 11 21 16 18 7 
11 21 5 6 16 19 13 20 23 10 18 14 3 8 15 24 0 25 4 22 12 7 1 17 2 9 
5 24 19 23 17 18 0 11 2 20 14 1 25 22 9 6 15 7 10 13 8 12 16 3 4 21 
1 11 8 13 3 22 14 19 4 6 5 15 24 9 21 23 7 0 2 12 25 10 16 17 20 18 
18 3 19 16 10 24 23 1 22 17 20 9 7 14 2 21 5 8 0 11 13 4 15 25 12 6 
5 11 7 0 8 2 13 17 23 16 9 10 20 12 19 3 25 24 18 21 14 22 15 6 4 1 
10 2 3 22 16 6 15 19 1 20 18 24 13 5 8 23 21 7 14 17 9 11 25 12 4 0 

現在我已試過,但它似乎沒有很好地工作:

Public Class Rotor 

    Public Property lines As New List(Of Liner)() 
    Public Property shift As Integer 
    Public Structure Liner 
     Public Property plug() As Integer() 
    End Structure 
End Class 

我的配置加載:

Public Sub ConfigRotor(ByVal RotorConf As Rotor, ByVal PathConf As String) 
    Dim i As Integer = 0 
    For Each line As String In File.ReadAllLines(PathConf) 
     Dim y As Integer = 0 
     For Each strr As String In line.Split(" ") 
      RotorConf.lines(i).plug(y) 
      y = y+1 
     Next 

     i = i + 1 
     If i > 26 Then 
      MsgBox("Configuration file not supported") 
     End If 
    Next 
End Sub 

你會看到一個更優化的方式,你可以參考我輕鬆處理這個?

+1

這可能更適合CodeReview? –

+0

「y = +1」?那會一直把1放在裏面。我不知道你是如何初始化你的陣列,或者......說「看起來效果不好」不是很清楚。什麼不行? –

+0

@the_lotus抱歉錯字錯誤哈哈糾正了它 –

回答

1

首先,正確的方式來聲明你的類是這樣的:

Public Class Rotor 

    Public Property lines As New List(Of Liner) 
    Public Property shift As Integer 
    Public Structure Liner 
     Public Property plug As Integer() 
    End Structure 
End Class 

要得到一個eficiente代碼,我建議你將文件類型從文本更改爲二進制。讀取和操作字節數組並將其轉換爲int數組比使用字符串數組所做的任何操作更簡單快捷。

我會假定你的文件是二進制文件,並且你已經存儲了它所需要的確切字節量(26 x 26 = 676字節)。代碼將是這樣的:

Public Sub ConfigRotor(ByVal RotorConf As Rotor, ByVal PathConf As String) 
    Dim myArray() As Byte = File.ReadAllBytes(PathConf) 

    If (myArray.Length <> 676) Then 
     MsgBox("Configuration file not supported") 
    Else 
     RotorConf.lines.Clear() 
     For pos As Integer = 0 To myArray.Length - 1 Step 26 
      Dim line As New Rotor.Liner 
      Dim newPlug(25) As Integer 
      line.plug = newPlug 
      Array.Copy(myArray, pos, line.plug, 0, 26) 
      RotorConf.lines.Add(line) 
     Next 
    End If 
End Sub 

簡單,快速和乾淨。 運行此代碼的總時間少於1毫秒(取決於您的驅動器的速度)。

0

好吧,因爲沒有人可以幫助我做了一件非常醜陋的事情,但它很有效。如果有人對未來REFFERENCE有興趣,我宣佈兩種結構:

Public Structure rotori 
    Dim lines() As liner 
    Public shift As Integer 
End Structure 

Public Structure liner 
    Public plugs() As Integer 
End Structure 

然後我用的應用程序啓動這樣使用ReDim:

Dim Rotor1 As New rotori 
ReDim Rotor1.lines(25) 
For b As Integer = 0 To 25 
    ReDim Rotor1.lines(b).plugs(25) 
Next