hi
everbody
i need some help
can anybody tell me about how to calculate the expression which is stored as a string...

Please help me

singh_sunju,
I decieded to post the code here instead of pm as others can look at it and give better ways of handeling the code.
On form1 add two textboxes and a button. Change the names of the textboxes to txtInput and txtOutput and the name of the button to btnEvaluate. The txtInput is where you will type in your formula and of course the txtOutput is where the results will be sorted. Add this code:

Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompile.Click
        Evaluate()
    End Sub

Now add this code to a module.

Module Module1
    Private Tokens As ArrayList = New ArrayList
    Private Index As Integer

    Public Sub Evaluate()
        ' First we need to parse the line. We will place each token in the words arraylist for
        ' later use.
        ParseLine(Form1.txtInput.Text)
        ' Now for the hard-easy part - evaluating the line and placing the results into a textbox
        ' Reset Index
        Index = 0
        Form1.txtOutput.Text = Expression()
    End Sub

    Public Function Expression()
        Dim x As String = SimpleExpression()
        Dim y As String
        If Index = Tokens.Count Then Return x
        If Tokens(Index) = "=" Then
            y = SimpleExpression()
            x = CStr(CDbl(x) = CDbl(y))
        End If
        Return x
    End Function

    Private Function SimpleExpression() As String
        Dim x As String, y As String
        x = Term()
        If Index = Tokens.Count Then Return x
        If Tokens(Index) = "+" Then
            Index += 1
            y = SimpleExpression()
            x = CStr(CDbl(x) + CDbl(y))
        End If
        If Index = Tokens.Count Then Return x
        If Tokens(Index) = "-" Then
            Index += 1
            y = SimpleExpression()
            x = CStr(CDbl(x) - CDbl(y))
        End If
        Return x
    End Function

    Private Function Term() As String
        Dim x As String, y As String
        x = SignedFactor()
        If Index = Tokens.Count Then Return x
        Select Case Tokens(Index)
            Case "*"
                Index += 1
                y = Term()
                x = CStr(CDbl(x) * CDbl(y))
            Case "/"
                Index += 1
                y = Term()
                x = CStr(CDbl(x) / CDbl(y))
        End Select
        Return x
    End Function

    Private Function SignedFactor() As String
        Dim x As String, Signed As Boolean
        If Tokens(Index) = "+" Then
            Index += 1
        ElseIf Tokens(Index) = "-" Then
            Index += 1
            Signed = True
        End If
        x = Factor()
        If Signed Then x = "-" & x.Trim
        Return x
    End Function

    Private Function Factor() As String
        Dim x As String = ""
        If Tokens(Index) = "(" Then
            Index += 1
            x = Expression()
            If Tokens(Index) <> ")" Then
                MessageBox.Show("Expected a ')'", "Program closing due to error")
                Stop
            End If
            Index += 1
            Return x
        End If
        If Tokens(Index) = "sin" Then
            Index += 1
            If Tokens(Index) <> "(" Then
                MessageBox.Show("Expected a '(", "Program ending")
                Stop
            End If
            Index += 1
            x = Expression()
            x = CStr(Math.Sin(CDbl(x)))
            If Tokens(Index) <> ")" Then
                MessageBox.Show("Expected a ')", "Program ending")
                Stop
            End If
            Index += 1
        End If
        If Index >= Tokens.Count Then
            Return x
        Else
            x = Tokens(Index)
            Index += 1
        End If
        Return x
    End Function

    Private Sub ParseLine(ByVal s As String)
        ' Reset word arraylist in case we have used it before
        Tokens.Clear()
        ' We will get each token and place it in the words arraylist
        Do While s.Length <> 0
            Tokens.Add(GetWord(s))
        Loop
        ' If you want to check the words(tokens) then uncomment the below lines.
        'For x As Int16 = 0 To Words.Count - 1
        '    MessageBox.Show(Words.Item(x))
        'Next
    End Sub

    Private Function GetWord(ByRef s As String) As String
        ' Get the first character in the string
        Dim curChar As Char = s.Substring(0, 1)
        Dim x As String         'this is a temp hold string
        ' reset index
        Index = 0
        ' Check if this character is a letter "a" to "z" or "A" to "Z"
        If Char.IsLetter(curChar) Then
            ' It is a letter so keep getting leters untill a non-letter is reached
            ' This could be variables or math expressions. This version does not do variables.
            ' It does handle sin, tan, asin, acos, atan, abs and pi
            Do While Char.IsLetter(curChar) And Index <= s.Length
                Index += 1
                curChar = s.Substring(Index, 1)
            Loop
            x = s.Substring(0, Index)
            s = s.Substring(Index)

        ElseIf Char.IsDigit(curChar) Then
            ' Gets the number
            Do While Char.IsDigit(curChar)
                Index += 1
                If Index = s.Length Then
                    'Index -= 1
                    Exit Do
                Else
                    curChar = s.Substring(Index, 1)
                End If
            Loop
            x = s.Substring(0, Index)
            s = s.Substring(Index)
        Else
            ' Gets the special character  + - * / ( )
            ' More can be added
            x = curChar
            s = s.Substring(1)
        End If
        ' Returns the selected token
        Return x
    End Function

End Module

If you have any questions please post here. I welcome any improvements to the code. More could be added but I kept it to a minimum. I am sure you can add other tokens. There is min error checking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.