| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1447 人关注过本帖, 2 人收藏
标题:最大值,最小值问题,盼望解决(结贴。。)
只看楼主 加入收藏
xinshous
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2010-2-22
结帖率:100%
收藏(2)
已结贴  问题点数:20 回复次数:17 
最大值,最小值问题,盼望解决(结贴。。)
说明:
1文件格式:id,a,b,c(a,b,c为三列数值)
2文件格式:类别,起始id,截止id
目的:
根据2文件中类别的起止id,分别算出a,b,c三列的最大值,最小值
结果文件格式:
类别,a最大值,b最大值,c最大值,,a最小值,b最小值,c最小值
1文件
  
图片附件: 游客没有浏览图片的权限,请 登录注册

2文件
图片附件: 游客没有浏览图片的权限,请 登录注册


结果文件:
nv01,0.192,0.256 ,0.303 ,,-0.165,-0.139,0.023
nv02,0.164,0.140 ,0.217 ,,-0.199,-0.152,0.023
nv03,0.052,0.038 ,0.124 ,,-0.100,-0.074,0.038
nv04,0.138,-0.021 ,0.157,,0.029 -0.106,0.067
nv05,0.161,0.149 ,0.170 ,,-0.156,-0.117,0.060

最大最小值.rar (1.4 KB)



[ 本帖最后由 xinshous 于 2010-2-24 09:52 编辑 ]
搜索更多相关主题的帖子: 最大值 
2010-02-22 08:53
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:20 
Option Explicit

Private Type 数据类型
    名称 As String
    范围1 As Long
    范围2 As Long
    最大值(1 To 3) As Double
    最小值(1 To 3) As Double
End Type

Dim dd() As 数据类型
Dim 总数 As Long

Const 文件1 = "1.txt"           '原始数据
Const 文件2 = "2.txt"           '分组规则
Const 文件3 = "结果.txt"        '结果文件

Private Sub Command1_Click()

Dim fr As Long
fr = FreeFile

Dim i As Long, o As Long
Dim j As String
Dim fj() As String

'分组计数
总数 = 0
Open 文件2 For Input As fr
Do While Not EOF(fr)
    Line Input #fr, j
    总数 = 总数 + 1
Loop
Close fr

ReDim dd(1 To 总数)

'取分组内容
Open 文件2 For Input As fr
i = 0
Do While Not EOF(fr)
    Line Input #fr, j
    i = i + 1
    fj() = Split(j, ",")
    dd(i).名称 = fj(0)
    dd(i).范围1 = fj(1)
    dd(i).范围2 = fj(2)
Loop
Close fr

'初始化最大值,最小值
For i = 1 To 总数
    For o = 1 To 3
        dd(i).最大值(o) = -99999
        dd(i).最小值(o) = 99999
    Next o
Next i

'打开数据,查找最大值\最小值
Open 文件1 For Input As fr
Do While Not EOF(fr)
    Line Input #fr, j
    fj() = Split(j, ",")
    For i = 1 To 总数
        If fj(0) > dd(i).范围1 And fj(0) < dd(i).范围2 Then
            For o = 1 To 3
                If fj(o) > dd(i).最大值(o) Then
                    dd(i).最大值(o) = fj(o)
                End If
                If fj(o) < dd(i).最小值(o) Then
                    dd(i).最小值(o) = fj(o)
                End If
            Next o
            Exit For
        End If
    Next i
Loop
Close fr

'保存结果
Open 文件3 For Output As fr
For i = 1 To 总数
    Print #fr, dd(i).名称; ","; dtos(dd(i).最大值(1)); ","; dtos(dd(i).最大值(2)); ","; dtos(dd(i).最大值(3)); ", ,";
    Print #fr, dtos(dd(i).最小值(1)); ","; dtos(dd(i).最小值(2)); ","; dtos(dd(i).最小值(3))
Next i
Close fr

'显示结果
Open 文件3 For Input As fr
Do While Not EOF(fr)
    Line Input #fr, j
    Text1.Text = Text1.Text & j & vbCrLf
Loop
Close fr

MsgBox "整理完成"
End Sub

Private Function dtos(cs As Double) As String       '补小数前导零
    dtos = Format(cs, "#0.#########")
End Function
--------------------------------------------
结果是:
nv01,0.192,0.256,0.303, ,-0.165,-0.139,0.023
nv02,0.164,0.139,0.217, ,-0.199,-0.152,0.023
nv03,0.074,0.152,0.155, ,-0.1,-0.101,0.028
nv04,0.138,-0.021,0.157, ,0.064,-0.074,0.067
nv05,0.161,0.149,0.161, ,-0.156,-0.117,0.06
--------------------------------------------

工程不传了,如果连工程都传的话,那真是专门求答案的人了.
这全部的代码都给了,工程自己建吧.也就是2个控件的问题
代码尽量少给注释,自己看吧.代码是分块的

所用到的知识比较深.相对BASIC 的语言来说,并不是相对WINDOWS 来说.




授人于鱼,不如授人于渔
早已停用QQ了
2010-02-22 09:51
xinshous
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2010-2-22
收藏
得分:0 
谢谢,完全符合要求
思路清晰明朗
2010-02-22 11:21
xinshous
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2010-2-22
收藏
得分:0 
为何我将第一行数值改为
100,23,12,-13
结果还是
nv01,0.192,0.256,0.303, ,-0.165,-0.139,0.023
nv02,0.164,0.139,0.217, ,-0.199,-0.152,0.023
nv03,0.074,0.152,0.155, ,-0.1,-0.101,0.028
nv04,0.138,-0.021,0.157, ,0.064,-0.074,0.067
nv05,0.161,0.149,0.161, ,-0.156,-0.117,0.06

最大值,最小值都不对了呀?
2010-02-22 13:35
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
你看一下我程序里的范围.

100-120

>100 并且 <120 ,不包括 100 120 这二个数.所以,你要测试的话,这个数需要弄成  101 就可以了.

可以算是我程序里的BUG
收到的鲜花
  • xinshous2010-02-22 16:53 送鲜花  3朵  

授人于鱼,不如授人于渔
早已停用QQ了
2010-02-22 16:17
xinshous
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2010-2-22
收藏
得分:0 
明白了,我也忽略了,谢谢
我改成>=  and <=就可以了
可是我加一行
100,23,12,-13
100,10,28,45,-998
101,-0.043 ,-0.122 ,0.129
102,0.145 ,0.099 ,0.176

结果:
nv01,23.,28.,45., ,-0.165,-0.139,-13.
23,28,45都挑对了,-998却没挑出来,不知为何
初学者,还请见教
2010-02-22 16:52
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
你检查一下你增加的这行数据.


100,10,28,45,-998

这行有 5 个数据了,而 -998正好是第5个数据,正好被忽略了.

------------------------优化代码,加强代码的强壮性--------------------------

Option Explicit

Private Type 数据类型
    名称 As String
    范围1 As Long
    范围2 As Long
    最大值(1 To 3) As Double
    最小值(1 To 3) As Double
End Type

Dim dd() As 数据类型
Dim 总数 As Long

Const 文件1 = "1.txt"           '原始数据
Const 文件2 = "2.txt"           '分组规则
Const 文件3 = "结果.txt"        '结果文件

Private Sub Command1_Click()

Dim fr As Long
fr = FreeFile

Dim i As Long, o As Long
Dim j As String
Dim fj() As String
Dim bz As Boolean

'判断数据文件和分组规划文件是否存在
If Dir(文件1) = "" Then
    MsgBox "数据文件不存在"
    Exit Sub
End If

If Dir(文件2) = "" Then
    MsgBox "分组规则文件不存在"
    Exit Sub
End If

'分组计数
总数 = 0
Open 文件2 For Input As fr
Do While Not EOF(fr)
    Line Input #fr, j
    fj() = Split(j, ",")
    If UBound(fj) > 1 Then            '判断是否能分成最少三段
        If IsNumeric(fj(1)) And IsNumeric(fj(2)) Then   '后面二段必须是数字
            总数 = 总数 + 1
        End If
    End If
Loop
Close fr

ReDim dd(1 To 总数)

'取分组内容
Open 文件2 For Input As fr
i = 0
Do While Not EOF(fr)
    Line Input #fr, j
    If UBound(fj) > 1 Then            '判断是否能分成最少三段
        If IsNumeric(fj(1)) And IsNumeric(fj(2)) Then   '后面二段必须是数字
            i = i + 1
            fj() = Split(j, ",")
            dd(i).名称 = fj(0)
            dd(i).范围1 = fj(1)
            dd(i).范围2 = fj(2)
        End If
    End If
Loop
Close fr

'初始化最大值,最小值
For i = 1 To 总数
    For o = 1 To 3
        dd(i).最大值(o) = -9999999
        dd(i).最小值(o) = 9999999
    Next o
Next i

'打开数据,查找最大值\最小值
Open 文件1 For Input As fr
Do While Not EOF(fr)
    Line Input #fr, j
    fj() = Split(j, ",")
    If UBound(fj) > 2 Then      '最少有四个元素
        bz = True               '先设置为可用
        For i = 0 To 3          '搜索前四个元素,确保均为数字
            If Not IsNumeric(fj(i)) Then
                bz = False
                Exit For
            End If
        Next i
        
        If bz Then              '只有全部是数字时,才去判断及求最大值\最小值
            For i = 1 To 总数
                If fj(0) >= dd(i).范围1 And fj(0) <= dd(i).范围2 Then
                    For o = 1 To 3
                        If fj(o) > dd(i).最大值(o) Then
                            dd(i).最大值(o) = fj(o)
                        End If
                        If fj(o) < dd(i).最小值(o) Then
                            dd(i).最小值(o) = fj(o)
                        End If
                    Next o
                    Exit For
                End If
            Next i
        End If
    End If
Loop
Close fr

'保存结果
Open 文件3 For Output As fr
For i = 1 To 总数
    Print #fr, dd(i).名称; ","; dtos(dd(i).最大值(1)); ","; dtos(dd(i).最大值(2)); ","; dtos(dd(i).最大值(3)); ", ,";
    Print #fr, dtos(dd(i).最小值(1)); ","; dtos(dd(i).最小值(2)); ","; dtos(dd(i).最小值(3))
Next i
Close fr

'显示结果
Open 文件3 For Input As fr
Do While Not EOF(fr)
    Line Input #fr, j
    Text1.Text = Text1.Text & j & vbCrLf
Loop
Close fr

MsgBox "整理完成"
End Sub

Private Function dtos(cs As Double) As String       '补小数前导零
    dtos = Format(cs, "#0.#########")
End Function



[ 本帖最后由 风吹过b 于 2010-2-22 20:19 编辑 ]
收到的鲜花
  • xinshous2010-02-22 20:23 送鲜花  3朵  

授人于鱼,不如授人于渔
早已停用QQ了
2010-02-22 19:47
xinshous
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2010-2-22
收藏
得分:0 
100,23,12,-13
100,28,45,-998
101,-0.043 ,-0.122 ,0.129
102,0.145 ,0.099 ,0.176
103,-0.049 ,0.000 ,0.049
104,0.104 ,-0.081 ,0.132
105,-0.025 ,-0.050 ,0.056
106,0.044 ,0.086 ,0.097
107,0.192 ,0.007 ,0.192
108,0.127 ,0.017 ,0.128
109,-0.165 ,0.075 ,0.181
110,0.023 ,0.000 ,0.023
结果变成如下了,还请关注
nv01,-9999999.,-9999999.,-9999999., ,9999999.,9999999.,9999999.
nv02,-9999999.,-9999999.,-9999999., ,9999999.,9999999.,9999999.
nv03,-9999999.,-9999999.,-9999999., ,9999999.,9999999.,9999999.
nv04,-9999999.,-9999999.,-9999999., ,9999999.,9999999.,9999999.
nv05,-9999999.,-9999999.,-9999999., ,9999999.,9999999.,9999999.
2010-02-22 20:21
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
[ 本帖最后由 风吹过b 于 2010-2-22 20:19 编辑 ]

请重新下我的代码.

    If UBound(fj) > 2 Then      '最少有四个元素

这行前面写错了.应该是写2,不是写3

收到的鲜花
  • xinshous2010-02-22 21:59 送鲜花  3朵   附言:好文章

授人于鱼,不如授人于渔
早已停用QQ了
2010-02-22 21:53
xinshous
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2010-2-22
收藏
得分:0 
谢谢 风吹过b老师
您不厌其烦的指导
及其高深的内在修养
让我受益匪浅
谢谢了
明天我将做深入检查
不便之处还请见谅
2010-02-22 21:58
快速回复:最大值,最小值问题,盼望解决(结贴。。)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037506 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved