We would like to have user defined formulas in our c++ program.
e.g. The value v = x + ( y - (z - 2)) / 2. Later in the program the user would define x,y and z -> the program should return the result of the calculation.
Somewhen later the formula may get changed, so the next time the program should parse the formula and add the new values.
Any ideas / hints how to do something like this ?
So far I just came to the solution to write a parser to calculate these formulas - maybe any ideas about that ?
C allows programmers to write their own functions, also known as user-defined functions. A user-defined function has three main components that are function declarations, function definition and function call. Further functions can be called by call by value or call by reference.
A function is a block of code that performs a specific task. C allows you to define functions according to your need. These functions are known as user-defined functions. For example: Suppose, you need to create a circle and color it depending upon the radius and color.
Library function: These function are the built-in functions i.e., they are predefined in the library of the C. These are used to perform the most common operations like calculations, updation, etc. Some of the library functions are printf, scanf, sqrt, etc.
A user-defined function does not require writing any code inside the header files. For example: swap() function does not require any header file. All the library functions are predefined inside the header files. For example: printf(), and scanf() function are defined in the stdio.
If it will be used frequently and if it will be extended in the future, I would almost recommend adding either Python or Lua into your code. Lua is a very lightweight scripting language which you can hook into and provide new functions, operators etc. If you want to do more robust and complicated things, use Python instead.
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