如何用VB里的 select case 实现 解一元二次方程的?
发布网友
发布时间:2023-08-21 16:18
我来回答
共2个回答
热心网友
时间:2024-02-20 09:01
在窗体上分别放三个文本框用于输入a,b,c的值,再放一个按钮用于求解
private sub command1_click()
dim x as double,x1 as double,x2 as double,a1 as double,a2 as double
a =val(text1.text)
b =val(text2.text)
c =val(text3.text)
cls
currentx=600
currenty=1100
if a =0 then
if b =0 then
msgbox "系数为0,请重新输入!"
text1.setfocus
text1.selstart=0
text1.sellength=len(text1.text)
else
x=-c/b
print "x=";format(x,"0.000")
end if
exit sub
end if
dim n as double
n=b^2-4*a*c
select case n
case 0
print "x1=x2=";format(-b/(2*a),"0.000")
case is >0
x1=(-b+sqr(n)/(2*a)
x2=(-b-sqr(n)/(2*a)
print "x1=";format(x1,"0.000")
currentx=600
currenty=1300
print "x2=";format(x2,"0.000")
case is<0
a1 = -b/(2*a)
a2 = sqr(abs(n))/(2*a)
print "x1=";format(a1,"0.000");"+";format(a2,"0.000");"i"
currentx=600
currenty=1300
print "x2=";format(a1,"0.000");"-";format(a2,"0.000");"i"
end select
end sub
热心网友
时间:2024-02-20 09:02
privat sub command1_click()
dim a,b,c,x1,x2,d as sigle
a=val(textl.text)
b=val(text2.text)
c=val(text3.text)
d=b^2-4*a*c
if d>0 then
x1=(-b+sqr(d))/(2*a)
x2=(-b-sqr(d))/(2*a)
else if d=0 then
x1=(-b/2*a)
x2=x1
else msgbox"方程没有实根"
end if
text4.text="x1=" & x1 & "" & "x2=" & x2
end sub