如何比较存储在数组中的字符串与c中完整库中的函数名称

在c中输入字符串并将其存储在例如char s [100]之后,如何将该字符串与math.h中的所有函数名称进行比较? 例如,我输入pow,结果将以存储的forms显示。

s[0]='p' s[1]='o' s[2]='w' s[3]='\0' 

由于我的字符串相当于pow(),我希望我的程序能够识别它,然后在执行程序时调用pow()。 我知道在代码中进行字符串比较并不困难,但这意味着我必须对库中的每个函数名进行字符串比较。 我不想那样做。 如何在不对每次比较进行硬编码的情况下将我的字符串与库中的所有名称进行比较?

谢谢 :)

你不能,不能没有自己做工作。 运行时通常没有函数名称,当然也没有未调用的函数名称。

C不是动态语言,名称仅在编译/链接时使用。

C中的正则表达式

尝试使用FILE解析头文件,并使用上述链接作为指导来检查函数是否存在。

我试着对我认为提问者正在寻找的内容( eval.c )做一些示例:

 #include  #include  #include  #include  /* mapping function names to function pointers and number of parameters */ struct Entry { const char *name; /* function name */ double (*pFunc)(); /* function pointer */ int nArgs; /* number of arguments */ } table[] = { #define REGISTER(FUNC, N_ARGS) { #FUNC, &FUNC, N_ARGS } REGISTER(atan2, 2), REGISTER(pow, 2), REGISTER(modf, 2), REGISTER(sin, 1), REGISTER(cos, 1) #undef REGISTER }; /* let compiler count the number of entries */ enum { sizeTable = sizeof table / sizeof *table }; void printUsage(const char *argv0) { int i; printf( "Usage:\n" " %s FUNC\n" " where FUNC must be one of:\n", argv0); for (i = 0; i < sizeTable; ++i) printf(" - %s\n", table[i].name); } int main(int argc, char **argv) { int i; char *func; struct Entry *pEntry; /* read command line argument */ if (argc <= 1) { fprintf(stderr, "ERROR: Missing function argument!\n"); printUsage(argv[0]); return -1; } func = argv[1]; /* find function by name */ for (i = 0; i < sizeTable && strcmp(func, table[i].name) != 0; ++i); if (i >= sizeTable) { fprintf(stderr, "ERROR! Unknown function '%s'!\n", func); printUsage(argv[0]); return -1; } /* perform found function on all (standard) input */ pEntry = table + i; for (;;) { /* endless loop (bail out at EOF or error) */ switch (pEntry->nArgs) { case 1: { double arg1, result; /* get one argument */ if (scanf("%lf", &arg1) != 1) { int error; if (error = !feof(stdin)) fprintf(stderr, "Input ERROR!\n"); return error; /* bail out at EOF or error */ } /* compute */ result = (*pEntry->pFunc)(arg1); /* output */ printf("%s(%f): %f\n", pEntry->name, arg1, result); } break; case 2: { double arg1, arg2, result; /* get two arguments */ if (scanf("%lf %lf", &arg1, &arg2) != 2) { int error; if (error = !feof(stdin)) fprintf(stderr, "Input ERROR!\n"); return error; /* bail out at EOF or error */ } /* compute */ result = (*pEntry->pFunc)(arg1, arg2); /* output */ printf("%s(%f, %f): %f\n", pEntry->name, arg1, arg2, result); } break; default: /* should never happen */ fprintf(stderr, "ERROR! Functions with %d arguments not yet implemented!\n", pEntry->nArgs); assert(0); return -1; /* bail out at error */ } } } 

我在Windows(64位)的cygwin中用gcc编译和测试了这个:

 $ gcc -std=c11 -o eval eval.c $ ./eval ERROR: Missing function argument! Usage: ./eval FUNC where FUNC must be one of: - atan2 - pow - modf - sin - cos $ echo "1 2 3 4 5 6 7 8 9 10" | ./eval pow pow(1.000000, 2.000000): 1.000000 pow(3.000000, 4.000000): 81.000000 pow(5.000000, 6.000000): 15625.000000 pow(7.000000, 8.000000): 5764801.000000 pow(9.000000, 10.000000): 3486784401.000000 $ echo "1 2 3 4 5 6 7 8 9 10" | ./eval sin sin(1.000000): 0.841471 sin(2.000000): 0.909297 sin(3.000000): 0.141120 sin(4.000000): -0.756802 sin(5.000000): -0.958924 sin(6.000000): -0.279415 sin(7.000000): 0.656987 sin(8.000000): 0.989358 sin(9.000000): 0.412118 sin(10.000000): -0.544021 

此应用程序的用法:要应用的函数的名称作为命令行参数提供。 值(应用函数)通过​​标准输入提供。 在示例会话中,我使用echo和管道( | )将echo的输出重定向到eval的输入。 (如果eval被称为独立,则可以通过键盘输入数字。)

笔记:

  1. table实际将字符串映射到函数指针。 为了解决有关参数数量的问题,我在struct Entry也考虑了这一点。

  2. REGISTER宏也是将标识符用作字符串常量的技巧。 #FUNC是一个字符串化宏操作(一种典型的C技巧,可防止由于拼写错误引起的错误)。

  3. sizeTable是防止冗余定义的另一个技巧。 我让编译器计算条目数。 因此,可以添加新条目,并且它仍然可以在没有任何其他编辑的情况下工作。

  4. 实际的技巧是提供一个函数指针,其中参数是“省略”的。 调用它时,使用正确数量的参数并且它可以正常工作。 (假设,当然, table初始化已经仔细实现了。)但是,在C ++中执行此操作会很痛苦,因为具有不同数量的参数的函数需要具有匹配签名的适当函数指针 – 可能需要进行可怕的转换。 (尝试用g++ -std=c++11 -c eval.c编译它,看看我的意思。)

  5. 对于高效的解决方案,我会按名称(按字典顺序)对条目进行排序,并应用二进制搜索(甚至使用散列更快更复杂)。 对于这个样本,我想保持简单。

  6. math.h在“ float味”中也提供了很多function。 如果没有额外的努力,这些可能不会添加到此示例中。 支持除double参数以外的其他参数

    1. 必须将某些类型的信息添加到表条目中
    2. 必须在switch语句中以某种方式考虑类型信息。
  7. …更不用说参数类型彼此不同的函数(或返回类型)。 (我不记得math.h是否提供了这样的function。)

顺便说一句。 这将适用于非math.h函数也…