C++ 使用用户参数作为函数名及其参数调用函数 sub; 如果(sub==“”) 打破 sa[计数]=子系统; cout

C++ 使用用户参数作为函数名及其参数调用函数 sub; 如果(sub==“”) 打破 sa[计数]=子系统; cout,c++,c++11,C++,C++11,假设我定义了几个函数: double square(float x) {return x*x} double cube (float x) {return x*x*x} read_lib(const string &file_name) 现在,当用户输入square 10时,我想调用square函数,以10作为参数,并返回100 类似地,当用户输入多维数据集3时,我希望返回27 类似地,当用户输入read_lib/path/to/file/library.gz时,我希望处理该文件 这是

假设我定义了几个函数:

double square(float x) {return x*x}
double cube (float x) {return x*x*x}
read_lib(const string &file_name)
现在,当用户输入
square 10
时,我想调用square函数,以
10
作为参数,并返回
100

类似地,当用户输入多维数据集3时,我希望返回
27

类似地,当用户输入read_lib/path/to/file/library.gz时,我希望处理该文件

这是我的密码:

int main()
{ 
    string quote;
    while(quote != "quit") {

        cout << "Enter: - ";
        string sa[70];
        getline(cin, quote);
        istringstream iss(quote);
        int count = 0;

        while(iss){
            string sub;
            iss>>sub;
            if(sub == "") 
                break;
            sa[count] = sub;
            cout << "sub "<<count <<" is " << sa[count]<<endl;
            count ++; 
        }

        string str = sa[0] + "(" + sa[1] + ")"; 
        // How do I dynamically switch quotations depending on sa[0]
        cout << "command executed was "<< str.c_str() <<endl;
        system(str.c_str());
    }
}
intmain()
{ 
字符串引用;
while(引号!=“退出”){
cout>sub;
如果(sub==“”)
打破
sa[计数]=子系统;

cout好的,首先你不需要一个包含70个字符串的数组来解析你的命令。根据你的要求,看起来你需要先解析一个命令,然后再解析一个参数。在这个例子中,我使用了一个std::array来创建一个包含2个字符串的数组。但是,你可以随意创建多个字符串,然后在loo中处理该命令p、 而不是手动使用
命令[0/1]
。如果需要一个命令,则使用向量,然后使用一些参数

#include <array>
#include <iostream>
#include <string>

using namespace std;

int main() {
    string quote;
    array<string, 2> command;
    while (quote != "quit") {
        cout << "Enter: ";
        getline(cin, quote);

        auto separator = quote.find(' ');
        if (separator == string::npos) continue;

        command[0] = quote.substr(0, separator);
        command[1] = quote.substr(separator + 1);

        cout << "Command executed was " << command[0] << '(' << command[1] << ")\n";
    }
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串引用;
数组命令;
while(引号!=“退出”){

cout您的目标有多个部分:

  • 解析输入
  • 解析参数以将其传递给命令函数
  • 确定要调用哪个命令函数
  • 显示结果
首先,让我们从要调用的函数开始:

double square(float x) {
    return static_cast<double>(x) * x;
}

double cube(float x) {
    return static_cast<double>(x) * x * x;
}
double square(浮点x){
返回静态_cast(x)*x;
}
双立方体(浮动x){
返回静态_cast(x)*x*x;
}
现在,我们需要一个“命令类型”,我们将其定义为一个函数,该函数将字符串作为参数并返回字符串:

using command_fn = std::function<std::string(std::string const &)>;
template <typename R, typename A>
command_fn create_command(R (*fn)(A)) {
    return [fn] (std::string const &string_arg) {
        std::istringstream s{string_arg};
        A arg;

        if (!(s >> arg)) {
            return std::string("Failed to convert argument");
        }

        return std::to_string(fn(arg));
    };
}
使用命令\u fn=std::function;
很好,但是我们的函数不接受字符串,也不返回字符串。不管怎样,我们将把它们包装在lambda中,该lambda使用标准的输入流运算符解析参数,并将结果格式化为字符串:

using command_fn = std::function<std::string(std::string const &)>;
template <typename R, typename A>
command_fn create_command(R (*fn)(A)) {
    return [fn] (std::string const &string_arg) {
        std::istringstream s{string_arg};
        A arg;

        if (!(s >> arg)) {
            return std::string("Failed to convert argument");
        }

        return std::to_string(fn(arg));
    };
}
模板
命令创建命令(R(*fn)(A)){
返回[fn](标准::字符串常量和字符串参数){
std::istringstreams{string_arg};
精氨酸;
如果(!(s>>arg)){
返回std::string(“转换参数失败”);
}
返回std::to_字符串(fn(arg));
};
}
现在我们可以创建一个
std::map
,将命令名映射到包装函数:

std::map<std::string, command_fn> commands{
    { "square", create_command(square) },
    { "cube", create_command(cube) }
};
std::map命令{
{“square”,创建_命令(square)},
{“多维数据集”,创建_命令(多维数据集)}
};
最后,我们解析输入并在循环中分派调用:

int main() {
    std::string line;

    while (std::getline(std::cin, line)) {
        auto space = line.find(' ');

        std::string cmd, arg;
        if (space == std::string::npos) {
            cmd = std::move(line);
        } else {
            cmd = line.substr(0, space);
            arg = line.substr(space + 1);
        }

        auto const &cmdfn = commands.find(cmd);

        if (cmdfn != commands.end()) {
            std::cout << cmdfn->second(arg) << std::endl;
        } else {
            std::cout << "No such command: " << cmd << std::endl;
        }
    }

    return 0;
}
intmain(){
std::字符串行;
while(std::getline(std::cin,line)){
自动间距=行。查找(“”);
std::字符串cmd,arg;
if(space==std::string::npos){
cmd=std::move(行);
}否则{
cmd=line.substr(0,空格);
arg=line.substr(空格+1);
}
auto-const&cmdfn=commands.find(cmd);
if(cmdfn!=commands.end()){

标准::cout秒(arg)你是否读过任何一本C++书?从VUE的安全点来看,很难比把任意的用户数据传递给<代码> STD::St/<代码>更糟糕。这个代码是如何与你在开始时提到的函数相关的?你的完整程序在哪里?你可能会收到什么错误消息?关于这个问题的研究在哪里?(不管它是什么)你想要的是什么?你想要写什么?为什么你要写这个代码?没有冒犯,但是写一个C++程序似乎没有意义,它除了把用户输入的代码转发到<代码>系统> /Cudio>调用。如果你输入<代码>平方(10),它不是更容易吗?“TabI303”,这只是一个例子。我有一个带有交互式C++接口的大工具。所有的函数都将在后台,用户希望在C++语法中有宽大。谢谢。但是我仍然没有看到它将如何调用命令[0 ]。函数?@ChinmoyKulkarni制作一个
std::map
并在映射中查找函数。您可以通过一个if语句传递它:
if(command[0]==“cube”)cube(stoi(command[1]))
假设您想要一个整数传递给多维数据集。当然,假设用户实际输入了一个数字。如果您不知道,您可能需要提前进行一些验证。正如我所说,这是一个不违反隐私和合法性的示例。但我有数千个以上的函数。使用map仍然是最佳选择吗?@ChinmoyKulkarni是。
system()
甚至不做你想做的事。