Матрица из текстового файла - PullRequest
0 голосов
/ 24 марта 2019

Считывает матрицу из текстового файла, первая строка текстового файла имеет размеры матрицы, следующие строки будут содержать элементы, разделенные пробелами. Я думал об этом, но не знаю, как получить текстовый файл.

   Dim path = "z:matrix.txt"
    Using reader As New IO.StreamReader(path)
        Dim size = reader.ReadLine() ' read first line which is the size of the matrix (assume the matrix is a square)
        Dim A(size - 1, size - 1) As Integer

        Dim j = 0 ' the current line in the matrix
        Dim line As String = reader.ReadLine() ' read next line
        Do While (line <> Nothing) ' loop as long as line is not empty
            Dim numbers = line.Split(" ") ' split the numbers in that line
            For i = 0 To numbers.Length - 1
                A(j, i) = numbers(i) ' copy the numbers into the matrix in current line
            Next

            j += 1 ' increment the current line
            line = reader.ReadLine() ' read next line
            Console.WriteLine(line)
        Loop


    End Using

3

1 3 5

2 4 6

7 8 9

1 Ответ

1 голос
/ 24 марта 2019
Dim path = "D:\matrix.txt"
Using reader As New IO.StreamReader(path)
    Dim size = reader.ReadLine() ' read first line which is the size of the matrix (assume the matrix is a square)
    Dim A(size - 1, size - 1) As Integer

    Dim j = 0 ' the current line in the matrix
    Dim line As String = reader.ReadLine() ' read next line
    Do While (line <> Nothing) ' loop as long as line is not empty
        Dim numbers = line.Split(" ") ' split the numbers in that line
        For i = 0 To numbers.Length - 1
            A(j, i) = numbers(i) ' copy the numbers into the matrix in current line
        Next

        j += 1 ' increment the current line
        line = reader.ReadLine() ' read next line
    Loop

    A.Dump() ' print the matrix in LinqPad
End Using

Образец текстового файла:

3
1 3 5
2 4 6
7 8 9

Результат в LinqPad.enter image description here

Модифицированный код без LinqPad:

Dim path = "d:\matrix.txt"
Dim A(,) As Integer
Using reader As New IO.StreamReader(path)
    Dim size = reader.ReadLine() ' read first line which is the size of the matrix (assume the matrix is a square)
    Redim A(size - 1, size - 1)

    Dim j = 0 ' the current line in the matrix
    Dim line As String = reader.ReadLine() ' read next line
    Do While (line <> Nothing) ' loop as long as line is not empty
        Dim numbers = line.Split(" ") ' split the numbers in that line
        For i = 0 To numbers.Length - 1
            A(j, i) = numbers(i) ' copy the numbers into the matrix in current line
        Next

        j += 1 ' increment the current line
        line = reader.ReadLine() ' read next line
    Loop
End Using

Console.WriteLine("Matrix A :")
Dim numberWidth As Integer = 2
Dim format As String = "D" & numberWidth
For i As Integer = 0 To A.GetUpperBound(0)
    Console.Write("| ")
    For j As Integer = 0 To A.GetUpperBound(1)
        Console.Write("{0} ", A(i, j).ToString(format))
    Next
    Console.WriteLine("|")
Next
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...