Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "f.h"
  5.  
  6. int main (int argc, const char **argv)
  7. {
  8.     FILE *in;
  9.     double x0,*x,*y,res;
  10.     int n,i;
  11.     clock_t time;
  12.  
  13.     if(argc!=3)
  14.     {
  15.         fprintf(stderr,"Usage %s n input_file x0\n",argv[0]);
  16.         return ERROR_READ;
  17.     }
  18.     if(sscanf(argv[1], "%d" ,&n)!=1 || n<2)
  19.     {  
  20.         fprintf(stderr,"Can not read n!\n");
  21.         return ERROR_READ;
  22.     }
  23.     if(!(x = (double *)malloc(n*sizeof(double))))
  24.     {
  25.         fprintf(stderr,"Can not allocate memory x[n]\n");
  26.         return ERROR_MEMORY;
  27.     }
  28.     if(!(y = (double *)malloc(n*sizeof(double))))
  29.     {
  30.         fprintf(stderr,"Can nor allocate memory y[n]\n");
  31.         return ERROR_MEMORY;
  32.     }
  33.     if(!(in = fopen(argv[2],"r")))
  34.     {
  35.         fprintf(stderr,"Can nor read %s\n",argv[2]);
  36.         free(x);
  37.         free(y);
  38.         return ERROR_OPEN;
  39.     }
  40.     for(i = 0; i<(n);i++)
  41.     {
  42.         if(fscanf(in,"%lf",x + i)!=1)
  43.         {
  44.             fprintf(stderr,"Can nor read elments from %s\n",argv[2]);
  45.                         free(x);
  46.                         free(y);
  47.                         return ERROR_READ;
  48.         }
  49.         if(fscanf(in,"%lf",y + i)!=1)
  50.         {
  51.             fprintf(stderr,"Can nor read elments from %s\n",argv[2]);
  52.             free(x);
  53.             free(y);
  54.             return ERROR_READ;
  55.         }
  56.     }
  57.     fclose(in);
  58.     printf("x0 = ");
  59.     if( scanf("%lf", &x0)!=1 )
  60.     {
  61.         fprintf(stderr, "Error! Can not read x0!\n");
  62.         free(x);
  63.         free(y);
  64.         return ERROR_READ;
  65.     }
  66.     /*if(sscanf(argv[4], "%lf", &x0)!=1)
  67.     {
  68.         fprintf(stderr,"Can not read %s\n",argv[4]);
  69.         free(x);
  70.         free(y);
  71.         return ERROR_READ;
  72.     }*/
  73.     time = clock();
  74.     res = fun(n,x0,x,y);
  75.     printf("RESULT: %lf\n  TIME: %lf\n",res,((double)(clock() - time) )/CLOCKS_PER_SEC);
  76.     free(x);
  77.     free(y);
  78.     return 0;
  79. }
  80. #include<stdio.h>
  81. #include<stdlib.h>
  82. #include "f.h"
  83.  
  84. double fun(int n,double x0,double*x,double*y)
  85. {
  86.     int i,j;
  87.     double res = y[0],k = 1;
  88.     for(i=0;i<n-1;i++)
  89.     {
  90.         k *=(x0 - x[i]);
  91.         for(j=0;j<(n-1-i);j++)
  92.             y[j] = (y[j + 1] - y[j])/(x[j + i + 1] - x[j]);
  93.         res += y[0]*k;
  94.            
  95.     }
  96.     return res;
  97. }  
  98. #ifndef F_H
  99. #define F_H
  100.  
  101. double fun(int n,double x0, double *x, double *y);
  102.  
  103. #define ERROR_READ 1
  104. #define ERROR_OPEN 2
  105. #define ERROR_MEMORY 3
  106. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement