以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  求助:自动进行记录列值调整  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=110881)

--  作者:xl
--  发布时间:2017/12/11 16:30:00
--  求助:自动进行记录列值调整

求助:自动进行记录列值调整

希望对《原始数据》中数据一、二、三……数据列的值进行位置调整,数据只能在有值处调整,不能纵向调整,只能横向调整。如编号1的记录数据有“物理”、

“地理”、“化学”这三个值,调整也就是这三个值可互换位置,不能调整到数据四、五、六列;这3个值也不能调整到其它编号,如不能调整到编号5的记录中。

现希望根据“希望调整后统计数据”这个表来进行调整。注:“调整前数据统计”是统计“原始数据”中相应列各值出现的次数。

 下载信息  [文件大小:   下载次数: ]
点击浏览该文件:记录列值调整.table


 

[此贴子已经被作者于2017/12/11 16:32:46编辑过]

--  作者:有点甜
--  发布时间:2017/12/11 18:45:00
--  

汗,递归就不写了麻烦,直接判断。

 

下面代码只做了两种情况的判断,你扩展一下写。

 

Dim dt1 As DataTable = DataTables("希望调整后统计结果")
Dim dt2 As DataTable = DataTables("原始数据")
Dim dtb As new DataTableBuilder("测试")
dtb.AddDef("编号", Gettype(String), 32)
dtb.AddDef("数据一", Gettype(String), 32)
dtb.AddDef("数据二", Gettype(String), 32)
dtb.AddDef("数据三", Gettype(String),32)
dtb.Build()
Dim dt3 As DataTable = DataTables("测试")
dt3.AddNew(dt2.DataRows.count)
Dim ary() As String = {"数据一", "数据二", "数据三"}
Dim drs = dt1.Select("科目 <> \'总计\'")
For Each c As String In ary
    Dim idx As Integer = 0
    For Each dr As DataRow In drs
        For i As Integer = idx To idx+dr(c)-1
            dt3.DataRows(i)(c) = dr("科目")
        Next
        idx += dr(c)
    Next
Next
For i As Integer = 0 To 10 \'测试少量数据
    Dim dr As DataRow = dt2.DataRows(i)
   
    \'Dim ls As new List(Of String)
    \'ls.add(dr("数据一"))
    \'ls.add(dr("数据二"))
    \'ls.add(dr("数据三"))
   
    \'Dim flag As Boolean = False
    \'For Each cdr As DataRow In dt3.DataRows
    \'If cdr.Isnull("编号") Then
    \'If ls.Contains(cdr("数据一")) AndAlso ls.Contains(cdr("数据二")) AndAlso ls.Contains(cdr("数据三")) AndAlso cdr("数据一") <> cdr("数据二") AndAlso cdr("数据一") <> cdr("数据三") AndAlso cdr("数据三") <> cdr("数据二")Then
    \'flag = True
    \'cdr("编号") = dr("编号")
    \'Exit For
    \'End If
    \'End If
    \'Next
   
   
    Dim temp As String = ""
    For Each cdr As DataRow In dt3.DataRows
        If cdr.Isnull("编号") Then
            If dr("数据一") = cdr("数据一") OrElse dr("数据一") = cdr("数据二") OrElse dr("数据一") = cdr("数据三") Then
                If dr("数据一") = cdr("数据一") Then
                    For Each ccdr As DataRow In dt3.DataRows
                        If ccdr.Isnull("编号") Then
                            If dr("数据二") = ccdr("数据二") OrElse dr("数据二") = ccdr("数据三") Then
                                If dr("数据二") = ccdr("数据二") Then
                                    For Each cccdr As DataRow In dt3.DataRows
                                        If cccdr.Isnull("编号") Then
                                            If dr("数据三") = cccdr("数据三") Then
                                                cdr("编号") = dr("编号")
                                                temp = cdr("数据三")
                                                cdr("数据三") = cccdr("数据三")
                                                cccdr("数据三") = temp
                                                temp = cdr("数据二")
                                                cdr("数据二") = ccdr("数据二")
                                                ccdr("数据二") = temp

                                                Exit For
                                            End If
                                        End If
                                    Next
                                ElseIf dr("数据二") = ccdr("数据三") Then
                                    For Each cccdr As DataRow In dt3.DataRows
                                        If cccdr.Isnull("编号") Then
                                            If dr("数据三") = cccdr("数据二") Then
                                                cdr("编号") = dr("编号")
                                                temp = cdr("数据二")
                                                cdr("数据二") = cccdr("数据二")
                                                cccdr("数据二") = temp
                                                temp = cdr("数据三")
                                                cdr("数据三") = ccdr("数据三")
                                                ccdr("数据三") = temp

                                                Exit For
                                            End If
                                        End If
                                    Next

                                End If
                                Exit For
                            End If
                        End If
                    Next
                End If
                Exit For
            End If
        End If
    Next
Next
MainTable = Tables("测试")

 


--  作者:有点蓝
--  发布时间:2017/12/11 23:48:00
--  
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:记录列值调整.table


--  作者:xl
--  发布时间:2017/12/12 10:46:00
--  
谢谢两位版主
--  作者:xl
--  发布时间:2017/12/17 20:23:00
--  
略过


[此贴子已经被作者于2017/12/18 20:12:28编辑过]

--  作者:有点甜
--  发布时间:2017/12/17 21:07:00
--  

1、看懂2楼代码;

 

2、判断各个组合集合,2楼的代码判断的是  数据一=数据一,数据二=数据二,数据三=数据三 的情况,以及 数据一=数据一,数据二=数据三,数据三=数据二 的情况。

 

你把只需要再加判断即可。应该有6种可能性。


--  作者:xl
--  发布时间:2017/12/18 9:21:00
--  
现数据一二三有6种可能性,实际数据需增加到数据七、数据八,这样就麻烦很多了。
--  作者:有点甜
--  发布时间:2017/12/18 9:27:00
--  

那就要组合排列,然后根据组合排列去编写代码,如

 

下载信息  [文件大小:316.0 KB  下载次数:37]
图片点击可在新窗口打开查看点击浏览该文件:排列组合.table


--  作者:有点甜
--  发布时间:2017/12/18 10:13:00
--  

参考代码,计算比较慢

 

Dim dt1 As DataTable = DataTables("希望调整后统计结果")
Dim dt2 As DataTable = DataTables("原始数据")
Dim dtb As new DataTableBuilder("测试")
dtb.AddDef("编号", Gettype(String), 32)
dtb.AddDef("数据一", Gettype(String), 32)
dtb.AddDef("数据二", Gettype(String), 32)
dtb.AddDef("数据三", Gettype(String),32)
dtb.Build()
Dim dt3 As DataTable = DataTables("测试")
dt3.AddNew(dt2.DataRows.count)
Dim ary() As String = {"数据一", "数据二", "数据三"}
Dim drs = dt1.Select("科目 <> \'总计\'")
For Each c As String In ary
    Dim idx As Integer = 0
    For Each dr As DataRow In drs
        For i As Integer = idx To idx+dr(c)-1
            dt3.DataRows(i)(c) = dr("科目")
        Next
        idx += dr(c)
    Next
Next

\'求排列
Dim lst_Permutation As List(Of String()) = PermutationAndCombination(Of String).GetPermutation(ary, ary.Length)

For i As Integer = 0 To 54 \'测试少量数据
output.show(i)
    Dim dr As DataRow = dt2.DataRows(i)
    Dim temp As String = ""
    For Each ary In lst_Permutation
output.show(ary(0) & " " & ary(1) & " " & ary(2))
        For Each cdr As DataRow In dt3.DataRows
            If cdr.Isnull("编号") Then
                If dr("数据一") = cdr(ary(0)) Then
                    For Each ccdr As DataRow In dt3.DataRows
                        If ccdr.Isnull("编号") Then
                            If dr("数据二") = ccdr(ary(1)) Then
                                For Each cccdr As DataRow In dt3.DataRows
                                    If cccdr.Isnull("编号") Then
                                        If dr("数据三") = cccdr(ary(2)) Then
                                            cdr("编号") = dr("编号")
                                            temp = cdr(ary(2))
                                            cdr(ary(2)) = cccdr(ary(2))
                                            cccdr(ary(2)) = temp
                                            temp = cdr(ary(1))
                                            cdr(ary(1)) = ccdr(ary(1))
                                            ccdr(ary(1)) = temp
                                            goto label1
                                        End If
                                    End If
                                Next
                            End If
                        End If
                    Next
                End If
            End If
        Next
    Next
    label1:
Next
MainTable = Tables("测试")


--  作者:xl
--  发布时间:2017/12/18 10:46:00
--  
编译出错提示:未申明名称: “PermutationAndCombination”

Dim lst_Permutation As List(Of String()) = PermutationAndCombination(Of String).GetPermutation(ary, ary.Length)