To evaluate an algebraic expression, you have to substitute a number for each variable and perform the arithmetic operations. In the example above, the variable x is equal to 6 since 6 + 6 = 12. If we know the value of our variables, we can replace the variables with their values and then evaluate the expression.
An expression in C is defined as 2 or more operands are connected by one operator and which can also be said to a formula to perform any operation. An operand is a function reference, an array element, a variable, or any constant. An operator is symbols like “+”, “-“, “/”, “*” etc.
We are required to write a JavaScript function that takes in a mathematical expression as a string and return its result as a number. Operators are always evaluated from left-to-right, and * and / must be evaluated before + and -.
Not sure why 'pow' only has one parameter, but using the ExprTk library one can derive the following simple solution:
#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 = "3 + sqrt(5) + pow(3,2) + log(5)";
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;
}
There is no way to do this with an off-the-shelf standard library in C++, though there are many good parsing algorithms out there that will let you evaluate expressions like these.
If you'd like some references on good parsing algorithms, consider looking into Chapter 14 on expression parsing in Programming Abstractions in C++ (free and available online!), or consider looking into Dijkstra's shunting-yard algorithm. Both of the algorithms mentioned here are simple to implement and will let you evaluate expressions with relative ease.
If you're interested in some more hardcore tools for evaluating expressions, consider looking into the flex
and GNU bison
tools, which can build powerful parsers for these sorts of expressions. I believe that the bison
documentation even shows you how to parse and evaluate arithmetic expressions, so you might have your work already done for you.
Hope this helps!
Boost.Spirit is a C++ parser library.
Examples:
muParserX is another C++ mathematical expression parser.
I've written a simple, easy-to-use, front-end to Lua for evaluating arithmetic expressions from C (and C++ of course). See http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#ae . See also OpenSouce C/C++ Math expression parser Library and What is a fast C or Objective-C math parser?
Lepton is another C++ library that can do this. In addition to parsing and evaluating expressions, it also has some more advanced abilities. For example, it can compute analytic derivatives, and it can do some basic algebraic simplification of expressions. The library is quite small, and it's open source (MIT license).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With