modfiy代码添加更多功能 [英] modfiy code to add more function

查看:181
本文介绍了modfiy代码添加更多功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




下面的代码将删除重复的行和排序。



i想修改下面的代码添加更多功能



as



1-如果g1,g2,g3,g4 = 0则行将被删除,如



 DDS,20110523,< big> 0,0,0,0< / big>,24 





2 - 如果g1,g2,g3,g4 =之前一行的g4将被移除,如



该行为

 DDS,20110526,< big> 1.1,1.1,1.1,1.1< / big>, 29 
DDS,20110525,2.4,2.7,2.3,< big> 1.1< / big>,22



txt文件包含超过3000行和不同的q(clo 0)(名称为DDS,RRB,EES ......等)和日期(clo 1)

我正在使用的代码



 公共  AAABB 
朋友 C. lass DataItemList
属性 DataItems 作为列表( Of DataItem)


朋友 DataItem
公共 q 作为 String
公开 theDate 作为 DateTime
公开 g1 作为 Double
公开 g2 作为 Double
公开 g3 作为 Double
公开 g4 作为 Double
公共 g5 作为 整数

< span class =code-keyword>覆盖 功能 ToString()作为 字符串
返回 字符串 .Format(< span class =code-string> {0},{1},{2},{3},{4},{5},{6 },q,theDate.ToString( yyyyMMdd),g1,g2, g3,g4,g5)
结束 功能

结束

Sub AddOrUpdate(d As DataItem)
If DataItems .Exists( Function (x)xq = dq AndAlso x.theDate = d.theDate)然后
Dim idx = DataItems.FindIndex( Function (x)xq = dq AndAlso x.theDate = d.theDate)
DataItems(idx)= d
Else
DataItems.Add(d)
结束 如果

结束 Sub


Sub LoadData(src As String
使用 tfp = TextFieldParser(src)
tfp.TextFieldType = FieldType.Delimited
tfp.Delimiters = { }
tfp.ReadLine() ' 跳过标题。
Dim s 作为 字符串()
Dim lineNo 作为 整数 = 1 ' 我们已跳过第一行

while tfp.EndOfData
s = tfp.ReadFields
如果 s.Count = 7 那么
' 你/应该/解析数据比这更彻底,例如使用TryParse。
Me .AddOrUpdate( New DataItemList.DataItem 使用
{。q = s( 0 ),
.theDate = DateTime.ParseExact( s( 1 ), yyyyMMdd Nothing ),
.g1 = Double .Parse(s( 2 )),
.g2 = Double .Parse(s( 3 )),
.g3 = Double .Parse(s( 4 )),
.g4 = Double .Parse(s( 5 )),
.g5 = Int32 .Parse(s( 6 ))})

Else
MsgBox( String .Format( 第{1}行的文件{0}出错。,src,lineNo))
< span class =code-keyword>结束 如果

lineNo + = 1

结束
结束 使用
结束 Sub

Sub ( )
DataItems = 列表( DataItem)
结束 Sub

结束


公共 Sub TEXTAAA()
Dim myData = DataItemList

myData.LoadData( C:\ AAAA.txt
myData.LoadData( C:\ BBBB.txt
Dim sortedData = myData.DataItems.OrderBy( Function (d)dq).ThenByDescending( Function (d) d.theDate)

' 显示结果
Dim objWriter As < span class =code-keyword>新 System.IO.StreamWriter( C:\ test .txt
objWriter.Write( String .Join(vbCrLf,sortedData))
objWriter.Close()

System.Diagnostics。
Process.Start( notepad C:\ test.txt



结束 Sub
结束 班级





谢谢你的帮助

解决方案

  Sub  AddOrUpdate(d  As  DataItem)
如果 DataItems.Exists( Function (x)xq = dq AndAlso x.theDate = d.theDate)然后
Dim idx = DataItems .FindIndex( Function (x)xq = dq AndAlso x.theDat e = d.theDate)
DataItems(idx)= d
否则
如果 d.g2 = d.g5 d.g3 = d.g5 d。 g4 = g5 然后
d.RemoveAt(d.Count - 1 删除以前添加的元素
结束 < span class =code-keyword>如果
如果 (d .g2 = 0 d.g3 = 0 d.g4 = 0 d.g5 = 0 然后
DataItems.Add(d)
结束 如果
结束 如果
结束 Sub





问候,

- Manfred

Hi
the code below will remove duplicate lines and sorting .

i would like to modify the code below to add more function

as

1- if g1,g2,g3,g4 =0 then line will be removed like

DDS,20110523,<big>0,0,0,0</big>,24



2-if g1,g2,g3,g4 =g4 of the line before then line will be removed like

the line as

DDS,20110526,<big>1.1,1.1,1.1,1.1</big>,29
DDS,20110525,2.4,2.7,2.3,<big>1.1</big>,22


the txt file contain more then 3000 lines and different q(clo 0)(name as DDS ,RRB,EES...etc) and date (clo 1)
the code i am using

Public Class AAABB
    Friend Class DataItemList
        Property DataItems As List(Of DataItem)
       

        Friend Class DataItem
            Public q As String
            Public theDate As DateTime
            Public g1 As Double
            Public g2 As Double
            Public g3 As Double
            Public g4 As Double
            Public g5 As Integer

            Overrides Function ToString() As String
                Return String.Format("{0},{1},{2},{3},{4},{5},{6}", q, theDate.ToString("yyyyMMdd"), g1, g2, g3, g4, g5)
            End Function

        End Class

        Sub AddOrUpdate(d As DataItem)
            If DataItems.Exists(Function(x) x.q = d.q AndAlso x.theDate = d.theDate) Then
                Dim idx = DataItems.FindIndex(Function(x) x.q = d.q AndAlso x.theDate = d.theDate)
                DataItems(idx) = d
            Else
                DataItems.Add(d)
            End If

        End Sub


        Sub LoadData(src As String)
            Using tfp = New TextFieldParser(src)
                tfp.TextFieldType = FieldType.Delimited
                tfp.Delimiters = {","}
                tfp.ReadLine() ' skip headers.
                Dim s As String()
                Dim lineNo As Integer = 1 ' we've skipped the first line

                While Not tfp.EndOfData
                    s = tfp.ReadFields
                    If s.Count = 7 Then
                        ' you /should/ parse the data more thoroughly than this, e.g. with TryParse.
                        Me.AddOrUpdate(New DataItemList.DataItem With
                            {.q = s(0),
                             .theDate = DateTime.ParseExact(s(1), "yyyyMMdd", Nothing),
                             .g1 = Double.Parse(s(2)),
                             .g2 = Double.Parse(s(3)),
                             .g3 = Double.Parse(s(4)),
                             .g4 = Double.Parse(s(5)),
                             .g5 = Int32.Parse(s(6))})

                    Else
                        MsgBox(String.Format("Error in file {0} at line {1}.", src, lineNo))
                    End If

                    lineNo += 1

                End While
            End Using
        End Sub

        Sub New()
            DataItems = New List(Of DataItem)
        End Sub

    End Class


    Public Sub TEXTAAA()
        Dim myData = New DataItemList

        myData.LoadData("C:\AAAA.txt")
        myData.LoadData("C:\BBBB.txt")
        Dim sortedData = myData.DataItems.OrderBy(Function(d) d.q).ThenByDescending(Function(d) d.theDate)

        ' show the result
        Dim objWriter As New System.IO.StreamWriter("C:\test.txt")
        objWriter.Write(String.Join(vbCrLf, sortedData))
        objWriter.Close()

        System.Diagnostics.
       Process.Start("notepad", "C:\test.txt")



    End Sub
End Class



thank you for your help

解决方案

Sub AddOrUpdate(d As DataItem)
    If DataItems.Exists(Function(x) x.q = d.q AndAlso x.theDate = d.theDate) Then
        Dim idx = DataItems.FindIndex(Function(x) x.q = d.q AndAlso x.theDate = d.theDate)
        DataItems(idx) = d
    Else
        If d.g2 = d.g5 And d.g3 = d.g5 And d.g4 = g5 Then
            d.RemoveAt(d.Count - 1) 'Remove the previously added element
        End If
        If Not (d.g2 = 0 And d.g3 = 0 And d.g4 = 0 And d.g5 = 0) Then
            DataItems.Add(d)
        End If
    End If
End Sub



Regards,

— Manfred


这篇关于modfiy代码添加更多功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆