Page 1 of 1

Math-Parser

Posted: Mon Feb 27, 2006 10:46 pm
by Kiffi
Hello,

here is a little piece of PB3.94-Code showing to use the MS-Jet-Engine to
parse mathematical formulas:

Important: Pauls MDB-Lib is required

Code: Select all

Global MathParserDatabase$

Procedure.s GetTempDir()
  TempDir$ = Space(#MAX_PATH+1)
  GetTempPath_(#MAX_PATH+1,TempDir$)
  ProcedureReturn TempDir$
EndProcedure

Procedure DeleteMathParserDatabase()
  If FileSize(MathParserDatabase$ + ".mdb") > 0 : DeleteFile(MathParserDatabase$ + ".mdb") : EndIf
EndProcedure 

Procedure.l InitMathParser()
 
  If InitDatabase()
    DeleteMathParserDatabase()
    If MDB_Create(MathParserDatabase$)
      dbHandle=MDB_Connect(GetTempDir(),"MathParser","","")
      If dbHandle
        If DatabaseQuery("Create Table tblMathParser (fldMathParser LONG)")
          If DatabaseQuery("Insert Into tblMathParser (fldMathParser) Values (1)")
            ProcedureReturn dbHandle
          Else ; Insertion of MathParser-Data failed
          EndIf
        Else ; Table-Creation failed
        EndIf
      Else ; MDB-Connection failed
      EndIf
    Else ; MDB-Creation failed
    EndIf
  Else ; Couldn't init database-system
  EndIf
 
EndProcedure   

Procedure.s GetExpressionResult(Expression$)
 
  If DatabaseQuery("Select (" + Expression$ + ") From tblMathParser Where fldMathParser = 1")
    NextDatabaseRow()
    ReturnValue$ = GetDatabaseString(0)
    If ReturnValue$ = "" : ReturnValue$ = "Couldn't calculate Formula" : EndIf
  Else
    ReturnValue$ = DatabaseError()
  EndIf
 
  ProcedureReturn ReturnValue$
 
EndProcedure

MathParserDatabase$ = GetTempDir() + "MathParser"

DatabaseID = InitMathParser()
   
If DatabaseID = 0
  MessageRequester("", "Couldn't init MathParser")
  DeleteMathParserDatabase()
  End
EndIf

Repeat
  myInput$=InputRequester("MathParser", "Enter your formula to calculate (ESC to exit):", myInput$)
  If myInput$ = "" : Break : EndIf
  MessageRequester("Result:", GetExpressionResult(myInput$))
ForEver
 
CloseDatabase(DatabaseID)

DeleteMathParserDatabase()

MDB_Disconnect("MathParser") 
Have fun ... Kiffi