1

i wonder if is possible, for example read a file with some content like:

a+b*c

and that my programm "create" a function to do this operation, and if i modify the file ( like a+b*c+2 ) the programm read this changes and updates what this function do. Well i dont have a solid backbround in the basis of C++ and i don't know if what i'm asking is just plain stupid. I need ( or something like this could be nice ) for my work in physics simulations, where the model is mainly definied by a equilibrium function ( and some other parameters ) so what i think is that could be good if i can make a programm to test this models without having to writte a special code for each one...

Thanks!

RolandDeschain
  • 483
  • 1
  • 4
  • 17
  • you want to create a function `foo(a,b,c)` that you can call in your code, that evaluates the experession in the file? – 463035818_is_not_an_ai Nov 14 '15 at 15:46
  • No it's not possible in a simple manner. You have to implement a parser and interpreter, or rely on a c++ Compiler create a shared library on the fly, and load it in order to execute the statements. – πάντα ῥεῖ Nov 14 '15 at 15:46
  • You are looking for creating equation parser and lexical analyzer – Abdul Rehman Nov 14 '15 at 15:47
  • @AbdulRehman yes something like that ... but i'm lack of information and i dont know where begin my research of this kind of think. do you have some bibliography to recommend to me? – RolandDeschain Nov 14 '15 at 17:33
  • @RolandDeschain http://stackoverflow.com/questions/4582398/writing-a-simple-equation-parser here is a good explanation and useful links of what you need. – Abdul Rehman Nov 14 '15 at 17:42

2 Answers2

0

C++ is not interpreted code. So you can only compile hard coded expressions in source code. However you can evaluate an expression on your own. You can look at some solutions here

Community
  • 1
  • 1
user1969104
  • 2,340
  • 14
  • 15
0

for sure it is "possible", actually that's what matlab, mapple or any other formal calculation software/lib do. BTW, writing one may be quite simple if you just handle */+- basic operators, and may become more and more complex depending on what you want to use (sin cos, exp, log etc.)

Basic implementations reads of the input and build an internal tree with final values on leaves would look like soething like this in your case:

  +
a   *
   b  c

I'm sure you can find a lot of docs on it.

OznOg
  • 4,440
  • 2
  • 26
  • 35