[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

textbox input ( 5* 2 ) + (5 * (2 + 3)) - 2 + 3, should get answer 36

Posted on 2006-06-13
11
Medium Priority
?
306 Views
Last Modified: 2010-04-07
textbox input ( 5* 2 ) + (5 * (2 + 3)) - 2 + 3, should get answer 36.

how to do it

Jini
0
Comment
Question by:Jini Jose
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16892445
0
 
LVL 10

Author Comment

by:Jini Jose
ID: 16892452
no not through script control
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16892478
why not? does the job!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Author Comment

by:Jini Jose
ID: 16892493
not possible
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16892504
WHY not possible?
WHY reinvent the wheel?
0
 
LVL 10

Author Comment

by:Jini Jose
ID: 16892515
no time to do
0
 
LVL 10

Author Comment

by:Jini Jose
ID: 16892534
i want the vb function code
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16892790
>no time to do

well, using the vb script control is done in 10 seconds, just check the reference in the project references, put a script control on the form, and use it's eval function.
there will be no faster than that...
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 16893045
Would doing

Eval(txtBox.Value)

bring it up?

txtBox holds your maths  ( 5* 2 ) + (5 * (2 + 3)) - 2 + 3

0
 
LVL 8

Accepted Solution

by:
wraith821 earned 750 total points
ID: 16893258
Here you go. i use it in an  sales commision calculator i built.


'***************** USAGE *************
Msgbox EvaluateExpression(txtBox.text)
'*************************************

'************* MODULE CODE *****************
' Evaluate the expression.
Public Function EvaluateExpression(ByVal expression As String) As Double
Const PREC_NONE = 11
Const PREC_UNARY = 10   ' Not actually used.
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 expr As String
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 all spaces.
    expr = Replace$(expression, " ", "")
    expr_len = Len(expr)
    If expr_len = 0 Then
        EvaluateExpression = 0
        Exit Function
    End If

    ' 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. We keep them here
            ' to make the error messages easier to
        ElseIf ch = "(" Then
            ' Increase the open parentheses count.
            parens = parens + 1

            ' A + or - 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, _
                    "EvaluateExpression", _
                    "Too many )s in '" & _
                    expression & "'"
            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

                ' See if this operator has higher
                ' precedence than the current one.
                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, _
            "EvaluateExpression", "Missing ) in '" & _
            expression & "'"
    End If
   
    ' Hopefully we have the operator.
    If best_prec < PREC_NONE Then
        lexpr = Left$(expr, best_pos - 1)
        rexpr = Mid$(expr, best_pos + 1)
        Select Case Mid$(expr, best_pos, 1)
            Case "^"
                EvaluateExpression = _
                    EvaluateExpression(lexpr) ^ _
                    EvaluateExpression(rexpr)
            Case "*"
                EvaluateExpression = _
                    EvaluateExpression(lexpr) * _
                    EvaluateExpression(rexpr)
            Case "/"
                EvaluateExpression = _
                    EvaluateExpression(lexpr) / _
                    EvaluateExpression(rexpr)
            Case "\"
                EvaluateExpression = _
                    EvaluateExpression(lexpr) \ _
                    EvaluateExpression(rexpr)
            Case "%"
                EvaluateExpression = _
                    EvaluateExpression(lexpr) Mod _
                    EvaluateExpression(rexpr)
            Case "+"
                EvaluateExpression = _
                    EvaluateExpression(lexpr) + _
                    EvaluateExpression(rexpr)
            Case "-"
                EvaluateExpression = _
                    EvaluateExpression(lexpr) - _
                    EvaluateExpression(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.
        EvaluateExpression = EvaluateExpression( _
            Mid$(expr, 2, expr_len - 2))
        Exit Function
    End If

    ' Look for -expr2.
    If Left$(expr, 1) = "-" Then
        EvaluateExpression = -EvaluateExpression( _
            Mid$(expr, 2))
        Exit Function
    End If

    ' Look for +expr2.
    If Left$(expr, 1) = "+" Then
        EvaluateExpression = EvaluateExpression( _
            Mid$(expr, 2))
        Exit Function
    End If
   
    ' Look for Fun(expr2).
    If expr_len > 5 And Right$(expr, 1) = ")" Then
        ' Find the first (.
        pos = InStr(expr, "(")

        If pos > 0 Then
            ' See what the function is.
            lexpr = LCase$(Left$(expr, pos - 1))
            rexpr = Mid$(expr, pos + 1, expr_len - pos - 1)
            Select Case lexpr
                Case "sin"
                    EvaluateExpression = Sin(EvaluateExpression(rexpr))
                    Exit Function
                Case "cos"
                    EvaluateExpression = Cos(EvaluateExpression(rexpr))
                    Exit Function
                Case "tan"
                    EvaluateExpression = Tan(EvaluateExpression(rexpr))
                    Exit Function
                Case "sqr"
                    EvaluateExpression = Sqr(EvaluateExpression(rexpr))
                    Exit Function
                Case "factorial"
                    EvaluateExpression = Factorial(EvaluateExpression(rexpr))
                    Exit Function
                ' Add other functions (including
                ' program-defined functions) here.
            End Select
        End If
    End If


   ' It must be a literal like "2.71828".
    On Error Resume Next
    EvaluateExpression = CDbl(expr)
    status = Err.Number
    On Error GoTo 0
    If status <> 0 Then
        Err.Raise status, _
            "EvaluateExpression", _
            "Error evaluating '" & expression & _
            "' as a constant."
    End If
End Function
' Return the factorial of the expression.
Private Function Factorial(ByVal Value As Double) As Double
Dim result As Double

    ' Make sure the value is an integer.
    If CLng(Value) <> Value Then
        Err.Raise vbObjectError + 1001, _
            "Factorial", _
            "Argument must be an integer in Factorial(" & _
            Format$(Value) & ")"
    End If

    result = 1
    Do While Value > 1
        result = result * Value
        Value = Value - 1
    Loop
    Factorial = result
End Function

0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 16899119
that a huge code ha?
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question