求解一元二次方程的VB源代码(可以求共轭复根)

发布时间:2013-01-20 19:03:31   来源:文档文库   
字号:

VERSION 5.00

Begin VB.Form Form1

Caption = "求解一元二次方程"

ClientHeight = 3360

ClientLeft = 555

ClientTop = 1155

ClientWidth = 5760

KeyPreview = -1 'True

LinkTopic = "Form1"

ScaleHeight = 3360

ScaleWidth = 5760

Begin VB.CommandButton Command2

Caption = " (F2)"

Height = 375

Left = 3480

TabIndex = 12

Top = 2760

Width = 1695

End

Begin VB.CommandButton Command1

Caption = " (F1)"

Height = 375

Left = 600

TabIndex = 11

Top = 2760

Width = 1695

End

Begin VB.TextBox Text3

BeginProperty Font

Name = "宋体"

Size = 10.5

Charset = 134

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 375

Left = 4200

TabIndex = 6

Top = 720

Width = 1335

End

Begin VB.TextBox Text2

BeginProperty Font

Name = "宋体"

Size = 10.5

Charset = 134

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 375

Left = 2400

TabIndex = 5

Top = 720

Width = 1335

End

Begin VB.TextBox Text1

BeginProperty Font

Name = "宋体"

Size = 10.5

Charset = 134

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 375

Left = 480

TabIndex = 4

Top = 720

Width = 1335

End

Begin VB.Label Label10

BeginProperty Font

Name = "宋体"

Size = 10.5

Charset = 134

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 615

Left = 600

TabIndex = 14

Top = 1680

Visible = 0 'False

Width = 5055

End

Begin VB.Label Label9

Caption = "x="

BeginProperty Font

Name = "宋体"

Size = 10.5

Charset = 134

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 375

Left = 120

TabIndex = 13

Top = 1680

Visible = 0 'False

Width = 375

End

Begin VB.Label Label8

BeginProperty Font

Name = "宋体"

Size = 10.5

Charset = 134

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 615

Left = 600

TabIndex = 10

Top = 2040

Width = 5055

End

Begin VB.Label Label7

BeginProperty Font

Name = "宋体"

Size = 10.5

Charset = 134

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 615

Left = 600

TabIndex = 9

Top = 1320

Width = 5055

End

Begin VB.Label Label6

Caption = "x2="

BeginProperty Font

Name = "宋体"

Size = 10.5

Charset = 134

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 375

Left = 120

TabIndex = 8

Top = 2040

Width = 375

End

Begin VB.Label Label5

Caption = "x1="

BeginProperty Font

Name = "宋体"

Size = 10.5

Charset = 134

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 375

Left = 120

TabIndex = 7

Top = 1320

Width = 375

End

Begin VB.Label Label4

Caption = "c:"

BeginProperty Font

Name = "宋体"

Size = 10.5

Charset = 134

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 375

Left = 3960

TabIndex = 3

Top = 720

Width = 255

End

Begin VB.Label Label3

Caption = "b:"

BeginProperty Font

Name = "宋体"

Size = 10.5

Charset = 134

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 375

Left = 2040

TabIndex = 2

Top = 720

Width = 255

End

Begin VB.Label Label2

Caption = "a:"

BeginProperty Font

Name = "宋体"

Size = 10.5

Charset = 134

Weight = 400

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 375

Left = 240

TabIndex = 1

Top = 720

Width = 240

End

Begin VB.Label Label1

Caption = "求解一元二次方程"

BeginProperty Font

Name = "隶书"

Size = 15.75

Charset = 134

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

ForeColor = &H00800000&

Height = 375

Left = 1560

TabIndex = 0

Top = 180

Width = 2850

End

End

Attribute VB_Name = "Form1"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

Private Sub Form_load()

Command1.Caption = " (F1)" '这两句代码是防止此程序在其他电脑上运行时按钮上的字幕只显示第一个字。

Command2.Caption = " (F2)"

End Sub

Private Sub Command1_Click()

Dim a, b, c, d, x, x1, x2, realpart, imagpart As Double '若将x1,x2,x定义为String,则当修复-1x2x)<00x2x)<1VB不显示整数部分的“0的问题时,添加的“0与小数点间将有空格,影响显示效果。

a = Val(Text1.Text)

b = Val(Text2.Text)

c = Val(Text3.Text)

If a = 0 Then ' 防止a=0时堆栈溢出。

Label7.Caption = ""

Label8.Caption = ""

Label10.Caption = ""

Label5.Visible = True '恢复默认设置。

Label6.Visible = True

Label7.Visible = True

Label8.Visible = True

Label9.Visible = False

Label10.Visible = False

Beep '响铃。

msgbox "此方程不是一元二次方程。", , "错误提示" '提示用户a不能为0

Exit Sub

Else

d = b ^ 2 - 4 * a * c

If d > 0 Then

x1 = (-b + Sqr(d)) / (2 * a)

x2 = (-b - Sqr(d)) / (2 * a)

If x1 > 0 And Left(Str(x1), 1) = "." Then '0时,显示出小数点前的0(在VB的默认条件下,整数部分的“0不显示)。

x1 = "0" & x1 '这里采用字符串拼接的方法解决这一问题

End If

If x1 < 0 And Mid(Str(x1), 2, 1) = "." Then '-1时,显示出小数点和负号之间的0(在VB的默认条件下,整数部分的“0不显示)。

x1 = "-0" & Abs(x1)

End If

If x2 > 0 And Left(Str(x2), 1) = "." Then '同上。

x2 = "0" & x2

End If

If x2 < 0 And Mid(Str(x2), 2, 1) = "." Then

x2 = "-0" & Abs(x2)

End If

Label5.Visible = True

Label6.Visible = True

Label7.Visible = True

Label8.Visible = True

Label9.Visible = False

Label10.Visible = False

Label7.Caption = x1

Label8.Caption = x2

End If

ephemeral = 2

If d = 0 Then

x = (-b) / (2 * a)

If x > 0 And Left(Str(x), 1) = "." Then '同上。

x = "0" & x

End If

If x < 0 And Mid(Str(x), 2, 1) = "." Then

x = "-0" & Abs(x)

End If

Label5.Visible = False

Label6.Visible = False

Label7.Visible = False

Label8.Visible = False

Label9.Visible = True

Label10.Visible = True

Label10.Caption = x

End If

If d < 0 Then

realpart = -b / (2 * a)

imagpart = Sqr(-d) / (2 * a)

Label5.Visible = True

Label6.Visible = True

Label7.Visible = True

Label8.Visible = True

Label9.Visible = False

Label10.Visible = False

If realpart = 0 Then

If Abs(imagpart) = 1 Then

Label7.Caption = "i"

Label8.Caption = "-i"

Else

If Left(Str(Abs(imagpart)), 1) = "." Then

Label7.Caption = Format(Abs(imagpart), "0.###############") & "i" '运用格式化输出函数加上被VB省略的“0

Label8.Caption = "-" & Format(-Abs(imagpart), "0.###############") & "i"

Else

Label7.Caption = Str(Abs(imagpart)) & "i"

Label8.Caption = "-" + Str(Abs(imagpart)) & "i"

End If

End If

Else

If realpart > O And Left(Str(realpart), 1) = "." Then

If Abs(imagpart) = 1 Then

Label7.Caption = "0" & Str(realpart) & "+i"

Label8.Caption = "0" & Str(realpart) & "-i"

Else

If Left(Str(Abs(imagpart)), 1) = "." Then

Label7.Caption = "0" & Str(realpart) & "+" & Format(Abs(imagpart), "0.###############") & "i"

Label8.Caption = "0" & Str(realpart) & "-" & Format(Abs(imagpart), "0.###############") & "i"

Else

Label7.Caption = "0" & Str(realpart) & "+" & Str(Abs(imagpart)) & "i"

Label8.Caption = "0" & Str(realpart) & "-" & Str(Abs(imagpart)) & "i"

End If

End If

Else

If realpart < 0 And Mid(Str(realpart), 2, 1) = "." Then

If Abs(imagpart) = 1 Then

Label7.Caption = "-0" & Str(Abs(realpart)) & "+i"

Label8.Caption = "-0" & Str(Abs(realpart)) & "-i"

Else

If Left(Str(Abs(imagpart)), 1) = "." Then

Label7.Caption = "-0" & Str(Abs(realpart)) & "+" & Format(Abs(imagpart), "0.###############") & "i"

Label8.Caption = "-0" & Str(Abs(realpart)) & "-" & Format(Abs(imagpart), "0.###############") & "i"

Else

Label7.Caption = "-0" & Str(Abs(realpart)) & "+" & Str(Abs(imagpart)) & "i"

Label8.Caption = "-0" & Str(Abs(realpart)) & "-" & Str(Abs(imagpart)) & "i"

End If

End If

Else

If Abs(imagpart) = 1 Then

Label7.Caption = Str(Abs(realpart)) & "+i"

Label8.Caption = Str(Abs(realpart)) & "-i"

Else

If Left(Str(Abs(imagpart)), 1) = "." Then

Label7.Caption = Str(Abs(realpart)) & "+" & Format(Abs(imagpart), "0.###############") & "i"

Label8.Caption = Str(Abs(realpart)) & "-" & Format(Abs(imagpart), "0.###############") & "i"

Else

Label7.Caption = Str(Abs(realpart)) & "+" & Str(Abs(imagpart)) & "i"

Label8.Caption = Str(Abs(realpart)) & "-" & Str(Abs(imagpart)) & "i"

End If

End If

End If

End If

End If

End If

End If

End Sub

Private Sub Command2_Click()

Text1.Text = ""

Text2.Text = ""

Text3.Text = ""

Label7.Caption = ""

Label8.Caption = ""

Label5.Visible = True

Label6.Visible = True

Label7.Visible = True

Label8.Visible = True

Label9.Visible = False

Label10.Visible = False

Text1.SetFocus

End Sub

Private Sub Form_Resize() '阻止用户任意改变窗体大小。

If Me.WindowState = 0 Then '当窗体窗口运行时的课件状态为正常状态时才起作用,保证最大化的正常运行。

Form1.Height = 3870

Form1.Width = 5880

End If

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer) '使用户按回车键后光标自动跳转至Text2。下面的代码的功能类似。

If KeyAscii = 13 Then

KeyAscii = 0 '避免用户按回车键后喇叭发出声响,下同。

Text2.SetFocus

End If

End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then

KeyAscii = 0

Text3.SetFocus

End If

End Sub

Private Sub Text3_KeyPress(KeyAscii As Integer) '用户可通过按回车键实现光标在3个文本框之间循环。

If KeyAscii = 13 Then

KeyAscii = 0

Text1.SetFocus

End If

End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Const key_F1 = &H70 '定义F1键,以增加程序可读性,下同。

If KeyCode = key_F1 Then '若用户按F1键,则执行Command1内的程序。

Call Command1_Click

End If

Const key_F2 = &H71

If KeyCode = key_F2 Then

Call Command2_Click

End If

End Sub

本文来源:https://www.2haoxitong.net/k/doc/023312b6c77da26925c5b0fb.html

《求解一元二次方程的VB源代码(可以求共轭复根).doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式