tags:

views:

161

answers:

6

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)

my program needs to read this express as string and then give the result
13

is there any simple way or 3rd party dll/lib to make this out?

COMMENT ADDED:

http://stackoverflow.com/questions/928563/code-golf-evaluating-mathematical-expressions/944716#944716

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()

so this link maybe is the best solution
http://www.codeproject.com/KB/recipes/sota_expression_evaluator.aspx

A: 

.NET solutions:

Here couple of topics on SO:


Also two projects I already used brefore:

C#: NCalc - Mathematical Expressions Evaluator for .NET

NCalc is a mathematical expressions evaluator in .NET. NCalc can parse any expression and evaluate the result, including static or dynamic parameters and custom functions.


VB.NET: Fast Lightweight Expression Evaluator

Flee is an expression parser and evaluator for the .NET framework. It allows you to compute the value of string expressions such as sqrt(a^2 + b^2) at runtime. It uses a custom compiler, strongly-typed expression language, and lightweight codegen to compile expressions directly to IL. This means that expression evaluation is extremely fast and efficient. Try out the demo, which lets you generate images based on expressions, and see for yourself.

You can use it with C# since it .NET anyway (via assembly reference).

Nick Martyshchenko
it's complex and the source code is VB, do you have any C# or C++ project like this?
Biwier
A: 

For C et al, here's a quick-n-dirty and highly unsafe cheat that requires Perl:

double eval(const char* expr) {
    char buf[1024];
    snprintf(buf, sizeof(buf), "perl -e 'print (%s)'", expr);
    FILE* p = popen(buf, "r");
    double d;
    fscanf(p, "%lf", &d);
    fclose(p);
    return d;
}
Marcelo Cantos
As you said, a cheat. Not so much C as it is perl...
Merlyn Morgan-Graham
A: 

In The UNIX Programming Environment, I think it was, a simple calculator called hoc (IIRC) was developed. Possibly its source code is available just about anywhere.

Cheers & hth.,

Alf P. Steinbach
+1  A: 

I'd consider embedding lua. Its fast, lightweight and ansi C. And its been specifically designed for embedding. Used by many games as their scripting language. (IMO much easier to embed than python or perl)

Here's a complete example to show how trivial it is

extern "C"
{
  #include "lua.h"
  #include "lauxlib.h"
  #include "lualib.h"
}

#include <string>
#include <iostream>

int main()
{
   std::string expression = "12+(3.0*(4)-1)/math.sqrt(121)";
   lua_State * L = lua_open();
   luaopen_math(L);
   if( luaL_dostring(L, ("return "+expression).c_str()) != 0 )
   {
      std::cout<<"ERROR : "<<lua_tostring(L,-1)<<std::endl;
   }
   if( lua_type(L,-1) == LUA_TNUMBER )
   {
      std::cout<<"GOT "<<lua_tonumber(L,-1)<<std::endl;
   }
   lua_close(L);
}
Michael Anderson
A: 

If you care to brave the source code, you can always look at bc. It handles all the Lex/Yacc goodness for you. And if you want a pure C++ solution, you can try coding in Boost Spirit.

chrisaycock
+1  A: 

For c++, try out muParser:

muParser - a fast math parser library

Inverse