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;
}
查找到腳本語言。 –
你需要一個表達式求值器,寫一個並不重要,但它也不是很難。你應該可以通過谷歌搜索找到這樣的事情。 –
@VittorioRomeo我已經知道Python和Matlab,並創建了該算法。現在我試圖用C++來做,因爲我一直都想學習它。 – bgaard