将字符串数学表达式转换为float [英] Convert a string mathematic expression to float

查看:108
本文介绍了将字符串数学表达式转换为float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我如何将字符串数学表达式转换为float。

这是一个例子:



((23.5 * 2)+(45 + 0.4)-1.3)

函数应返回:91.1



谢谢

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

推荐答案

你必须知道C ++是一种编译语言。因此,在运行时,您无法轻松解释语言表达式。您需要使用的是一个库(或您自己的代码)来解释和评估表达式。



在这里查看示例:http://fastmathparser.codeplex.com/ [ ^ ]



或此处: http:// muparser。 beltoforion.de/ [ ^ ]
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/[^]

您需要一个评估者。看看这里:表达式评估员:使用RPN [ ^ ]
You need an evaluator. Have a look here: Expression evaluator : using RPN[^]


< a href =http://www.codeproject.com/Articles/12805/NET-Rational-fraction-value-type-using-Decimals-w> .NET Rational(fraction)值类型使用Decimals,用C#编写 [ ^ ]



.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 ;


这篇关于将字符串数学表达式转换为float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆