Вычислить значения функции - VB (59103)
Формулировка задачи:
Написать программу в Visual Basic для вычисления значения функции. Помогите пожалуйста)
Решение задачи: «Вычислить значения функции»
textual
Листинг программы
- Option Explicit
- Private Enum DotAndCommaEnum
- IsNotPresent
- IsPresent
- End Enum
- Private Type SignType
- DotAndComma As DotAndCommaEnum
- Minus As DotAndCommaEnum
- End Type
- Private mlngContentOfString As SignType
- Private Const A As Single = 1.3
- Private Const B As Single = 1.29
- Private Sub cmdCompute_Click()
- Dim dblRetval As Double, dblResult As Double, strRetval As String, lngStringLength As Long
- strRetval = txtValueX.Text
- lngStringLength = VBA.Strings.Len(txtValueX.Text)
- If lngStringLength = 1 And (txtValueX.Text Like "[-.,]") Or lngStringLength = 0 Then
- VBA.Interaction.MsgBox "Недопустимое значение X.", vbInformation, "Ошибка"
- With txtValueX
- .SetFocus
- .SelStart = 0
- .SelLength = lngStringLength
- End With
- Exit Sub
- End If
- If VBA.Strings.InStr(1, strRetval, ".") <> 0 Then strRetval = _
- VBA.Strings.Replace(strRetval, ".", ",")
- dblRetval = VBA.Conversion.CDbl(strRetval)
- Select Case dblRetval
- Case Is < 1
- dblResult = A * dblRetval + B
- Case 1
- dblResult = VBA.Math.Cos(A * dblRetval)
- Case Is > 1
- dblResult = VBA.Math.Exp(-A * dblRetval) * VBA.Math.Cos(A * dblRetval)
- End Select
- txtValueY.Text = VBA.Conversion.CStr(dblResult)
- End Sub
- Private Sub cmdExit_Click()
- Unload Me
- End Sub
- Private Sub txtValueX_Change()
- If Not txtValueX.Text Like "*[,.]*" Then mlngContentOfString.DotAndComma = IsNotPresent
- End Sub
- Private Sub txtValueX_KeyPress(KeyAscii As Integer)
- If KeyAscii = 45 Then
- If txtValueX.SelLength = 0 Then
- If VBA.Strings.Left$(txtValueX.Text, 1) <> "-" Then
- If txtValueX.SelStart <> 0 Then KeyAscii = 0
- Else
- KeyAscii = 0
- End If
- Else
- If txtValueX.SelStart <> 0 Then KeyAscii = 0
- End If
- ElseIf KeyAscii = 44 Or KeyAscii = 46 Then
- If txtValueX.SelStart = 0 And (VBA.Strings.InStr(1, txtValueX, "-") <> 0) And txtValueX.SelLength = 0 Then
- KeyAscii = 0
- Else
- If mlngContentOfString.DotAndComma = IsNotPresent Then
- mlngContentOfString.DotAndComma = IsPresent
- Else
- If Not txtValueX.SelText Like "*[.,]*" Then KeyAscii = 0
- End If
- End If
- Else
- If VBA.Strings.Chr$(KeyAscii) Like "[0-9]" Then
- If txtValueX.SelLength = 0 Then
- If txtValueX.SelStart = 0 And VBA.Strings.InStr(1, txtValueX.Text, "-") Then KeyAscii = 0
- Else
- If txtValueX.SelStart = 0 And VBA.Strings.InStr(1, txtValueX.SelText, "-") = 0 Then KeyAscii = 0
- End If
- Else
- If KeyAscii <> 8 Then KeyAscii = 0
- End If
- End If
- End Sub
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д