求解方程ax*x+bx+c=0,使用select case语句

解决方案 »

  1.   

    是不是Case几个解的问题?
    只是原来的公式都不记得了,好象是分了
    a=0,a>0,a<0及
    △=0,△>0,△<0等情况.
      

  2.   

    以前上课时随便写的二分法解方程的代码,很不完善。
    Private Function fun(x As Single)
        fun = 2 * x ^ 3 - 6 * x ^ 2 + 5 * x + 8
    End Function
    'a为有根区间的下限
    'b为有根去件的上限
    'tol为收敛的精度
    'max为最大迭带次数
    Private Function bisect(a As Long, b As Long, tol As Single, max As Long)
        Dim x() As Single, xx() As Single
        Dim y() As Single, yy() As Single
        Dim result1() As Single, result2() As Single
        ReDim x(1 To max) As Single, xx(1 To max) As Single
        ReDim y(1 To max) As Single, yy(1 To max) As Single
        ReDim result1(1 To max) As Single, result2(1 To max) As Single
        
        x(1) = a: xx(1) = b
        y(1) = fun(x(1)): yy(1) = fun(xx(1))
        If y(1) * yy(1) > 0 Then
            MsgBox "在区间端点函数同号"
            Exit Function
        End If
        For i = 1 To max
            result1(i) = (x(i) + xx(i)) / 2: result2(i) = fun(result1(i))
            If Abs(result1(i) - x(i)) < tol Then
                MsgBox "二分法已收敛"
                Exit Function
            End If
            If result2(i) = 0 Then
                MsgBox "获得准确解"
            ElseIf result2(i) * y(i) < 0 Then
                x(i + 1) = x(i): y(i + 1) = y(i)
                xx(i + 1) = result1(i): yy(i + 1) = result2(i)
            Else
                x(i + 1) = result1(i): y(i + 1) = result2(i)
                xx(i + 1) = xx(i): yy(i + 1) = yy(i)
            End If
            iter = i
            If iter >= max Then
                MsgBox "没有得到满足精度要求的根"
            End If
            bisect = result1(i)
        Next
          
    End FunctionPrivate Sub Command1_Click()MsgBox bisect(-100, 100, 0.0001, 2000)End Sub有兴趣就自己该该吧。…………