一个问题的C程序 [英] C program for a question
问题描述
我有一个问题描述。
我有100个函数的文件。
例如:func_a()
func_b()
..
我正在创建一个可以根据用户要求调用这些函数的函数
例如:如果用户想要调用所有函数那么用户将传递all作为参数,如果用户想要调用特定函数,那么它只会将函数名称作为参数传递。
请给解决这个问题。
我尝试过:
我尝试为此解决方案创建函数指针。需要更多帮助。
作为替代方法,您可以在应用程序中嵌入脚本语言(如Lua) ,请参阅 Lua编程:25 [ ^ ]。允许用户只需键入其名称即可调用文件的脚本函数。
我将使用结构条目数组。结构应该包含函数名称作为字符串,指向实际函数的指针。类似于:
struct ftable
{
char * funcName;
void (* function)();
}
functionTable [] =
{
{ func_a ,func_a},
{ func_b,func_b}
// 等。
};
然后,您可以检查用户是否输入all,只需通过调用每个函数的表。如果他们输入了一个函数名,那么在表中搜索该条目并直接调用它。
这假设所有函数都具有相同的输入参数和返回值。 / BLOCKQUOTE>
I have a problem description.
I have file having certain 100 functions.
eg: func_a()
func_b()
..
I am creating a function which can call these functions according to user's requirement
for eg: if user wants to call all the function then the user will pass "all" as argument and if the user wants to call a specific function then it will just pass the function name as a argument.
Kindly give solution to this problem.
What I have tried:
I have tried creating function pointer for this solution.Need some more help on this.解决方案As an alternative approach you might embed a script language (like Lua) in your appication, see Programming in Lua : 25[^]. The user is the allowed to call the script functions of your file simply typing their name.
I would use an array of struct entries. The struct should contain tha function name as a string, a pointer to the actual function. Something like:
struct ftable { char* funcName; void (*function)(); } functionTable[] = { { "func_a", func_a }, { "func_b", func_b } // etc. };
You can then check if the user enters "all" just go through the table calling each function. If they enter a function name then search the table for the entry and call it directly.
This assumes that all the functions have the same input parameters and return values.
这篇关于一个问题的C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!