Is there any way to pass function as a parameter when starting a program in C? I am implementing an app for integral approximation, and all I need is to type a function I want to work with when starting the application. I tried (e.g.) 2/(2+2*x), but I only get back “2”. When I write to application directly, there is no problem. Is there any simple way of getting this? Maybe redistribute it to more parameters? Like
app.c number number*x number *x*x number *x*x*x... ?
Thanks
An easy way to go about this would be to store your expression in a file, give your program that file’s name as an argument, and then use
system()or an equivalent to have the shell interpret and evaluate the expression at runtime. This will be fairly slow however.If you want more performance, and your functions aren’t terribly complicated (e.g. you only have basic arithmetic operations), then you could write a recursive function to evaluate it. Starting from the end of the formula, walk backwards and find the operation with the lowest precedence. Recursively evaluate both sides, then apply the operation to the results. If you can’t find an operator, check for parentheses around your expression, and evaluate the insides. If there are no parentheses either, then the entire expression is a single value, and can be converted to a double (ostensibly).