-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
59 lines (47 loc) · 978 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
#include <iostream>
#include "main.h"
using std::cin;
using std::cout;
double L(double x, int k, double *xi, int d) {
int i;
double ans = 1.0;
for (i = 0; i <= d; i++) {
if (i == k) continue;
ans *= (x - xi[i]) / (xi[k] - xi[i]);
}
return ans;
}
double p(double x, double *xi, double *fi, int d) {
int i;
double ans = 0.0;
for (i = 0; i <= d; i++)
ans += fi[i] * L(x, i, xi, d);
return ans;
}
int main() {
char s[256];
int d, i;
double *xi, *fi, x;
cout << "Number of cases: ";
cin >> d;
xi = new double[d];
fi = new double[d];
d--;
for (i = 0; i <= d; i++) {
cout << "\nEnter x" << i << ": ";
cin >> xi[i];
cout << "Enter f" << i << ": ";
cin >> fi[i];
}
while (1) {
cout << "\nFind f(x) at x: ";
cin >> x;
cout << "f(" << x << ") = " << p(x, xi, fi, d) << "\n";
cout << "Stop? ";
cin >> s;
if (s[0] == 'y' || s[0] == 'Y')break;
}
delete fi;
delete xi;
return 0;
}