VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Expression parser

by Mark van Cuijk (2 Submissions)
Category: Math/Dates
Compatability: Visual Basic 5.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (5 Votes)

This module can be used to parse a mathematical expression given as a string. The result is just a double value, which can be used for anything you'd like to.

Inputs
A string containing an expression. Spaces between operators and operands are ignored. The following operators are supported: - exponentiations (^) - multiplications (*) - divisions (/) - modulus (%) - additions (+) - substractions (-) Besides it's possible to use some built-in functions, putting parameters between braces: - sin, asin, cos, acos, tan, atan - int (returns integer part) - frac (returns decimal part) - log (logarithm with base 10) - ln (natural logarithm with base e) - abs (absolute value) - sign (returns -1, 0 or 1) - rnd (random number between 0 and param) - sqrt (square root) You can define your own functions. These functions are implemented in BasicFunctions. I haven't implemented variable support, because the best way to do so depends on how you're going to use this code. The ReadVariable-function is called when the value of a variable is needed. It's your own job to implement it. Last but not least sub-expressions are supported by encapsulating them with braces. Example (showing some features): 12 * (14-36 ^ (-sin(-4*sqrt(18)))) / (15 * rnd(4) +1)
Assumes
Just call ParseExpression("1+1") to see what happens. Variable support isn't implemented.
Code Returns
A double value containing the result.
Side Effects
Operator precedence: 1. Functions and variables 2. Sub-expressions 3. Exponentiations (^) 4. Multiplications (*) and divisions (/) 5. Modulus (%) 6. Additions (+) and substractions (-)

Rate Expression parser

Download Expression parser

Download Expression parser (4 KB)

Expression parser Comments

No comments have been posted about Expression parser. Why not be the first to post a comment about Expression parser.

Post your comment

Subject:
Message:
0/1000 characters