简单的库或执行一个数学前pression评估 [英] Simple library or implementation for a mathematical expression evaluator
问题描述
我有一个文本文件,它包含只有一行行只包含一个数学前pression
例如12岁以上(3.0 *(4)-1)/ SQRT(121)
i have one text file that contains only one line the line only contains one math expression for example 12+(3.0*(4)-1)/sqrt(121)
我的程序需要读取这个前preSS作为字符串,然后给出结果结果
13
my program needs to read this express as string and then give the result
13
有没有简单的方法或第三方DLL / lib中,使这个呢?
is there any simple way or 3rd party dll/lib to make this out?
补充意见:
<一个href=\"http://stackoverflow.com/questions/928563/$c$c-golf-evaluating-mathematical-ex$p$pssions/944716#944716\">http://stackoverflow.com/questions/928563/$c$c-golf-evaluating-mathematical-ex$p$pssions/944716#944716
下面是解决方案,但许多解决方案只包含+ - / * acturally,我需要运营商尽可能多的如吊顶平方平方根和功率()
here is the solution but many of the solutions only contain +-/* acturally ,i need the operators as many as possible such as ceiling square square root and power()
所以这个环节,也许是最好的解决结果
<一href=\"http://www.$c$cproject.com/KB/recipes/sota_ex$p$pssion_evaluator.aspx\">http://www.$c$cproject.com/KB/recipes/sota_ex$p$pssion_evaluator.aspx
so this link maybe is the best solution
http://www.codeproject.com/KB/recipes/sota_expression_evaluator.aspx
推荐答案
使用 ExprTk 库人们可以很容易得到下面的简单的解决方案:
#include <cstdio>
#include <string>
#include "exprtk.hpp"
int main()
{
typedef exprtk::expression<double> expression_t;
typedef exprtk::parser<double> parser_t;
std::string expression_string = "12 + (3.0 * (4) - 1) / sqrt(121)";
expression_t expression;
parser_t parser;
if (parser.compile(expression_string,expression))
{
double result = expression.value();
printf("Result: %19.15\n",result);
}
else
printf("Error in expression\n.");
return 0;
}
这篇关于简单的库或执行一个数学前pression评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!