添加两个函数 我在C++中做了一个解析函数,它以字符串和双变量作为参数,并返回字符串的“值”。

添加两个函数 我在C++中做了一个解析函数,它以字符串和双变量作为参数,并返回字符串的“值”。,c++,c++11,C++,C++11,代码如下: double evaluate (char * toParse, int length, double x) { // Case 'x' if ((toParse[0] == 'x') && (length == 1)) { return x; } // Case value char * endptr; double num = strtod(toParse, &en

代码如下:

double evaluate (char * toParse, int length, double x)
{

    // Case 'x'
    if ((toParse[0] == 'x') &&
        (length == 1))
    {
        return x;
    }

    // Case value
    char * endptr;
    double num = strtod(toParse, &endptr);
    if(endptr - toParse == length)
    {
        return num;
    }

    // Parsing
    int nBrackets = 0;
    for (int i = 0; i < length; i++)
    {
        if (toParse[i] == '(')
        {
            nBrackets++;
        }
        else if (toParse[i] == ')')
        {
            nBrackets--;
        }

        // Remove brackets.
        double _x = (toParse[0] == '(' && toParse[i-1] == ')' ) ?
                        evaluate(&toParse[1], i-2, x) : evaluate(toParse, i, x);
        double _y = (toParse[i+1] == '(' && toParse[length-1] == ')' ) ?
                        evaluate(&toParse[i+2], length - (i+1) - 2, x) : evaluate (&toParse[i+1] , length - (i+1), x);

        // Supports +, -, * and /
        if (nBrackets == 0 &&
            toParse[i] == '+')
        {
            return _x + _y;
        }
        else if (nBrackets == 0 &&
            toParse[i] == '-')
        {
            return _x - _y;
        }
        else if (nBrackets == 0 &&
            toParse[i] == '*')
        {
            return _x * _y;
        }
        else if (nBrackets == 0 &&
            toParse[i] == '/')
        {
            return _x / _y;
        }
    }
    return 0.;
}

int main()
{
    cout << evaluate("((4*x)+7)-x", 11, 5.) << endl;
    // Outputs 22, which sounds correct.
    return 0;
}
当然,这是行不通的。有什么想法吗?(班级等)


谢谢。

您可以使用函数指针。我不太明白您是否也希望函数指针作为参数,但以下是如何做到这一点:

typedef double (*handler) (double);


double add(handler x, handler y);
double sub(handler x, handler y);
double func1(double n);
double func2(double n);

int main()

{

    double (*funcPtr[256]) (double);

    funcPtr['+'] = func1;
    funcPtr['-'] = func2;

    double answer = funcPtr['+'](2));
}

double func1(double n)
{
    return n;
}

double func2(double n)
{
    return n;
}

double add(handler x, handler y) 
{ 
    return x(2) + y(2);
}
double sub(handler x, handler y) 
{ 
    return x(4) - y(2);
}
如果它不是您想要的,但类似于下面的代码,请告诉我,我将编辑我的答案:

funcPtr[toParse[i]](2, 2); // toParse[i] is '+' it will then call add(2,2)

256大小的
funcPtr
数组与
sizeof(char)
有关。您应该确保
索引
是数组中的实际值,否则您将访问超出范围的值。

会想到lambda。不幸的是,我认为从捕获函数参数的函数返回lambda是未定义的行为。 也不能重载函数指针之类的内置类型。 你可以这样做:

#include <iostream>
#include <functional>

struct CombinedFunction{
    enum class Operator{ add, subtract, multiply, divide } op;
    CombinedFunction(std::function<double(double)> f1, std::function<double(double)> f2, Operator op) : f1(std::move(f1)), f2(std::move(f2)), op(op){
    }
    std::function<double(double)> f1, f2;
    double operator()(double d){
        switch (op){
        case Operator::add:
            return f1(d) + f2(d);
            break;
        case Operator::subtract:
            //...
            break;
            //...
        }
    }
};

std::function<double(double)> operator+ (std::function<double(double)> f, std::function<double(double)> g){
    return CombinedFunction(f, g, CombinedFunction::Operator::add);
}

double f(double x){
    return 2 * x;
}
double g(double x){
    return 3 * x;
}

int main(){
    auto h = std::function<double(double)>(f) + g;
    std::cout << h(2);
    auto h2 = f + h + g;
    std::cout << h2(2);
}
#包括
#包括
结构组合函数{
枚举类运算符{加、减、乘、除}op;
组合函数(std::function f1,std::function f2,操作符op):f1(std::move(f1)),f2(std::move(f2)),op(op){
}
std::函数f1,f2;
双运算符()(双d){
开关(op){
案例运算符::添加:
返回f1(d)+f2(d);
打破
大小写运算符::减法:
//...
打破
//...
}
}
};
标准::函数运算符+(标准::函数f,标准::函数g){
返回CombinedFunction(f,g,CombinedFunction::Operator::add);
}
双f(双x){
返回2*x;
}
双g(双x){
返回3*x;
}
int main(){
自动h=std::函数(f)+g;

STC++:C++中的字符串函数调用该函数是非法的。C和C++都允许传递指针到其他函数。嗨,谢谢你!三!克里斯:我不能只使用STD::String来符合C++ 11吗?我不介意改变它!”JoeWrase:你能解释得更好吗?我看不出我应该把哪个指针传递给WHIC。h函数实际上:(可以
return[=](double x){return f(x)+g(x);};
作为
std::function
之类的函数,但是当一个类型是用户定义的类型时,你只能重载一个操作符。嗨,我使用了你的解决方案,一切都很好!我真的不理解“double operator()(double d)”背后的魔力“返回组合函数(…)”;“但是,不管怎样:”我将改变我的STD::函数评估(char * TopARSE,int长度)原型:STD::函数评估(STD::String TopAsSE),以后它将是更多的C++!)谢谢!我认为这个答案是一个触摸过于复杂:
#include <iostream>
#include <functional>

struct CombinedFunction{
    enum class Operator{ add, subtract, multiply, divide } op;
    CombinedFunction(std::function<double(double)> f1, std::function<double(double)> f2, Operator op) : f1(std::move(f1)), f2(std::move(f2)), op(op){
    }
    std::function<double(double)> f1, f2;
    double operator()(double d){
        switch (op){
        case Operator::add:
            return f1(d) + f2(d);
            break;
        case Operator::subtract:
            //...
            break;
            //...
        }
    }
};

std::function<double(double)> operator+ (std::function<double(double)> f, std::function<double(double)> g){
    return CombinedFunction(f, g, CombinedFunction::Operator::add);
}

double f(double x){
    return 2 * x;
}
double g(double x){
    return 3 * x;
}

int main(){
    auto h = std::function<double(double)>(f) + g;
    std::cout << h(2);
    auto h2 = f + h + g;
    std::cout << h2(2);
}