gpt4 book ai didi

c++ - 如何导入用户定义的函数

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:24 24 4
gpt4 key购买 nike

我正在尝试创建一个可以执行任何函数的数字积分的程序。为此,用户需要能够指定需要集成的函数(代数)。目前,函数定义为 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;
}

最佳答案

您想获取用户输入,例如 std::string,然后使用 float operator()(float x) 将其转换为某种内容。这是一个“表达式评估器”,@MichaelWalz 在评论中建议。

一个简单的例子如下:

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

关于c++ - 如何导入用户定义的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42668977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com