如何用VB求一元二次方程的虚数解(一定是虚数解)。 10
3个回答
展开全部
vb代码如下:
Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single
Dim d As Single, x1 As Single, x2 As Single
a = InputBox("请输入一元二次方程的系数a")
b = InputBox("请输入一元二次方程的系数b")
c = InputBox("请输入一元二次方程的系数c")
If a = 0 Then
a = InputBox("因为a≠0,你输入的a=0,请重新输入系数a")
End If
d = b * b - 4 * a * c
If d >= 0 Then
x1 = (-b + Sqr(d)) / (2 * a)
x2 = (-b - Sqr(d)) / (2 * a)
Print "系数为"; a; b; c; "的一元二次方程的根分别为"; x1
Print "系数为"; a; b; c; "的一元二次方程的根分别为"; x2
Else
Print "此方程在实数范围内无解"
End If
End Sub
一、按钮“求一元二次方程”的vb代码如下:
Private Sub Command1_Click()
a = Text1.Text
b = Text2.Text
c = Text3.Text
d = b * b - 4 * a * c
If d >= 0 Then
X1 = (-b + Sqr(d)) / (2 * a)
X2 = (-b - Sqr(d)) / (2 * a)
Label4.Caption = X1
Label5.Caption = X2
Else
Label4.Caption = "在实数范围内无解"
End If
End Sub
二、按钮“重置”的vb代码如下:
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Label4.Caption = ""
Label5.Caption = ""
End Sub
三、按钮“退出”的vb代码如下:
Private Sub Command3_Click()
End
End Sub
特殊例子(指定系数c的值为5):
在窗体“Form1.frm”的“Command1_click“事件中编写代码(请不要随便更改其它代码),使之能够实现如下功能:在Text1输入整数a、、Text2输入b,判断一元二次方程ax2+bx+5=0有无实数根。并在Text3文本框中显示判断结果,即有实数根则在Text3文本框中输出“有”,否则输出“无”。
VB程序代码如下:
Private Sub Command1_Click()
dim a as single,b as single
a = Text1.Text
b = Text2.Text
d = b * b - 4 * a * 5
If d >= 0 Then
Text3.Text="有"
Else
Text3.Text="无"
End If
End Sub
如果不用dim定义变量,则上面的输入部分语句改为下面的语句,这样可以把字符变量类型转化为数值类型:
a =val(Text1.Text)
b =val(Text2.Text)
Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single
Dim d As Single, x1 As Single, x2 As Single
a = InputBox("请输入一元二次方程的系数a")
b = InputBox("请输入一元二次方程的系数b")
c = InputBox("请输入一元二次方程的系数c")
If a = 0 Then
a = InputBox("因为a≠0,你输入的a=0,请重新输入系数a")
End If
d = b * b - 4 * a * c
If d >= 0 Then
x1 = (-b + Sqr(d)) / (2 * a)
x2 = (-b - Sqr(d)) / (2 * a)
Print "系数为"; a; b; c; "的一元二次方程的根分别为"; x1
Print "系数为"; a; b; c; "的一元二次方程的根分别为"; x2
Else
Print "此方程在实数范围内无解"
End If
End Sub
一、按钮“求一元二次方程”的vb代码如下:
Private Sub Command1_Click()
a = Text1.Text
b = Text2.Text
c = Text3.Text
d = b * b - 4 * a * c
If d >= 0 Then
X1 = (-b + Sqr(d)) / (2 * a)
X2 = (-b - Sqr(d)) / (2 * a)
Label4.Caption = X1
Label5.Caption = X2
Else
Label4.Caption = "在实数范围内无解"
End If
End Sub
二、按钮“重置”的vb代码如下:
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Label4.Caption = ""
Label5.Caption = ""
End Sub
三、按钮“退出”的vb代码如下:
Private Sub Command3_Click()
End
End Sub
特殊例子(指定系数c的值为5):
在窗体“Form1.frm”的“Command1_click“事件中编写代码(请不要随便更改其它代码),使之能够实现如下功能:在Text1输入整数a、、Text2输入b,判断一元二次方程ax2+bx+5=0有无实数根。并在Text3文本框中显示判断结果,即有实数根则在Text3文本框中输出“有”,否则输出“无”。
VB程序代码如下:
Private Sub Command1_Click()
dim a as single,b as single
a = Text1.Text
b = Text2.Text
d = b * b - 4 * a * 5
If d >= 0 Then
Text3.Text="有"
Else
Text3.Text="无"
End If
End Sub
如果不用dim定义变量,则上面的输入部分语句改为下面的语句,这样可以把字符变量类型转化为数值类型:
a =val(Text1.Text)
b =val(Text2.Text)
展开全部
dim a,b,c
d=b*b-4*a*c
p=-b/(2*a)
q=sqr(-d)/(2*a)
print "x1=" ;p;''+";q;"i'
print "x2=";p;"-"q;"i"
d=b*b-4*a*c
p=-b/(2*a)
q=sqr(-d)/(2*a)
print "x1=" ;p;''+";q;"i'
print "x2=";p;"-"q;"i"
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
解一元二次方程的Vb程序:
在窗体上画一个命令按钮,输入下列代码,运行后可在窗体打印结果。
Private Sub Command1_Click()
dim a as double,b as double ,c as double,x as string
a=inputbox("请输入系数a:")
b=inputbox("请输入系数b:")
c=inputbox("请输入系数c:")
x=QuarEquation(a,b,c)
print x
end sub
printe function QuarEquation(byval a as double,byval b as double,byval c as double) as string
dim a as double,b as double ,c as double,P as double
dim xr as double , xi as double ,x as string,xp as double
dim x1 as string, x2 as string,hh as string
hh=chr(13)+chr(10)
xr=-b/2
p=b^2-4*a*c
select case p
case is >0
xp=sqr(P)/2
x1=xr+xp
x2=xr-xp
x="x1=" & x1 & ";" & hh "x2=" & x2
QuarEquation=x
case is = 0
x="x1=x2=" &xr
QuarEquation=x
case is <0
xi=sqr(abs(P))/2
x1=xr & "+" & "xi"
x2=xr & "-" & "i"
x="X1=" & x1 & ";" & hh "x2=" & x2
QuarEquation=x
end select
end function
没测试过,可能会有问题。你可以试试
在窗体上画一个命令按钮,输入下列代码,运行后可在窗体打印结果。
Private Sub Command1_Click()
dim a as double,b as double ,c as double,x as string
a=inputbox("请输入系数a:")
b=inputbox("请输入系数b:")
c=inputbox("请输入系数c:")
x=QuarEquation(a,b,c)
print x
end sub
printe function QuarEquation(byval a as double,byval b as double,byval c as double) as string
dim a as double,b as double ,c as double,P as double
dim xr as double , xi as double ,x as string,xp as double
dim x1 as string, x2 as string,hh as string
hh=chr(13)+chr(10)
xr=-b/2
p=b^2-4*a*c
select case p
case is >0
xp=sqr(P)/2
x1=xr+xp
x2=xr-xp
x="x1=" & x1 & ";" & hh "x2=" & x2
QuarEquation=x
case is = 0
x="x1=x2=" &xr
QuarEquation=x
case is <0
xi=sqr(abs(P))/2
x1=xr & "+" & "xi"
x2=xr & "-" & "i"
x="X1=" & x1 & ";" & hh "x2=" & x2
QuarEquation=x
end select
end function
没测试过,可能会有问题。你可以试试
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |