添加两个函数

Addition of two functions

本文关键字:两个 函数 添加      更新时间:2023-10-16

我正在用c++做一个解析函数,它接受字符串和双精度体作为参数,并返回字符串的"值"。

代码如下:

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;
}

它远非完美无瑕(对操作符没有优先级,如果字符串包含太多括号则不起作用,等等),但我想删除双x参数,并直接处理函数。(因为我想绘制函数,如果我不处理函数,我将不得不为x的每个值解析相同的字符串…)

有可能吗?我的意思是,像这样做:

double (double) operator+ (double f(double), double g(double))
{
    double h (double x)
    {
        return f(x)+g(x);
    }
    return h;
}

当然,它不起作用。有什么想法吗?(类等)

谢谢。

可以使用函数指针。我不太明白,如果你想要一个函数指针作为参数,但这是你可以做到的:

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)。您应该确保index是数组中的实际值,否则您将访问越界

我想到了一个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);
}

它不能很好地扩展,效率也不高。也许有一个模板解决方案可以在编译时完成所有这些。