Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
I have a mathematical expression. I want to save it in CString. And after that I want to execute that CString as a code line.

Mathematical expression is like = 3 * (var1 + 10) + 100
if var1 > 100 then var1 = 1 else if var1 < 100 var1 = 2.

value of var1 is taken from some CSV / text file.

Suppose if var1 = 90
then CString strExp = 3 * (2 + 10) + 100;
and then I want to execute this CString and result would be saved in another string.

double Result = XXXX strExp XXXXX //whatever operation / parsing
// result = 136
Posted

You cannot do it in any simple way. In some interpretive languages, and even in some compiling languages, this is a fundamental feature, but C++ is not one of them.

You will need to create some compiler or interpreter to parse and execute your string. You can start here: http://www.codeproject.com/search.aspx?q=C%2b%2b+expression+parser&doctypeid=1%3b2%3b3%3b13%3b14%3b5[^].

Alternative way would be not using strings at all. You can develop special types representing nodes of expression tree and the tree itself, which your could use to create expressions right in C++ code. In other words, it's possible to develop something similar to .NET lambda expressions. But I guess it would take me writing a whole book to explain it. Your question strongly suggests that you are not ready for such works yet.

—SA
 
Share this answer
 
Comments
CPallini 14-Aug-15 3:01am    
5.
Sergey Alexandrovich Kryukov 14-Aug-15 10:30am    
Thank you, Carlo.
—SA
Padmanabh Kulkarni 14-Aug-15 3:13am    
Thanks CPallini, Kryukov.
But I want to do it in MFC only and not using any third party or other library.
Sergey Alexandrovich Kryukov 14-Aug-15 10:31am    
First, this it totally unrelated to MFC. There is no such thing as miracle. Either use 3rd-party library or write your own. Any questions?
—SA
As an alternative, you may embed a script language, like Lua in your application and then let the script engine evaluate expressions (and more complex constructs) for you. We have an article on this very topic: "Integrating Lua into C++" here at Code Project[^].
See also "Evaluating Mathematical Expressions using Lua" at Stack Overflow[^].
 
Share this answer
 
Comments
CPallini 14-Aug-15 3:12am    
That entirely depends on the framework you are using.
Sergey Alexandrovich Kryukov 14-Aug-15 10:31am    
Hm... another idea. A 5.
—SA
Albert Holguin 14-Aug-15 11:37am    
Embedding a scripting language is the way I would go about this too... not sure I'd use Lua though as there are a variety of heavy lifting mathematically intensive scripting engine options (Octave/Matlab) that would likely support math intensive scripting very well. Of course, I don't have much experience with Lua so that probably makes me a little biased against it. .....+5 for the scripting suggestion though...
XML
template <typename T>
void trig_function()
{
   typedef exprtk::symbol_table<T> symbol_table_t;
   typedef exprtk::expression<T>     expression_t;
   typedef exprtk::parser<T>             parser_t;

   std::string expression_string = "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)";

   T x;

   symbol_table_t symbol_table;
   symbol_table.add_variable("x",x);
   symbol_table.add_constants();

   expression_t expression;
   expression.register_symbol_table(symbol_table);

   parser_t parser;
   parser.compile(expression_string,expression);

   for (x = T(-5); x <= T(+5); x += T(0.001))
   {
      T y = expression.value();
      printf("%19.15f\t%19.15f\n",x,y);
   }

}
 
Share this answer
 
Comments
Padmanabh Kulkarni 14-Aug-15 5:36am    
What I understand from above answer is "In MFC it is not possible (with using our regular code / logic / libraries )for this there are 2 options
1) Develop special types representing nodes of expression tree and the tree itself - Sergey Alexandrovich Kryukov "
2) Use third party libraries / scripts - CPallini

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