Сделал 38 Задание

This commit is contained in:
AZEN-SGG 2024-09-22 20:52:39 +03:00
parent c6260ab148
commit dae8abd5cc
7 changed files with 109 additions and 0 deletions

24
Sixth/exp_weigh_avg.c Normal file
View file

@ -0,0 +1,24 @@
#include "exp_weigh_avg.h"
int betweenZeroOne(double lambda) {
if ((lambda < FAULT) || ((1 - lambda) <= FAULT)) return 0;
return 1;
}
double expWeigthAvg(FILE * file, double lambda) {
double current, i, eavg, lambda_n;
if (fscanf(file, "%lf", &current) != 1) {
printf("File is empty!\n");
return -1.;
}
i = 1.;
eavg = 0.;
do {
eavg += current * pow(lambda, -i);
i++;
} while (fscanf(file, "%lf", &current) == 1);
lambda_n = pow(lambda, i - 1);
return (eavg * lambda_n) * ((1 - lambda) / (1 - lambda_n));
}

13
Sixth/exp_weigh_avg.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef EXP_WEIGH_AVG
#define EXP_WEIGH_AVG
#include <stdio.h>
#include <math.h>
#define FALSE 0
#define FAULT 1.e-10
int betweenZeroOne(double lambda);
double expWeigthAvg(FILE * file, double lambda);
#endif // EXP_WEIGH_AVG

5
Sixth/input.txt Normal file
View file

@ -0,0 +1,5 @@
5
4
3
2
1

25
Sixth/main.c Normal file
View file

@ -0,0 +1,25 @@
#include <stdio.h>
#include "tools.h"
#include "exp_weigh_avg.h"
/*
Çàäàíèå 38
Ïðèìåð:
5 4 3 2 1 è ëÿìáäà: 0.5
Îòâåò: 1.83...
*/
int main(void) {
double lambda;
FILE * file = getFile();
printf("Enter lambda: ");
scanf("%lf", &lambda);
if (betweenZeroOne(lambda) == FALSE) {
printf("Lambda is not between 0 to 1!");
return -1;
}
printf("The exponentially weighted average is %lf\n", expWeigthAvg(file, lambda));
return 0;
}

11
Sixth/makefile Normal file
View file

@ -0,0 +1,11 @@
all: main.o exp_weigh_avg.o tools.o
gcc main.o exp_weigh_avg.o tools.o && del *.o
main.o: main.c
gcc -c main.c
exp_weigh_avg.o: exp_weigh_avg.c
gcc -c exp_weigh_avg.c
tools.o: tools.c
gcc -c tools.c

23
Sixth/tools.c Normal file
View file

@ -0,0 +1,23 @@
#include "tools.h"
FILE * getFile(void)
{
FILE * file;
char filename[50];
printf("Enter filename: ");
if (scanf("%s", filename) == 1)
{
file = fopen(filename, "r");
if (file == NULL) {
printf("Error file!\n");
return NULL;
} else {
return file;
}
} else
{
printf("Empty name!\n");
return NULL;
}
}

8
Sixth/tools.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef TOOLS
#define TOOLS
#include <stdio.h>
FILE * getFile(void);
#endif