2017-03-08 29 views
1

我試圖創建一個可以執行任何函數的數字集成的程序。爲此,用戶需要能夠指定需要集成的函數(代數)。目前,該功能被定義爲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

查找到腳本語言。 –

+2

你需要一個表達式求值器,寫一個並不重要,但它也不是很難。你應該可以通過谷歌搜索找到這樣的事情。 –

+0

@VittorioRomeo我已經知道Python和Matlab,並創建了該算法。現在我試圖用C++來做,因爲我一直都想學習它。 – bgaard

回答

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