Facebook
From szymon, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 55
  1. #include<iostream>
  2. #include<fstream>
  3. #include<vector>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. float first(vector<pair<int,int>>tmp, int i)
  9. {
  10.         return ((tmp[i+1].second - tmp[i].second) / (tmp[i+1].first - tmp[i].first));
  11. }
  12.  
  13. float Newton(vector<pair<int,int>>tmp, int k, int i)
  14. {
  15.         if(k == 0)
  16.         {
  17.                 return 1;                                                      
  18.         }
  19.         if(k == 1)
  20.         {
  21.                 return first(tmp, i);  
  22.         }
  23.  
  24.         return  (Newton(tmp, k - 1, i + 1) - Newton(tmp, k - 1, i)) / (tmp[i+k].first - tmp[i].first);
  25.        
  26. }
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. int main()
  34. {
  35.         vector<pair<int,int>>tmp;
  36.         fstream plik;
  37.         plik.open("dane", fstream::in);
  38.         if(plik.good())
  39.         {
  40.                 int n;
  41.                 plik >> n;
  42.                 cout<<"Ilosc wezlow: "<<n<<endl;
  43.                 for(int i=0; i<n; i++)
  44.                 {
  45.                         int x,y;
  46.                         plik >> x;
  47.                         plik >> y;
  48.                         tmp.push_back(make_pair(x,y));
  49.                 }
  50.  
  51.                 for(int m=0; m<n; m++)
  52.                 {
  53.                         cout<<"x: "<<tmp[m].first<<"  y:"<<tmp[m].second<<endl;
  54.                 }
  55.  
  56.                 float q;
  57.                 cin>>q;
  58.                 float result = tmp[0].second;
  59.                 float pomocnicza = 0;
  60.                 for (int i = 0; i < n; i++)
  61.                 {
  62.                         pomocnicza = 0;                        
  63.                         for (int j = 0; j < i; j++)
  64.                         {      
  65.                                 if (j == 0)
  66.                                 {
  67.                                         pomocnicza = q - tmp[0].first; 
  68.                                 }
  69.                                 else
  70.                                 {
  71.                                         pomocnicza *= q - tmp[j].first;        
  72.                                 }
  73.                         }
  74.                         result += pomocnicza*Newton(tmp,i,0);
  75.                         }
  76.  
  77.                 cout<<result;
  78.         }
  79.         else
  80.         {
  81.                 cout<<"Fail open"<<endl;
  82.  
  83.         }
  84.  
  85.        
  86.  
  87.        
  88.  
  89.  
  90.  
  91.  
  92.         return 0;
  93. }
  94.