求解两道vb程序设计题 求函数值 50
两道题目都一样 数据如图有两个 求解答 窗体设计也要 展开
Private Sub Command1_Click(Index As Integer)
DetermineFunctionalValue Index
End Sub
Private Sub DetermineFunctionalValue(ByVal i As Integer)
Print Command1(i).Caption + ":";
Dim x As Single, y As Single
x = InputBox("x=", "第(" & i + 1 & ")题", 0)
Select Case i
Case 0
y = Function_1(x)
Case 1
y = Function_2(x)
End Select
Print "x=" & x & "," & "f(x)=" & y
End Sub
Private Function Function_1(ByVal x As Single) As Single
If x < 0 And x <> -3 Then
Function_1 = x ^ 2 + x - 6
ElseIf x >= 0 And x <> 2 And x <> 3 Then
Function_1 = x ^ 2 - 5 * x + 6
Else
Function_1 = x ^ 2 - x - 1
End If
End Function
Private Function Function_2(ByVal x As Single) As Single
If x < 1 And x <> -3 Then
Function_2 = 3 * x / 5
ElseIf x >= 1 And x < 10 Then
Function_2 = Abs(2 - 5 * x)
Else
Function_2 = Sqr(4 * x - 13)
End If
End Function
注:本程序窗体上的两个命令按钮是一个控件数组,其名称都是Command1,下标分别为0和1:
Command1(0),Command1(1);标题(Caption属性)分别为"第(1)题"和"第(2)题"。