作业帮 > 数学 > 作业

VB题.将用户输入的三个数字按照从小到大获从大到小的顺序排序,排序方式可由用户设置.程序怎么写呢

来源:学生作业帮 编辑:作业帮 分类:数学作业 时间:2024/04/27 20:25:34
VB题.将用户输入的三个数字按照从小到大获从大到小的顺序排序,排序方式可由用户设置.程序怎么写呢
2012-10-30 14:58_毛线线 | 分类:VB | 浏览19次
要对a、b、c这三个数排序(依顺序为例),先比较前两个数,使得a≤b,再比较b与c,若b≤c,则排序已完成,否则需要交换b与c的值,之后再继续比较a与交换后的b,使得a小于等于b,排序结束.
求高手帮忙写下这个程序.
这里有两种方法.第一种,直接交换,代码比较多,容易出错.第二种,把交换部分写成一个独立过程,中间调用.便于阅读,书写方便,不易出错.用户的关于从大到小和从小到大排序选择,使用Option控件()如图.Private Sub Command1_Click()
    Dim a As Integer
    Dim b As Integer
    Dim c As Integer
    Dim tmp As Integer
    a = Val(Text1.Text)
    b = Val(Text2.Text)
    c = Val(Text3.Text)
    If Option1.Value = True Then
        If a < b Then
            tmp = a
            a = b
            b = tmp
        End If
        If c > a Then
            tmp = b
            b = c
            c = tmp
            tmp = a
            a = b
            b = tmp
        ElseIf c > b Then
            tmp = b
            b = c
            c = tmp
        End If
    Else
        If a > b Then
            tmp = a
            a = b
            b = tmp
        End If
        If c < a Then
            tmp = b
            b = c
            c = tmp
            tmp = a
            a = b
            b = tmp
        ElseIf c < b Then
            tmp = b
            b = c
            c = tmp
        End If
    End If
    Text1.Text = CStr(a)
    Text2.Text = CStr(b)
    Text3.Text = CStr(c)
   
End SubSub swap(x, y)
    Dim tmp
    tmp = x
    x = y
    y = tmp
End SubPrivate Sub Command2_Click()
    Dim a As Integer
    Dim b As Integer
    Dim c As Integer
    Dim tmp As Integer
    a = Val(Text1.Text)
    b = Val(Text2.Text)
    c = Val(Text3.Text)
    If Option1.Value = True Then
        If a < b Then
        End If
        If c > a Then
            swap b, c
            swap a, b
        ElseIf c > b Then
            swap b, c
        End If
    Else
        If a > b Then
            swap a, b
        End If
        If c < a Then
            swap b, c
            swap a, b
        ElseIf c < b Then
            swap b, c
        End If
    End If
    Text1.Text = CStr(a)
    Text2.Text = CStr(b)
    Text3.Text = CStr(c)
   
End Sub