A simple expression evaluator that doesn't use OOP.
- It converts the given expression from the infix form to it's postfix form using the Shunting-Yard Algorithm
- Using the postfix form of the expression, it follows this algorithm to get the result:
- Create a stack to store operands (or values).
- Scan the postfix expression and do the following steps for each element.
- If the element is a number, push it into the stack
- If the element is an operator, pop operands for the operator from stack. Evaluate the operator and push the result back to the stack
- When the scan is ended, the number in the stack is the final answer.
- Paranthesis: You can use
(
and)
for the general purposes. - Operators:
+, -, /, *, <>, =, <=, >=, or, and, xor, not, mod, \
-=
is ONLY for comparsion. - Functions: all functions from here.
- You can use real, integer or logical constants. (E.g:
1.14 + 3.36 - 2 or false
) - You can use variables and/or set values for certain variables(pi, e, etc).
- Compile the source with a compiler that supports at least c++11 (works on both Windows and Linux).
- Run the program and insert the expression, e.g:
(1 + (2 * 3) - 2) / 5
and it will return the answer1
.
- Changelog 1.1
- Added support for new operators (
<>, =, <=, >=, or, and, xor, not, mod, \
). - Added support for variables and predefined variables
- Added support for logical constants (
true, false
). - Added debug informations and rules.
- Added support for new operators (
- Changelog 1.2
- Added support for all the functions found here (except DivRem - will be added in next update)
- Fixed an error that made some functions that contain numbers in the name to not be recognized.
- Deleted some useless comments.
- Changelog 1.3
- Added support for function DivRem found here.
- Changed the Result stack from type
long double
tostd::string
so it can support changing the value of a variable after it was defined. - Fixed a bug with the
not
operator, had to change it's precedance.