如何通过一个C数学运算符(+ - * /%)到一个函数的结果= mathfunc(X +,Y);? [英] How to pass a C math operator (+-*/%) into a function result=mathfunc(x,+,y);?

查看:83
本文介绍了如何通过一个C数学运算符(+ - * /%)到一个函数的结果= mathfunc(X +,Y);?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在多precision整数C程序中的一些数学程序,我希望能够轻松地编写前pressions,但我自己的函数处理数学运算。所以,我想一些办法,让我做到这一点:

I'm writing some math routines in a C program for multi-precision integers and I want to be able to easily write expressions, but handle the math with my own function. So I want some way that lets me do this:

MPI x=mpi(5),y=mpi(6),z;
z=mpimath(x,+,y);

通过某种方式的字符编码​​在C是这可能吗?我知道有没有操作符重载所以它必须是一个函数调用,而 + -sign不能是函数名称的一部分。

Is this possible in C by encoding the character somehow? I know there's no operator overloading so it has to be a function call, and the +-sign cannot be part of the function name.

推荐答案

是的,这是可能的。

您可以使用宏接口用于连接功能。宏让你在code的外貌额外的权力。特别是,你可以的字符串化的参数。您还可以通过任何完整的C的标记的作为参数传递给宏。

You can interface to your function using a macro. A macro gives you extra powers over the physical appearance of the code. In particular, you can stringify an argument. And you can pass any complete C token as an argument to a macro.

所以,做这样的事情:

#define mpimath(X,F,Y) (mpimath)(X,#F,Y)  /* stringify F */

然后定义你的函数接受的char * 参数。这将导致在字符串中的+的参数 + 。通过使用相同的名称为宏,调用该函数是的拦截的该宏所以像这样的电话:

Then define your function to accept a char * parameter. This will result in the string "+" for the argument +. By using the same name for the macro, calls to the function are intercepted by this macro so a call like this:

mpimath(p,*,r)   /* macro invocation */

被扩展为

(mpimath)(p,"*",r)   /* function call */

。周围的函数名的括号并非严格必要这里,由于一个宏不允许是递归的,它会扩展到正确的事。但包装是的括号也显式调用的功能的绕过任何宏定义的方式,所以我觉得它有助于自我文档code到他们在这里补充。

. The parens around the function name are not strictly necessary here, since a macro is not allowed to be recursive, it will expand to the right thing. But wrapping parens is also the way to explicitly call the function bypassing any macro definitions, so I find it helps self-document the code to add them here.

您可以使用类似的char * OPS =+ - * /;诠释运算=的strstr(OPS中,f)-ops; 来字符串映射到一个小的整数(*),那么它可以被用来索引的函数表。或者,你可以取消引用字符串获得字符您可以在开关使用

You can use something like char *ops="+-*/"; int op=strstr(ops, f)-ops; to map the string to a small integer (*) which can then be used to index a function-table. Or you can dereference the string to get a char which you can use in a switch.

MPI mpimath(MPI x, char *f, MPI y){
    switch(*f){
    case '+': //...
              break;
    case '-': //...
              break;
    //etc.
}

或者你可以将提领回宏。它看起来在宏有点怪异。但我认为它使功能看起来更漂亮。

Or you can move the dereferencing back into the macro. It looks a little weird in the macro. But I think it makes the function look nicer.

#define mpimath(X,F,Y) (mpimath)(X,*#F,Y) /* stringify and deref F */

MPI mpimath(MPI x, char f, MPI y){
    switch(f){
    case '+':
    //etc.
    }
}


编辑:关于实际code,它给人们带​​来这一些细节的共享您的知识的Q /一对

为运营商传递作为动机字符间接产生的,但从来没有在这个问题所示的简化形式。目前使用的宏看起来像这样的 + 功能:

The motivation for the passing the operator as a char arose indirectly, but never in the simplified form shown in the question. The current use of macro looks like this for the + function:

 BIN_MATH_FUNC(+,AV(z)[i],AV(a)[i],AV(w)[i],plusover,plusdomainI,plusdomainD)

和宏也直接处理原生类型,如 INT 双击。这样,以适应所需要的MPI函数已经成立了自己的实际C运算符来区分操作(作为宏参数)的框架。所以,实际情况并不允许使用枚举符号。

and the macro also directly handles native types like int and double. So the framework that the mpi function needed to fit was already set up to distinguish operations by their actual C operator (as a macro argument). So the real situation didn't allow for the use of enum symbols.

<子>(*)这不,礼貌地说,一个很好的办法做到这一点。在code依赖于编译器将两个字符串凝结成在编译的程序的字符串表中的一个位置。这是一个典型的(一个甚至可以说的明显的)编译器做优化,但它没有任何标准的保证。这将是更好的(尽管更详细),分配字符串到的char * 变量,并在两地使用变量。可以(并且可能应该),结果做任何事情之前测试的strstr 的结果为'NULL。

(*) This is not, to put it politely, a good way to do it. The code relies upon the compiler to condense the two strings into one location in the compiled program's string-table. This is a typical (one might even say obvious) optimization for the compiler to do, but is it not guaranteed by any standard. It would be better (though more verbose) to assign the string literal to a char * variable and use the variable in both places. You can (and probably should) test the result of strstr for `NULL before doing anything with the result.

这篇关于如何通过一个C数学运算符(+ - * /%)到一个函数的结果= mathfunc(X +,Y);?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆