三角形面积计算
0421
1. 如图,利用控件工具箱插入四个文本按钮和两个命令按钮,分别调节其位置如图。修改四个文本按钮的大小到合适,插入小写的a、b、c,一次对应Text1,Text2,Text3,修改两个命令按钮的caption为求解和清空。
2. 打开代码窗口,全选其中代码删去,粘贴输入如下代码:
Private Function triArea(ByVal x As Double, ByVal y As Double, ByVal z As Double) 三角形面积函数过程
Dim s As Double, area As Double
s = (x + y + z) / 2
area = Sqr((s - x) * (s - y) * (s - z) * s)
triArea = area
End Function
Private Sub Command1_Click() 事件过程
Dim a, b, c, s As Double
Dim intYesorNo As Integer
a = Val(Text1.Text): b = Val(Text2.Text): c = Val(Text3.Text)
If a + b > c And b + c > a And c + a > b Then
Text4.SelText = "恭喜您,您给的数据能构成三角形,三角形的面积S" & "(" & a & "," & b & "," & c & ")" & "=" & Int(triArea(a, b, c) * 10 ^ 2 + 0.5) / 10 ^ 2 精确到小数点后两位
Text4.SelText = Chr(10)
Else
intYesorNo = subErr()
End If
End Sub
Private Function subErr() As Integer 数据错误提示
subErr = MsgBox("请重新输入", vbYesNo + vbInformation, "很抱歉,您给的数据不能构成三角形!")
End Function
Private Sub Command2_Click() 清空事件
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub
3. 保存。
http://www.nordridesign.cn/viewthread.php?tid=4686&extra=page%3D1%26amp%3Borderby%3Ddateline