Как сделать evaluate формулы, которая введена в TextBox? - VB
Формулировка задачи:
Господа, подскажите пожалуйста как сделать evaluate формулы которая введена в TextBox.
Я понял что в Basice нет функции которая вычисляет формулу например как в FoxPro
evaluate(). Неужели надо делать синтаксический разбор формулы вручную?
Решение задачи: «Как сделать evaluate формулы, которая введена в TextBox?»
textual
Листинг программы
Private Sub Command1_Click () Dim n As Double If e_eval(Text1.Text, n) Then MsgBox n End If End Sub Add the following code in the (General)(Declaration) section of Form1: Dim e_input As String ' Expression input string. Dim e_tok As String ' Current token kind. Dim e_spelling As String ' Current token spelling. Dim e_error As Integer ' Tells if syntax error occurred. ' e_eval ' Evaluate a string containing an infix numeric expression. ' If successful, return true and place result in . ' This is the top-level function in the expression evaluator. Function e_eval (ByVal s As String, value As Double) As Integer ' Initialize. e_error = 0 e_input = s Call e_nxt ' Evaluate. value = e_prs(1) ' Check for unrecognized input. If e_tok <> '' And Not e_error Then MsgBox 'syntax error, token = '' + e_spelling + ''' e_error = -1 End If e_eval = Not e_error End Function ' e_prs ' Parse an expression, allowing operators of a specified ' precedence or higher. The lowest precedence is 1. ' This function gets tokens with e_nxt and recursively ' applies operator precedence rules. Function e_prs (p As Integer) As Double Dim n As Double ' Return value. Dim fun As String ' Function name. ' Parse expression that begins with a token (precedence 12). If e_tok = 'num' Then ' number. n = Val(e_spelling) Call e_nxt ElseIf e_tok = '-' Then ' unary minus. Call e_nxt n = -e_prs(11) ' Operand precedence 11. ElseIf e_tok = 'not' Then ' logical NOT. Call e_nxt n = Not e_prs(6) ' Operand precedence 6. ElseIf e_tok = '(' Then ' parentheses. Call e_nxt n = e_prs(1) Call e_match(')') ElseIf e_tok = 'id' Then ' Function call. fun = e_spelling Call e_nxt Call e_match('(') n = e_prs(1) Call e_match(')') n = e_function(fun, n) Else If Not e_error Then MsgBox 'syntax error, token = '' + e_spelling + ''' e_error = -1 End If Loop While is_id% e_input = c + e_input ' Check for keyword. is_keyword = -1 Select Case LCase$(e_spelling) Case 'and' Case 'eqv' Case 'imp' Case 'mod' Case 'not' Case 'or' Case 'xor' Case Else: is_keyword = 0 End Select If is_keyword Then e_tok = LCase$(e_spelling) End If ' Check for <=, >=, <>. Case '<', '>' e_tok = c c = Left$(e_input, 1) If c = '=' Or c = '>' Then e_tok = e_tok + c e_input = Mid$(e_input, 2) End If ' Single character token. Case Else e_tok = c End Select If e_spelling = '' Then e_spelling = e_tok End If End Sub ' e_match ' Check the current token and skip past it. ' This function helps with syntax checking. Sub e_match (token As String) If Not e_error And e_tok <> token Then MsgBox 'expected ' + token + ', got '' + e_spelling + ''' e_error = -1 End If Call e_nxt End Sub
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д