Folgendes Beispiel zeigt einen sehr einfachen Mathe-Parser. Neben den Grundrechenarten lässt sich das Beispiel auf Klammern und Sinus etc. noch erweitern
Aufruf erfolgt ganz easy mittels
Dim t as Term = Term.Split("1+2")
Console.WriteLine(t.Value())
Prinzipell besteht jeder Term aus einer Operation die zwei Terme miteinander verbindet. Die Terme werden an den Grundrechenoperationen rekursiv zerlegt (Split-Methode). Die neuen Teil-Terme werden den einzelnen Operator-Klassen zugewiesen. Diese zerlegen die Teil-Terme erneut - rekursiv eben.
Public MustInherit Class Term
''' <summary>
''' liefert den Wert des aktuellen Terms
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public MustOverride Function Value() As Integer
''' <summary>
''' die linke Seite der Operation
''' </summary>
''' <remarks></remarks>
Protected left As Term
''' <summary>
''' die rechte Seite der Operation
''' </summary>
''' <remarks></remarks>
Protected right As Term
''' <summary>
''' der Konsturktor wird von den anderen Klassen aufgerufen
''' </summary>
''' <param name="left">die linke Seite der Operation</param>
''' <param name="right">die rechte Seite - jener welcher eben</param>
''' <remarks></remarks>
Protected Sub New(ByVal left As String, ByVal right As String)
If left Is Nothing Or right Is Nothing Then Return
Me.left = Term.Split(left)
Me.right = Term.Split(right)
End Sub
''' <summary>
''' zerlegt den übergebenen Term in die entsprechenden kleineren Terme
''' </summary>
''' <param name="t">dieser Term soll zerlegt werden</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function Split(ByVal t As String) As Term
t = t.Replace(" ", "") ' Leerzeichen sind doof
' Term splitten - Punktrechnung vor Strichrechnung
Dim index As Integer = -1
' -- Addition
index = t.IndexOf("+")
If index > 0 Then Return New Addition(t.Substring(0, index), t.Substring(index + 1))
' -- Subtraktion
index = t.IndexOf("-")
If index > 0 Then Return New Subtraction(t.Substring(0, index), t.Substring(index + 1))
' -- Multiplikation
index = t.IndexOf("*")
If index > 0 Then Return New Multiplication(t.Substring(0, index), t.Substring(index + 1))
' -- Division
index = t.IndexOf("/")
If index > 0 Then Return New Division(t.Substring(0, index), t.Substring(index + 1))
Return New Variable(t)
End Function
End Class
Public Class Addition
Inherits Term
Public Sub New(ByVal left As String, ByVal right As String)
MyBase.New(left, right)
End Sub
Public Overrides Function Value() As Integer
Return left.Value + right.Value
End Function
Public Overrides Function ToString() As String
Return "[" & left.ToString & "+" & right.ToString & "]"
End Function
End Class
Public Class Subtraction
Inherits Term
Public Sub New(ByVal left As String, ByVal right As String)
MyBase.New(left, right)
End Sub
Public Overrides Function Value() As Integer
Return left.Value - right.Value
End Function
Public Overrides Function ToString() As String
Return "[" & left.ToString & "-" & right.ToString & "]"
End Function
End Class
Public Class Multiplication
Inherits Term
Public Sub New(ByVal left As String, ByVal right As String)
MyBase.New(left, right)
End Sub
Public Overrides Function Value() As Integer
Return left.Value * right.Value
End Function
Public Overrides Function ToString() As String
Return "[" & left.ToString & "*" & right.ToString & "]"
End Function
End Class
Public Class Division
Inherits Term
Public Sub New(ByVal left As String, ByVal right As String)
MyBase.New(left, right)
End Sub
Public Overrides Function Value() As Integer
Return left.Value / right.Value
End Function
Public Overrides Function ToString() As String
Return "[" & left.ToString & "/" & right.ToString & "]"
End Function
End Class
Public Class Variable
Inherits Term
Private content As Integer
Public Sub New(ByVal t As String)
MyBase.New(Nothing, Nothing)
If Not Integer.TryParse(t, content) Then Throw New ApplicationException("Term '" & t & "' ist keine Zahl")
End Sub
Public Overrides Function Value() As Integer
Return content
End Function
Public Overrides Function ToString() As String
Return "[" & content & "]"
End Function
End Class
1 Kommentare zum Snippet