2017-01-31 6 views
0

Ich habe eine Datei array.txt, die aus Zahlen besteht. Ich muss daraus ein 2D-Array machen. Wie kann ich das machen?Wie übersetzt man Datei-Int 2-D-Array?

0 6 10 0 0 0 0 0 0 0 
6 0 12 11 14 0 0 0 0 0 
10 12 0 12 0 0 8 16 0 0 
0 11 12 0 0 6 3 0 0 0 
0 14 0 0 0 4 0 0 6 0 
0 0 0 6 4 0 0 0 12 0 
0 0 8 3 0 0 0 0 16 6 
0 0 16 0 0 0 0 0 0 8 
0 0 0 0 6 12 16 0 0 13 
0 0 0 0 0 0 6 8 13 0 

Antwort

0

müssen Sie haben 2 Variable für Reihe und Spalte ... Lesen Sie die Datei, bis lesen neue Zeile Stecke jede Zahl in arr [Spalte] [Zeile] und fügen Sie 1 bis Spalte, wenn gelesen neue Zeile Zeile 1 hinzufügen und Nummer in neuer Zeile wieder lesen.

0

+ Combo_ci ist korrekt. Sie müssen die Datei lesen, nach den Leerzeichen suchen, um die Spalte zu inkrementieren, und nach der neuen Zeilenkombination CR-LF, um die Zeile zu vergrößern. Hier ist ein modifiziertes Beispiel, das ich ausgearbeitet habe, um eine Excel-CSV-Datei zu lesen. Der einzige Unterschied ist, dass ich den Kommatrenner durch einen Platz

'Reading a space deliminted file 
    TextWindow.Show() 

    LoadFile() 
    TextWindow.WriteLine("File Size = " + Text.GetLength(DataIn)) 
    ParseFile() 
    TextWindow.WriteLine("Rows = " + rows + ", Columns = " + cols) 
    DisplayTable() 

    '--------------------------------------------------------------- 

    'Read the contents of the CSV style (spaces instead of commas) file into memory 
    Sub LoadFile 
     filename = Program.Directory 
     filename = filename + "\excelintoSB.csv" 

     DataIn = File.ReadContents(filename) 
    EndSub 

    'Parse the file contents, looking for spaces and line breaks to separate the cells. 
    Sub ParseFile 
     row = 1 
     col = 1 
     cell = "" 

     For i =1 To Text.GetLength(DataIn) 'Look at each character in the file 
     ch = Text.GetSubText(DataIn,i,1) 
     'Is it a space or a cariage return? Store the Cell 
     If ch = " " or text.GetCharacterCode(ch) = 13 then 
      table[row][col] = cell 
      cell = "" 
      If text.GetCharacterCode(ch) = 13 Then 'end of row, start a new one 
      row = row + 1 
      col = 1 
      Else 'New Cell, current row 
      col = col + 1 
      If col > maxCol then 'Keep track of how many columns we have encountered 
       maxCol = col 
      endif 
      endif 
     ElseIf text.GetCharacterCode(ch) <> 10 then 'build the cell, ignoring line feeds. 
      cell = cell + ch 
     EndIf 
     EndFor 
     rows = row - 1 
     cols = maxCol 
    EndSub 

    'Display the table in row/column format 
    Sub DisplayTable 
     TextWindow.WriteLine("The Table --- ") 
     For i = 1 To rows 
     TextWindow.Write("[ ") 
     For j = 1 To cols 
      TextWindow.Write(table[i][j] + " ") 
     EndFor 
     TextWindow.WriteLine("]") 
     EndFor 
    EndSub 
ersetzt habe
Verwandte Themen