2014-11-21 28 views
-1

我想订购包含数字的文本文件,并把它改写其他纺织品不使用阵列如何做使用比较语句就订购一个文本文件,而无需使用数组和查询

查询我尝试但最终只写一个数字,在新的文本文件

要清楚程序,以查找该文件中位数这是我不能没有重新排序文件升序或降序

这就是我所做的

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 
+2

所以......你为什么不想把数据解析成数组? – Zack 2014-11-21 18:36:01

+0

我想看看我是否可以用另一种方式解析数据到数组中 – 2014-11-21 18:51:38

+0

您可以通过多次遍历两个交替输出文件来对数据进行冒泡排序。 – 2014-11-21 18:56:49

回答

0

好,骂的我不知道你的文本文件的样子,我不能没有他的字符串格式帮助 所以我做了两个函数谁可以帮助关于中央计划

程序是发现在该文件中位数

您可以测试这个代码,只是这一切都添加相框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 
相关问题