找一个能计算表达式的函数

CoolCHEN 2004-07-25 03:32:31
比如:
((((((((((1+1)*1)*(1+1))*1)*1)*1)*(1+1))*1)*1)*1)*2=?
...全文
268 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinesc 2004-07-25
  • 打赏
  • 举报
回复
呵呵 不错 ~ 谢谢
northwolves 2004-07-25
  • 打赏
  • 举报
回复
大头方法简单
hhjjhjhj 2004-07-25
  • 打赏
  • 举报
回复
Set scr = CreateObject("MSScriptControl.ScriptControl")
scr.Language = "vbscript"
MsgBox scr.Eval("((((((((((1+1)*1)*(1+1))*1)*1)*1)*(1+1))*1)*1)*1)*2")
frankwong 2004-07-25
  • 打赏
  • 举报
回复
收藏先,试试看
还想懒够 2004-07-25
  • 打赏
  • 举报
回复
还有一个办法是运行SQL语句
northwolves 2004-07-25
  • 打赏
  • 举报
回复
'引用microsoft excel 10.0 object library( OR OTHER VERSION)
' add a textbox and a commandbutton to form1
Function result(ByVal x As String)
Dim myobj As Object
Set myobj = CreateObject("excel.sheet")
Set myobj = myobj.Application.ActiveWorkbook.ActiveSheet
myobj.Range("a1").Formula = "= " & x '
result = myobj.Range("a1").Value
If Err.Number > 0 Then MsgBox Err.Description
Set myobj = Nothing

End Function

Private Sub Command1_Click()
Dim x As String
x = "((((((((((1+1)*1)*(1+1))*1)*1)*1)*(1+1))*1)*1)*1)*2"
MsgBox x & "=" & result(x)
End Sub
xuboying 2004-07-25
  • 打赏
  • 举报
回复
函数式要返回值的,楼上做法怎么取到返回值呢?
northwolves 2004-07-25
  • 打赏
  • 举报
回复
Option Explicit
Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long ' API

Private Function ExecuteLine(sCode As String, Optional fCheckOnly As Boolean) As Boolean
ExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function


Private Function result(ByVal x As String) As Single '计算表达式的结果
ExecuteLine "dim x as single"
ExecuteLine "x= " & x
ExecuteLine "clipboard.settext x" '发送到剪切板
result = Clipboard.GetText '从剪切板获取
Clipboard.Clear '清空剪切板
End Function


Private Sub Command1_Click()
Dim x As String
x = "((((((((((1+1)*1)*(1+1))*1)*1)*1)*(1+1))*1)*1)*1)*2"
MsgBox x & "=" & result(x)
End Sub
BlueBeer 2004-07-25
  • 打赏
  • 举报
回复
还有个简单的办法是用WebBrowser控件


test="((((((((((1+1)*1)*(1+1))*1)*1)*1)*(1+1))*1)*1)*1)*2"

WebBrowser1.Navigate "javascript:" & test
xuboying 2004-07-25
  • 打赏
  • 举报
回复
你要的表达式就在Form1的Caption属性中了
xuboying 2004-07-25
  • 打赏
  • 举报
回复
ScriptControl 控件附属于VB 6.0,如果安装后没有看到此一控件,可在光盘的\Common\Tools\VB\Script 目录底下找此一控件, 其文件名为Msscript.ocx

在工程中引用Microsoft Script Control 1.0

Dim Sc As New ScriptControl
Private Sub Form_Load()
Dim Statement As String
Sc.Language = "vbscript"
Sc.AddObject "myform", Me
Statement = "myform.caption=((((((((((1+1)*1)*(1+1))*1)*1)*1)*(1+1))*1)*1)*1)*2"
Sc.ExecuteStatement Statement
End Sub
i0562 2004-07-25
  • 打赏
  • 举报
回复
Private Function EvaluateExpr(ByVal expr As String) As Single
Const PREC_NONE = 11
Const PREC_UNARY = 10
Const PREC_POWER = 9
Const PREC_TIMES = 8
Const PREC_DIV = 7
Const PREC_INT_DIV = 6
Const PREC_MOD = 5
Const PREC_PLUS = 4

Dim is_unary As Boolean
Dim next_unary As Boolean
Dim parens As Integer
Dim pos As Integer
Dim expr_len As Integer
Dim ch As String
Dim lexpr As String
Dim rexpr As String
Dim value As String
Dim status As Long
Dim best_pos As Integer
Dim best_prec As Integer

' Remove leading and trailing blanks.
expr = Trim$(expr)
expr_len = Len(expr)
If expr_len = 0 Then Exit Function

' If we find + or - now, it is a unary operator.
is_unary = True

' So far we have nothing.
best_prec = PREC_NONE

' Find the operator with the lowest precedence.
' Look for places where there are no open
' parentheses.
For pos = 1 To expr_len
' Examine the next character.
ch = Mid$(expr, pos, 1)

' Assume we will not find an operator. In
' that case the next operator will not
' be unary.
next_unary = False

If ch = " " Then
' Just skip spaces.
next_unary = is_unary
ElseIf ch = "(" Then
' Increase the open parentheses count.
parens = parens + 1

' An operator after "(" is unary.
next_unary = True
ElseIf ch = ")" Then
' Decrease the open parentheses count.
parens = parens - 1

' An operator after ")" is not unary.
next_unary = False

' If parens < 0, too many ')'s.
If parens < 0 Then
Err.Raise vbObjectError + 1001, _
"EvaluateExpr", _
"Too many )s in '" & _
expr & "'"
End If
ElseIf parens = 0 Then
' See if this is an operator.
If ch = "^" Or ch = "*" Or _
ch = "/" Or ch = "\" Or _
ch = "%" Or ch = "+" Or _
ch = "-" _
Then
' An operator after an operator
' is unary.
next_unary = True

Select Case ch
Case "^"
If best_prec >= PREC_POWER Then
best_prec = PREC_POWER
best_pos = pos
End If

Case "*", "/"
If best_prec >= PREC_TIMES Then
best_prec = PREC_TIMES
best_pos = pos
End If

Case "\"
If best_prec >= PREC_INT_DIV Then
best_prec = PREC_INT_DIV
best_pos = pos
End If

Case "%"
If best_prec >= PREC_MOD Then
best_prec = PREC_MOD
best_pos = pos
End If

Case "+", "-"
' Ignore unary operators
' for now.
If (Not is_unary) And _
best_prec >= PREC_PLUS _
Then
best_prec = PREC_PLUS
best_pos = pos
End If
End Select
End If
End If
is_unary = next_unary
Next pos

' If the parentheses count is not zero,
' there's a ')' missing.
If parens <> 0 Then
Err.Raise vbObjectError + 1002, _
"EvaluateExpr", "Missing ) in '" & _
expr & "'"
End If

' Hopefully we have the operator.
If best_prec < PREC_NONE Then
lexpr = Left$(expr, best_pos - 1)
rexpr = Right$(expr, expr_len - best_pos)
Select Case Mid$(expr, best_pos, 1)
Case "^"
EvaluateExpr = _
EvaluateExpr(lexpr) ^ _
EvaluateExpr(rexpr)
Case "*"
EvaluateExpr = _
EvaluateExpr(lexpr) * _
EvaluateExpr(rexpr)
Case "/"
EvaluateExpr = _
EvaluateExpr(lexpr) / _
EvaluateExpr(rexpr)
Case "\"
EvaluateExpr = _
EvaluateExpr(lexpr) \ _
EvaluateExpr(rexpr)
Case "%"
EvaluateExpr = _
EvaluateExpr(lexpr) Mod _
EvaluateExpr(rexpr)
Case "+"
EvaluateExpr = _
EvaluateExpr(lexpr) + _
EvaluateExpr(rexpr)
Case "-"
EvaluateExpr = _
EvaluateExpr(lexpr) - _
EvaluateExpr(rexpr)
End Select
Exit Function
End If

' If we do not yet have an operator, there
' are several possibilities:
'
' 1. expr is (expr2) for some expr2.
' 2. expr is -expr2 or +expr2 for some expr2.
' 3. expr is Fun(expr2) for a function Fun.
' 4. expr is a primitive.
' 5. It's a literal like "3.14159".

' Look for (expr2).
If Left$(expr, 1) = "(" And Right$(expr, 1) = ")" Then
' Remove the parentheses.
EvaluateExpr = EvaluateExpr(Mid$(expr, 2, expr_len - 2))
Exit Function
End If

' Look for -expr2.
If Left$(expr, 1) = "-" Then
EvaluateExpr = -EvaluateExpr( _
Right$(expr, expr_len - 1))
Exit Function
End If

' Look for +expr2.
If Left$(expr, 1) = "+" Then
EvaluateExpr = EvaluateExpr( _
Right$(expr, expr_len - 1))
Exit Function
End If

' Look for Fun(expr2).
If expr_len > 5 And Right$(expr, 1) = ")" Then
lexpr = LCase$(Left$(expr, 4))
rexpr = Mid$(expr, 5, expr_len - 5)
Select Case lexpr
Case "sin("
EvaluateExpr = Sin(EvaluateExpr(rexpr))
Exit Function
Case "cos("
EvaluateExpr = Cos(EvaluateExpr(rexpr))
Exit Function
Case "tan("
EvaluateExpr = Tan(EvaluateExpr(rexpr))
Exit Function
Case "sqr("
EvaluateExpr = Sqr(EvaluateExpr(rexpr))
Exit Function
End Select
End If

On Error Resume Next
EvaluateExpr = CSng(expr)
status = Err.Number
On Error GoTo 0
If status <> 0 Then
Err.Raise status, _
"EvaluateExpr", _
"Error evaluating '" & expr & _
"' as a constant."
End If
End Function
helanshan 2004-07-25
  • 打赏
  • 举报
回复
楼主是不是没有说清楚问题啊。。
cso 2004-07-25
  • 打赏
  • 举报
回复
晕!!!!!!!
这是不是个数列啊,如果是的话那你直接用公式不就行了?

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧