VB方程问题

来源:百度知道 编辑:UC知道 时间:2024/06/14 21:02:55
Private Sub Image3_Click()
Dim a, b, c, d, e, f, g, h As Single
Text1.Text = a
Text2.Text = b
Text3.Text = c
Text4.Text = d
Text5.Text = e
Text6.Text = f
g = (c * e - b * f) / CDbl(a * e - b * d)
h = (a * f - c * d) / CDbl(a * e - b * d)
Text7.Text = g
Text8.Text = h
End Sub

我是在开发二元一次方程,它说代码错了g = (c * e - b * f) / CDbl(a * e - b * d),哪里错呢
Ax+By=C
Dx+Ey=F A,B,C,D,E,F分别为text 1,text 2 ,text3,text 4~~
A,B,C,D,E,F为text

首先1.
Text1.Text = a
Text2.Text = b
Text3.Text = c
Text4.Text = d
Text5.Text = e
Text6.Text = f
全部反掉。你是用abcdef带入程序计算的,这里面却把Text内容全部设置为它们。于是Text内容就是single的初始值=0.最后的计算也没有意义了,还会出现“除数为0”的错误。

2.g = (c * e - b * f) / (a * e - b * d)
这样写即可。乘法除法运算会自动转成Double。

3.Dim a, b, c, d, e, f, g, h As Single
这句声明不严谨。这句声明仅仅将h定位single,其它的依然是变体型。
尤其是后来还要由字符串赋值,它们就变成了字符串型!
正确的应该是
Dim a as single,b as single,c as single,d as single,e as single,f as single,g as single,h as single

4.
Text7.Text = g
Text8.Text = h
这两句依然不严谨。
数值(g,h)型转字符串型,如果使用VB默认的方式(自动转换)会出现空格(如g=1那么转换到text以后就是" 1"。)

于是 最后代码是:
Private Sub Image3_Click()
Dim a as single,b as single,c as single,d as single,e as single,f as single,g as single,h as single
a=val(Text1.Text)
b=val(Text2.Text)
c=val(Text3.Text)
d=val(Text4.Text)
e=val(Text5.Text)
f=val(Text6.Text)
g = (c *