通过用户输入调用功能 [英] calling functions by user input

查看:137
本文介绍了通过用户输入调用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨-ITNOG

我想通过用户输入来调用函数,例如,如果用户输入了"max",max函数将像这样调用:


Hi - ITNOG

I want to calling functions by user input , for example if user entered ''max'' , max function will call , like it :


int max ( int k,int z)
{
...
}


int min( int k,int z)
{
...
}
void Main()
{

string s;

consol.read(s);


//What should I do now 
...

consol.write("You have entered max , so max function called and it returned 5 ");

}






我想要一些可以获取函数名称并运行它的函数.

而不使用if.






I want something that get function name , and run it .

without using if.

推荐答案

c#或c ++?因为可用的选项可能不相同.

如果您不想使用if,那么开关又如何呢?

虽然我想如果您不想要一个if,您也不想切换.

本页: C#常见问题:如何使用名称字符串调用方法 [
c# or c++? because the options available may not be the same.

If you don''t want to use an if, how about a switch?

Though I suppose if you don''t want an if, you don''t want a switch.

This page: C# FAQ: How call a method using a name string[^]) gives some code that calls a method by name using MethodInfo.

To pass multiple params, you''d want something like:
methodInfo.Invoke(chosenMethod, new object[] {k, z});



但这是c#.



But that''s c#.


这在C ++中根本不容易做到(至少,以一种友好而灵活的方式).您确定要执行类似的操作吗?其背后的原因是什么?
如果您真的想做类似的事情,我会在您的C ++应用程序中集成脚本语言.

在您的示例中,您不仅需要提供函数名称的名称,而且用户还必须提供函数的参数.想象一下,当您有许多不同的功能,不同的参数类型和数量时,您需要确保一切正确.

如果我必须做这样的事情,我会这样做:我不会有不同的函数,但是每个函数"实际上都是继承自基类的类(类似于CBaseOperation).基类具有虚函数来询问参数并执行操作.然后,每个子类将实现这些功能,以要求提供正确数量的参数并检查其类型.然后他们还实现执行操作的功能.

最后,所有这些类都在工厂类中注册(工厂类只是地图的包装,操作名称是键,而类的实例是值).当用户输入函数名称时,程序会在地图中查找对象是否存在,并依次调用askParameters函数和executeOperation函数(或其他名称).

我认为这是解决问题的一种优雅而灵活的方法,但是您真的确定这是您想要做的吗?
This is something not easy at all to do in C++ (well, in a nice and flexible way at least). Are you sure that you want to do something like that ? What is the reason behind that ?
If you really want to do something like that, I would integrate a script language inside your C++ app.

In your example, you not only need to give the name of the function name but the user has to also provides the parameters for the function. Imagine when you have a lot of different functions, with different parameter types and number, you''ll need to make sure everything is correct.

If I had to do something like that, I would do it this way: I wouldn''t have the different functions but each "function" would in fact be a class inheriting from a base class (something like CBaseOperation). The base class has virtual functions to ask for the parameters and to execute the operation. Each sub-class would then implement those functions in order to ask for the correct number of parameters and check their type. Then they also implement the function to execute the operation.

Finally, all these classes are registered in a factory class (which is just a wrapper around a map, with the operation name being the key and an instance of the class being the value). When the user enters the function name, the program looks in the map to see if the object exists and call the askParameters function then the executeOperation function (or whatever names).

This is I think one elegant and flexible way to solve your problem, but are you really sure that it is what you want to do ?


您将不得不反思以及可能的表达式(这是我不是专家的领域)才能做到这一点.您将无法避免在行中的某个地方使用"if",如果您希望代码非常健壮,那是不可以的.

typeof(Program).GetMethod("Max").Invoke(..)[阅读其余文档]是我认为您正在寻找的
You''re going to have to look into reflection and possibly expressions(that is a field I''m not an expert in) to be able to do this. You''re not going to be able to avoid using ''if'' somewhere along the line, not if you want the code to be pretty robust.

typeof( Program).GetMethod( "Max").Invoke( .. )[read the docs on the rest] is what I think you''re looking for


这篇关于通过用户输入调用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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