c/c++语言开发共享如何比较存储在数组中的字符串与c中完整库中的函数名称

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

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

由于我的字符串相当于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 FUNCn" " where FUNC must be one of:n", argv0); for (i = 0; i < sizeTable; ++i) printf(" - %sn", 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): %fn", 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): %fn", 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被称为独立,则可以通过键盘输入数字。)

    笔记:

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

      以上就是c/c++开发分享如何比较存储在数组中的字符串与c中完整库中的函数名称相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/522066.html

      (0)
      上一篇 2020年12月9日
      下一篇 2020年12月9日

      精彩推荐