jbutcher5 / MathEvaluator

A simple but effective C++ math evaluator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MathEvaluator

MathEvaluator is quite a basic C++ library that evaluates infix math expressions. MathEvaluator has a focus on simplicity, simply one header file and implementation file that you include and compile into your project and only a few commands to get a working evalutation.

Compiling

Standard C++

To compile ./test/test.cpp you must compile ./include/evaluator.cpp alongside it. This can be done with g++ ./test/test.cpp ./include/evaluator.cpp.

Make

You can compile the test cases with make cd build make.

Webassembly

Install emscripten and compile ./test/test.cpp with em++ ./test/test.cpp ./include/evaluator.cpp.

Examples

Basic Eval

double result = evaluate("1+1"); // returns 2

Basic Eval (With MathEvaluator Class)

MathEvaluator evalutor;

double result = evalutor.eval("1+1"); // returns 2

External Variable Eval

MathEvaluator evalutor;

double x = 20;

evalutor.appendVariable("x", x);

double result = evalutor.eval("1+x"); // returns 21

Updating External Variable Eval

MathEvaluator evalutor;

double x = 20;

evalutor.appendVariable("x", x);

double result1 = evalutor.eval("1+x"); // returns 21

x = 10;

double result2 = evalutor.eval("1+x"); // returns 11

Functions Eval

MathEvaluator evalutor;
double result = evalutor.eval("sin(1.5707963267948966)"); // returns 1

About

A simple but effective C++ math evaluator


Languages

Language:C++ 42.1%Language:C 24.6%Language:CMake 20.0%Language:Makefile 13.2%Language:TypeScript 0.1%