Adding Strings To Create The Function
Adding Strings To Create The Function
INTRODUCTION
Okay, now it comes something I would like to share with other people. It came into spot light long time
ago, however it has been magnified some time ago. Actually, while I was developing the calculator
application, it had come back.
Now, I wonder, would this thing happened one day the way I would like it to happen. So, let's stop bitting
about bush and consider what I was talking about.
MAIN PART
Now if we would like to add two numbers we could write function like this:
double add_function ( double a, double b) { return a + b; }
and if you would like to multiply you would create function like this
double multiply_function ( double a, double b){ return a * b;}
Then you could add some function with the function pointers like this:
double
some_operation( double first, double (* f ) ( double, double), double right)
{
return (*f) (first, second );
}
Now in the main function you would do something like this:
...
some_operation( value_1, add_function, value_2);
...
some_operation( value_1, multiply_function, value_2);
Now, you could add functions: subtract, and divide. After that, you would be having templates in mind
and how to do it in C++ in the most efficient way.
Well, who like to type if we could create more flexible function.
Then you might get idea to write function like this:
double
binary_operation( double first, char op, double second )
double dResult = 0;
switch ( op )
{
case '+': dResutl = first + second;
break;
case '*': dResutl = first * second;
break;
case '-': dResutl = first - second;
break;
case '/': dResutl = first / second;
break;
}
return dResult;
int
main( void )
{
msg(MOST);
msg(MYLANGUAGE);
msg(FRIENDLANGUAGE);
return EXIT_SUCCESS;
}
As you have noticed, well I hope, we have added two string into one in order to create one macro, and
now if we have function-like macros we would be able to create something that will have macro and
function in one.
That does the job, but creates rather ugly syntax.
So, not happy yet.
There comes the way, which is still not accomplished by me yet.
How would I like to do it, well like this:
double nice_binary_operation ( double first, char op, double second)
{
char[] body =;
strcat( body, itoa( first ) );
strcat(body, op);
strcat( body, itoa( second );
return body;
}
This syntax is still pretty ugly. However it would make me way more happy to accomplish something like
this.
CONCLUSION
Now, there is a time to ask you self a question, how would you use this tool in real life scenario. Well,
let say that you would like to create the math expression parser. In order to accomplish that you would
need to write complex things like: binary expression, unary expression, functions that you use and
stuff.
If I could write what I need, I would be able to create something more elegant and more flexible.
So, user inputs the expression and you have math expression and the result is executed after you get the
input.
Well, it could be done, but it is not nice... There is a lot of work... and I would rather to some other
stuff than
You know, like creating dynamic expressions that act like functions.