Calculate a mathematical expression

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
tom_gamer
Posts: 15
Joined: Sat Jun 01, 2013 8:51 am

Calculate a mathematical expression

Post by tom_gamer »

This is not strictly Irrlicht-related, but it may be useful in a project, that uses Irrlicht. :)

With these classes, you can evaluate an mathematical expression like `3.4*PI/sin(4.6+min(4,2)^1.7)`. This can be useful for scripting...
The classes only works on float (f32) values. (no vectors, no double, no matrices, sorry...)

Header
http://codetidy.com/6425/

CPP
http://codetidy.com/6426/

There is a doxygen documentation in the files.

Code: Select all

 
// Create a new object
SCalcExpr *calc = new SCalcExpr();
 
// try to parse the string
if (!calc->readExpr("min(clamp(PI*2.5*round(3.9)/PI,1,2), 3)"))
{
        // readExpr() will show a detailed error message
        printf("Invalid expression!\n");
        return(0);
}
 
f32 f;
// Calculate the result
if (!calc->calcExpr(NULL, f))
{
        // calcExpr() will show a detailed error message
        printf("can't calculate expression\n");
        return(0);
}
 
// we don't need this object anymore
delete calc;
 
printf("Result = %0.3f\n", f);
return(0);
 
SpaceCombat
http://sourceforge.net/projects/spacecombatgame
(OpenSource space combat simulation)
chronologicaldot
Competition winner
Posts: 685
Joined: Mon Sep 10, 2012 8:51 am

Re: Calculate a mathematical expression

Post by chronologicaldot »

Interesting. Could be quite useful for a calculator. Thanks for sharing!
Reminds me of work I'd like to finish on my programming language. Sadly, our setups are completely different or I could use the code you gave.
gerdb
Posts: 194
Joined: Wed Dec 02, 2009 8:21 pm
Location: Dresden, Germany

Re: Calculate a mathematical expression

Post by gerdb »

looks nice, reminds me of the TiGCC SDK for Ti calculators ( CAS )

But, pls dont ever use "using namespace irr/core" in a header file
Post Reply