VB编程题

来源:百度知道 编辑:UC知道 时间:2024/09/26 13:08:40
1. 编程计算下列分段函数值:
x2+x-6, x<0 且x≠-3

f(x)= x2-5x+6 0≤x<10且x≠2且x≠3

x2-x-1 其他

Private Sub Command1_Click()
IsNumber
ResultOfFunction
End Sub

Private Sub Form_Load()
Text1.Text = ""
End Sub

Private Sub IsNumber()
If IsNumeric(Text1.Text) = False Then
MsgBox "请输入一个数字"
Exit Sub
End If
End Sub

Private Function ResultOfFunction()
Dim x, fn As Double
x = Val(Text1.Text)
If x < 0 And x <> -3 Then
fn = x * x + x - 6
ElseIf x >= 0 And x <= 10 And x <> 2 And x <> 3 Then
fn = x * x - 5 * x + 6
Else
fn = x * x - x - 1
End If
Label1.Caption = fn
End Function

Private Sub Command1_Click()
Dim x As Integer
x = InputBox("")
Select Case x
Case Is < 0 And x <> -3
y = x ^ 2 + x - 6
Case 0 To 9 And x <> 2 And x <> 3
y = x ^ 2 - 5 * x + 6
Case Else
y = x ^ 2 - x - 1
End Select
MsgBox y
End Sub