如何在不使用数组和查询的情况下对文本文件进行排序



我正在尝试订购一个包含数字的文本文件,并在不使用数组查询的情况下用其他纺织品重写它

我尝试使用比较语句来做到这一点,但最终它只在新的文本文件中写入一个数字

为了明确程序,程序是在该文件中找到中位数,如果不对文件进行降序或升序重新排序,我就无法做到这一点

这就是我所做的

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim sw As IO.StreamReader = IO.File.OpenText("Numbers.txt")
    Dim sr As IO.StreamWriter = IO.File.CreateText("Numbers2.txt")
    Dim num As Double
    Dim min As Double = CDbl(sw.ReadLine)
    Dim line As String = sw.ReadLine
    Do Until sw.EndOfStream
        num = CDbl(sw.ReadLine)
        If min > num Then
            sr.WriteLine(num)
        End If
    Loop
    sr.Close()
End Sub

好吧,诅咒我不知道你的文本文件是什么样子的,没有他的字符串格式我无能为力所以我做了两个函数,可以帮助中位数程序

该程序是找到该文件中的中位数

您可以

测试此代码,只需在框架中添加 1 个按钮即可(我写了一些字符串进行测试,您可以更改它)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Button1.Text = "20; 30; 50; 40; 10; 80; 90; 70" Then 'A pair list of simple numbers
        Button1.Text = "20; 30; 50; 40; 80; 90; 70" 'Odd list of simple numbers
    Else
        If Button1.Text = "20; 30; 50; 40; 80; 90; 70" Then
            Button1.Text = "18; 91,123; 47,789; 29; 35; 78.456; 91; 84" 'A test with a mix of some decimal separators mistakes
        Else
            If Button1.Text = "18; 91,123; 47,789; 29; 35; 78.456; 91; 84" Then
                Button1.Text = "25.45; 78; 10.123; 65; 27.485; 78; 19; 41; 26" 'Another odd test
            Else
                Button1.Text = "20; 30; 50; 40; 10; 80; 90; 70"
            End If
        End If
    End If
    Dim StringList As String = Button1.Text
    Me.Text = "Medan of " & StringList & " = " & MedianOf(StringList, "; ") 'Using "; " (; + space) separator (can be changed)
End Sub
Private Function MedianOf(ByVal StrLine As String, ByVal Separator As String) As Double
    Dim Nb As Double
    Dim Str As String = StrLine
    'Write all values in the good order with the next function down here e.g.(from "23; 12; 78; ..." to "12; 23; 78; ...")
    StrLine = GetOrderOf(StrLine, Separator)
    'Counts how many values StrLine contains
    Dim ValuesInString = System.Text.RegularExpressions.Regex.Split(StrLine, "" & Separator).GetUpperBound(0) + 1
    'Check ValuesInString count odd's (calculated with different ways)
    If ((ValuesInString And 1) = 1) = True Then
        Nb = CDbl(System.Text.RegularExpressions.Regex.Split(StrLine, "" & Separator)(ValuesInString  2))
        MessageBox.Show("ODD:" & Environment.NewLine & "First String =       " & Str & Environment.NewLine & "Ordered string = " & StrLine & Environment.NewLine & "***  Median value = " & Nb & " ***")
    Else
        Nb = (CDbl(System.Text.RegularExpressions.Regex.Split(StrLine, "" & Separator)((ValuesInString / 2) - 1)) + CDbl(System.Text.RegularExpressions.Regex.Split(StrLine, "" & Separator)(ValuesInString / 2))) / 2
        MessageBox.Show("PAIR:" & Environment.NewLine & "First String =       " & Str & Environment.NewLine & "Ordered string = " & StrLine & Environment.NewLine & "(" & StrLine.Split(Separator)((ValuesInString / 2) - 1) & "+" & StrLine.Split(Separator)(ValuesInString / 2) & ")/2 = " & Nb)
    End If
    'MessagesBoxes were added only for a best understanding of how it works
    'Should be disabled if tests are ok
    Return Nb
End Function
Private Function GetOrderOf(ByVal Line As String, ByVal Separator As String) As String
    If Separator = "." Or Separator = "," Then MessageBox.Show("Wrong... so Wrong separator")
    Line = Line.Replace(".", ",")
    Dim Line_InOrder As String = ""
    Dim Str As String = ""
    Dim Nb As Double = 0
    Do While Line.Length > 0
        Nb = 0
        If Line.Contains(Separator) Then
            For Each St As String In System.Text.RegularExpressions.Regex.Split(Line, "" & Separator)
                If CDbl(St) > Nb Then
                    Nb = CDbl(St)
                    Str = St
                End If
            Next
        Else
            Str = Line
            Line &= Separator
        End If
        If Line.Contains(Str & Separator) Then
            Line = Replace(Line, Str & Separator, "", , 1)
        Else
            Line = Replace(Line, Separator & Str, "", , 1)
        End If
        If Line_InOrder.Length = 0 Then
            Line_InOrder = Str
        Else
            Line_InOrder = Str & Separator & Line_InOrder
        End If
    Loop
    Return Line_InOrder
End Function

相关内容

最新更新