Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello how can i convert a string mathematic expression to float.
Here is an example:

"((23.5*2)+(45+0.4)-1.3)"
function should return: 91.1

Thank you
Posted

You have to know that C++ is a compiled language. Therefore, at runtime you cannot easily interpret a language expression. What you need to employ is a library (or your own code) that interprets and evaluates the expression.

Look for example here: http://fastmathparser.codeplex.com/[^]

or here: http://muparser.beltoforion.de/[^]
 
Share this answer
 
You need an evaluator. Have a look here: Expression evaluator : using RPN[^]
 
Share this answer
 
.NET Rational (fraction) value type using Decimals, written in C#[^]

PIEBALD.Types.Rational g = 
   PIEBALD.Types.Rational.ParseInfix ( "((23.5*2)+(45+0.4)-1.3)" ) ;

float x = (float) g ;
 
Share this answer
 
I know this has already been answered, but maybe as a complement, youm ay check toxcct's article which embeds a parser:
Visual Calc v3.0 - A new dimension for the desktop calculator[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900