#include
#include
#include
int N;
double* x;//< massiv piketov
double* y; //< massiv izmeryaemoi velichini
//pluchenie piketov v faileint
fSize(FILE*fInput)
{int num;
char c;
fseek(fInput, 0, SEEK_SET);
//ustanavlivaem kursor na nachalo faila
num = 0;
//schitaem kolichestvo perevodov strok
while(!feof(fInput))
{
fscanf(fInput, "%c", &c);
//perehod na sleduyushchuyu stroku
if (c=='\n')
{
num++;
}
}
fseek (fInput, 0, SEEK_SET); //vozvrashchaem kursor na nachalo faila
return num; //vozvrashchaem kolichectvo strok
}
//poluchenie velichini v tochke newx
double interpol(double newx)
{
int i;
double ans = 0; //iskomaya velichina
//ishchem interval soderzashchii newx
for (i = 0; i < N - 1; i = i + 1)
{
if ( x[i] <= newx && newx <= x[i + 1])
{
//nashli interval, poluchili znachenie, vihodim iz sikla
ans = (y[i] + (y[i+1] - y[i]) * (newx - x[i]) / (x[i+1] - x[i]));
break;
}
}
return ans;
//vozvrashchaem iskomuyu velichinu
}
int main(int argc, char **argv)
{
FILE *fInput;
FILE * fOutput;
int i, k, newN;
double first, last, sr, min, max, h, cur, dx;
char c;
if (argc!= 3)
{
printf ("V komandnoi stroke net imeni fila dannih\n");
exit (0);
}
fInput = fopen(argv[1],"rt");
{
//ecli fail ne udalos otkrt
if (NULL == fInput)
{
//vivodim soobshchenie i zavershaem programmu
printf(" Faila s dannumi net! (Input.txt) \n");
return -1;
}
}
fOutput = fopen (argv[2], "wt");
{
if (NULL == fOutput)
{
printf(" Faila s rezultatom sozdat ne udalos! (Output.txt) \n");
return -2;
}
}
N = fSize(fInput); //< kolichestvo piketov
//schitivaem pervuyu ctroku
do
{
//dochitat do konsa stroki faila
fscanf(fInput,"%c", &c);
}
// pereiti na sleduyushchuyu stroku
while (c!='\n');
// videlyaem pamyat pod massivi piketov i velichin
x = (double*)malloc(N * sizeof(double));
if (x == NULL )
exit (0);
y = (double*)malloc(N * sizeof(double));
if (y == NULL)
exit (0);
//zapolnyaem massivifor
(i = 0; i < N; i++)
{
k=fscanf(fInput, "%lf\t%lf", &x[i], &y[i]);
if (k!=2)
{
printf("Error in %d line!!!\n", i+2 );
return -3;
}
}
fclose(fInput);
printf("Nazhmite lubuy klavishu\n");
getch(); //ozidanie nazatiya klavishi
//poisk pervogo i poslednego piketa, vichislyaem srednii shag
first = x[0];last = x[N-1];
sr = 0;
if (N > 1)
{sr = (x[N - 1] - x[0]) / (N - 1);
}
//poisk minimalnogo i maksimalnogo shaga
min = x[1] - x[0]; //nachalnie prisvaivaniya
max = x[1] - x[0];
for (i = 0; i < N - 1; i++ )
{dx = x[i + 1] - x[i];
//tekushchii shag
if (dx > max){max = dx;
}
if (dx < min)
{
min = dx;
}
}
fprintf(stderr"Final piket: %lf\n", first);
fprintf(stderr"Start piket: %lf\n", last);
fprintf(stderr"Sredniy shag: %lf\n", sr);
fprintf(stderr,"Maximalnuy shag: %lf\n", max);
fprintf(stderr,"Minimalnuy shag: %lf\n", min);
//schitivaem novii shagprintf("Vvedite novuy shag: ");
scanf("%lf", &h);
//kolichestvo piketov pri zadannom shage
newN = (int)((x[N-1]-x[0])/h) + 1;
fprintf(fOutput, "pk\tdT\n");
cur = x[0];
for (i = 0; i < newN; i++ )
{
fprintf(fOutput, "%.8lf\t%.8lf \n", cur, interpol(cur));
cur = cur + h;
}
fclose(fOutput);
free(x);
free(y);
printf("Nazhmite lubuy klavishu\n");
getch();
return 0;
}
P.S. Простенько и со вкусом)))