1、VERSION 5.00Begin VB.Form Form1 BorderStyle = 4 Fixed ToolWindowCaption = “Form1“ClientHeight = 4995ClientLeft = 5550ClientTop = 4260ClientWidth = 5295BeginProperty Font Name = “宋体“Size = 6.75Charset = 134Weight = 400Underline = 0 FalseItalic = 0 FalseStrikethrough = 0 FalseEndPropertyLinkTopic = “F
2、orm1“MaxButton = 0 FalseMinButton = 0 FalseScaleHeight = 333ScaleMode = 3 PixelScaleWidth = 353ShowInTaskbar = 0 FalseStartUpPosition = 1 所有者中心Begin VB.TextBox Text5 BeginProperty Font Name = “宋体“Size = 12Charset = 134Weight = 400Underline = 0 FalseItalic = 0 FalseStrikethrough = 0 FalseEndPropertyF
3、oreColor = &H00000000&Height = 1140Left = 2040MultiLine = -1 TrueTabIndex = 8Top = 3600Width = 2655EndBegin VB.CommandButton Command1 Caption = “求优“BeginProperty Font Name = “楷体_GB2312“Size = 15Charset = 134Weight = 400Underline = 0 FalseItalic = 0 FalseStrikethrough = 0 FalseEndPropertyHeight = 480
4、Left = 360TabIndex = 7Top = 3840Width = 1095EndBegin VB.TextBox Text4 BeginProperty Font Name = “楷体_GB2312“Size = 15Charset = 134Weight = 400Underline = 0 FalseItalic = 0 FalseStrikethrough = 0 FalseEndPropertyHeight = 420Left = 2040TabIndex = 6Text = “0.005“Top = 2760Width = 2655EndBegin VB.TextBox
5、 Text3 BeginProperty Font Name = “楷体_GB2312“Size = 15Charset = 134Weight = 400Underline = 0 FalseItalic = 0 FalseStrikethrough = 0 FalseEndPropertyHeight = 420Left = 3000TabIndex = 4Text = “3“Top = 1920Width = 615EndBegin VB.TextBox Text2 BeginProperty Font Name = “楷体_GB2312“Size = 15Charset = 134We
6、ight = 400Underline = 0 FalseItalic = 0 FalseStrikethrough = 0 FalseEndPropertyHeight = 420Left = 2040TabIndex = 3Text = “-1“Top = 1920Width = 615EndBegin VB.TextBox Text1 BeginProperty Font Name = “楷体_GB2312“Size = 15Charset = 134Weight = 400Underline = 0 FalseItalic = 0 FalseStrikethrough = 0 Fals
7、eEndPropertyHeight = 420Left = 1320TabIndex = 0Text = “x2-x+2“Top = 1080Width = 3375EndBegin VB.Label Label3 Caption = “搜索精度:“BeginProperty Font Name = “楷体_GB2312“Size = 15Charset = 134Weight = 400Underline = 0 FalseItalic = 0 FalseStrikethrough = 0 FalseEndPropertyHeight = 375Left = 360TabIndex = 5
8、Top = 2760Width = 1575EndBegin VB.Label Label2 Caption = “搜索区间:( , )“BeginProperty Font Name = “楷体_GB2312“Size = 15Charset = 134Weight = 400Underline = 0 FalseItalic = 0 FalseStrikethrough = 0 FalseEndPropertyHeight = 375Left = 360TabIndex = 2Top = 1920Width = 3855EndBegin VB.Label Label1 Caption =
9、“f(x)=“BeginProperty Font Name = “楷体_GB2312“Size = 15.75Charset = 134Weight = 400Underline = 0 FalseItalic = 0 FalseStrikethrough = 0 FalseEndPropertyHeight = 495Left = 360TabIndex = 1Top = 1080Width = 1095EndEndAttribute VB_Name = “Form1“Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable =
10、FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = FalseOption ExplicitPrivate Declare Function EbExecuteLine Lib “vba6.dll“ (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As LongPrivate Function ExecuteLine(sCode As String, Option
11、al fCheckOnly As Boolean) As BooleanExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, Abs(fCheckOnly) = 0End FunctionFunction FunVal(ByVal f As String, ByVal x As Double) As Doublef = Replace(f, “x“, “(“ & Str(x) & “)“)ExecuteLine “dim f as double“ExecuteLine “f= “ & fExecuteLine “clipboard.settext
12、 f“FunVal = Clipboard.GetTextEnd FunctionPrivate Sub Command1_Click()Dim a As Double, b As Double, e As DoubleDim a1 As Double, a2 As DoubleDim f1 As Double, f2 As DoubleDim fx As String, iCount As Integerfx = Text1 表达式a = Val(Text2): b = Val(Text3) 搜索区间e = Val(Text4) 搜索精度a1 = b - 0.618 * (b - a)a2
13、= a + 0.618 * (b - a)f1 = FunVal(fx, Str(a1)f2 = FunVal(fx, Str(a2)While b - a eiCount = iCount + 1If f1 = f2 Thena = a1: a1 = a2: f1 = f2a2 = a + 0.618 * (b - a)f2 = FunVal(fx, a2)Elseb = a2: a2 = a1: f2 = f1a1 = b - 0.618 * (b - a)f1 = FunVal(fx, a1)End IfWendText5 = “求优结果:“ + vbCrLfText5 = Text5
14、+ “函数在 x=“ + Format(a + b) / 2, “0.#“) + “处取得极值,“Text5 = Text5 + “极值为:“ + Format(FunVal(fx, (a + b) / 2), “0.#“) + vbCrLfText5 = Text5 + “叠代次数:“ + Str(iCount)End SubPrivate Sub Form_Load()Dim sTitle As String, i As IntegerShowsTitle = “0.618法一维求优“Me.Font.Size = 24Me.Font.Name = “楷体_GB2312“RandomizeFor i = 1 To 10Me.CurrentX = (Me.ScaleWidth - Me.TextWidth(sTitle) / 2 + 0.5 * iMe.CurrentY = 10 + i * 0.4Me.ForeColor = QBColor(Int(16 * Rnd()Me.Print sTitleNext iEnd SubPrivate Sub Text1_DblClick()Text1.SelStart = 0Text1.SelLength = Len(Text1)End Sub