从文本文件中读取数组?

时间:2017-11-25 16:53:28

标签: vb.net console console-application vb.net-2010

Imports System.IO
Module Module1

    Sub Main()
        Dim readers As New StreamReader("input.txt")
        Dim s As String
        Dim a() As String
        Dim b(,) As Integer
        Dim count As Integer = 0

        s = readers.ReadLine
        Do
            s = readers.ReadLine
            If s <> "" Then
                a = Split(s, ",")
                ReDim Preserve b(count, a.Length - 1)
                fill2darr(a, b, count)
                count += 1
            End If
            s = readers.ReadLine
        Loop While s <> " "

        Dim temp() As Integer = TransferTo1D(b)
        SelectionSort(temp)
        TransferTo2D(b, temp)
        writeintofile(b)

        Console.ReadKey()
    End Sub
    Public Sub fill2darr(ByVal a() As String, ByRef b(,) As Integer, ByVal counter As Integer)
        For i = 0 To a.Length - 1
            b(counter, i) = Val(a(i))
        Next
    End Sub
    Public Function TransferTo1D(ByRef B(,) As Integer) As Integer()
        Dim ARR1D(B.Length - 1) As Integer, e As Integer = 0
        For i = 0 To B.GetUpperBound(0)
            For j = 0 To B.GetUpperBound(1)
                ARR1D(e) = B(i, j)
                e += 1
            Next
        Next
        Return ARR1D
    End Function
    Public Sub SelectionSort(ByRef arr1d() As Integer)
        Dim i, j, min, temporary As Integer
        For i = 0 To arr1d.GetUpperBound(0)
            min = i
            For j = i + 1 To arr1d.GetUpperBound(1)
                If (arr1d(j) < arr1d(min)) Then
                    min = j
                End If
            Next
            temporary = arr1d(i)
            arr1d(i) = arr1d(min)
            arr1d(min) = temporary
        Next
    End Sub
    Public Sub TransferTo2D(ByRef twodarray(,) As Integer, ByRef onedarray() As Integer)
        Dim counter As Integer = 0
        For i = 0 To twodarray.GetUpperBound(0)
            For j = 0 To twodarray.GetUpperBound(1)
                If counter < onedarray.Length Then
                    twodarray(i, j) = onedarray(counter)
                    counter = counter + 1
                Else
                    twodarray(i, j) = 0
                End If
            Next
        Next

    End Sub
    Public Sub writeintofile(ByVal b(,) As Integer)
        Dim writer As New StreamWriter("output.txt")
        For i = 0 To b.GetUpperBound(0)
            For j = 0 To b.GetUpperBound(1)
                writer.Write(b(i, j))
                writer.Write(",")

            Next
            writer.WriteLine()
        Next

    End Sub
End Module

这是我编写的一个程序,用于从文本文件中读取值并将其排列在二维数组中,然后将其传输到一个D数组,以正确的方式将其从最小值排序到最大值,最后在另一个中重新打印文本文件。上面的代码没有任何错误,但是它是为了从文本文件中读取一个数组而编写的,我希望程序只要读取单词&#34; array:&#34;在&#34;输出&#34;中执行传输,排序和重新打印结果数组的步骤。文本文件,然后它清空数组,以便它可以接收下一个数组,我不知道我怎么能写这个条件,任何建议?

0 个答案:

没有答案