如何导入用户定义的函数

时间:2017-03-08 10:36:49

标签: c++ import

我试图创建一个可以执行任何功能的数字集成的程序。为此,用户需要能够指定需要集成的功能(代数)。目前,该功能定义为f[i] = 2 * x[i]。是否可以让用户输入功能(使用cin或其他东西)?

void fun(int N, float x_min, float x_max, float *f, float *x ) {
    float deltaX = (x_max - x_min) / (N - 1);
    x[0] = x_min;
    for (int i = 0; i < N; i++ ) {
        f[i] = 2 * x[i];                 // This function should be user defined!
        if (i == N - 1) {       } 
        else {
            x[i+1] = x[i] + deltaX;
        }
    }
}

我已经包含了程序的其余部分,因为之前提到的可能有点令人困惑:

#include<iostream>
#include<math.h>
using namespace std;

// Delaring functions
void fun(int N, float x_min, float x_max, float *f, float *x);

int main() {
    int N;                      // Number of nodes
    float x_min, x_max;         // Smallest and greatest value on the x-axis

    cin >> N;                   // Enter the number of nodes

    float *f = new float[N];
    float *x = new float[N];

    cin >> x_min;               // Enter smallest x-value
    cin >> x_max;               // Enter greatest x-value

    fun(N, x_min, x_max, f, x);

    // Dome some more stuff here (approximation / integration)!

    delete[] f;             // Remember to clear the pointer from the memory!
    delete[] x;             // Remember to clear the pointer from the memory!

    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您想要接受用户输入,比如std::string,并将其转换为float operator()(float x)的内容。这是一个表达式评估员&#34;,@ MichaelJalz在评论中建议。

一个简单的例子如下:

struct Expression 
{
    virtual float operator()(float x) const = 0;
}

struct LiteralExpression : public Expression 
{
    LiteralExpression (float data) : data(data)
    float operator()(float x) const { return data; }
private:
    float data;
}

struct XExpression : public Expression 
{
    float operator()(float x) const { return x; }
}

struct AddExpression : public Expression
{
    AddExpression (std::unique_ptr<Expression> lhs, std::unique_ptr<Expression> rhs) : lhs(std::move(lhs)), rhs(std::move(rhs)) {}
    float operator()(float x) const { return lhs(x) + rhs(x); }
private:
    std::unique_ptr<Expression> lhs;
    std::unique_ptr<Expression> rhs;
}

struct MulExpression : public Expression
{
    MulExpression (std::unique_ptr<Expression> lhs, std::unique_ptr<Expression> rhs) : lhs(std::move(lhs)), rhs(std::move(rhs)) {}
    float operator()(float x) const { return lhs(x) * rhs(x); }
private:
    std::unique_ptr<Expression> lhs;
    std::unique_ptr<Expression> rhs;
}

// more operations

std::unique_ptr<Expression> parse_expression(std::string input)
{
    // Order of construction here is precedence of expressions
    std::size_t mul_pos = input.find('*');
    if (mul_pos != input.npos)
    {
        std::unique_ptr<Expression> lhs = parse_expression(input.substr(0, mul_pos - 1));
        std::unique_ptr<Expression> rhs = parse_expression(input.substr(mul_pos + 1));
        return make_unique<MulExpression>(std::move(lhs), std::move(rhs));
    }

    std::size_t add_pos = input.find('*');
    if (add_pos != input.npos)
    {
        std::unique_ptr<Expression> lhs = parse_expression(input.substr(0, add_pos - 1));
        std::unique_ptr<Expression> rhs = parse_expression(input.substr(add_pos + 1));
        return make_unique<AddExpression>(std::move(lhs), std::move(rhs));
    }

    // Other expressions

    x_pos = input.find('x');
    if (x_pos != input.npos)
    {
         return make_unique<XExpression>();
    }

    float literal = stof(input);
    return make_unique<LiteralExpression>(literal);
}