-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
65 lines (52 loc) · 966 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include "main.h"
using std::cin;
using std::cout;
// Global Variables
int d;
double xi, *fi, h;
int main() {
char s[256];
int i;
double x;
cout << "Number of cases: ";
cin >> d;
fi = new double[d];
d--;
cout << "\nEnter x0: ";
cin >> xi;
cout << "Enter h: ";
cin >> h;
cout << "\n";
for (i = 0; i <= d; i++) {
cout << "Enter f" << i << ": ";
cin >> fi[i];
}
while (1) {
cout << "\nFind f(x) at x: ";
cin >> x;
cout << "f(" << x << ") = " << p(x) << "\n";
cout << "Stop? ";
cin >> s;
if (s[0] == 'y' || s[0] == 'Y') break;
}
delete fi;
return(0);
}
double f(int j, int k) {
if (j > 0) return f(j-1, k) - f(j-1, k-1);
return(fi[d+k]);
}
double p(double x) {
int i;
double r, fc = 0, fp = 1, a = 1, ans = 0;
r = (x - (xi + d*h)) / h;
for (i = 0; i <= d; i++) {
ans += a * f(i, 0) / fp;
a *= r;
r++;
fc++;
fp *= fc;
}
return ans;
}