Сделал задание 37
This commit is contained in:
parent
3dd2710fc1
commit
c6260ab148
8 changed files with 96 additions and 0 deletions
23
Fifth/get_file.c
Normal file
23
Fifth/get_file.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "get_file.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
Fifth/get_file.h
Normal file
8
Fifth/get_file.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef GET_FILE
|
||||
#define GET_FILE
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
FILE * getFile(void);
|
||||
|
||||
#endif
|
5
Fifth/input.txt
Normal file
5
Fifth/input.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
14
Fifth/main.c
Normal file
14
Fifth/main.c
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include <stdio.h>
|
||||
#include "get_file.h"
|
||||
#include "max_deviation.h"
|
||||
|
||||
/*
|
||||
Çàäàíèå 37
|
||||
*/
|
||||
|
||||
int main(void) {
|
||||
FILE * file = getFile();
|
||||
if (file == NULL) return -1;
|
||||
printf("The standard deviation from the arithmetic mean is equal to %d", maxDeviation(file));
|
||||
return 0;
|
||||
}
|
11
Fifth/makefile
Normal file
11
Fifth/makefile
Normal file
|
@ -0,0 +1,11 @@
|
|||
all: main.o max_deviation.o get_file.o
|
||||
gcc main.o max_deviation.o get_file.o && del *.o
|
||||
|
||||
main.o: main.c
|
||||
gcc -c main.c
|
||||
|
||||
max_deviation.o: max_deviation.c
|
||||
gcc -c max_deviation.c
|
||||
|
||||
get_file.o: get_file.c
|
||||
gcc -c get_file.c
|
26
Fifth/max_deviation.c
Normal file
26
Fifth/max_deviation.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include "max_deviation.h"
|
||||
|
||||
int ipow(int number, int power) {
|
||||
int result = 1;
|
||||
for (int i = 0; i < power; i++) result *= number;
|
||||
return result;
|
||||
}
|
||||
|
||||
int maxDeviation(FILE * file) {
|
||||
int current, sq_sum, sum, count, ar_mean;
|
||||
if (fscanf(file, "%d", ¤t) != 1) {
|
||||
printf("File is empty!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
sq_sum = sum = count = 0;
|
||||
|
||||
do {
|
||||
sum += current;
|
||||
sq_sum += ipow(current, 2);
|
||||
count++;
|
||||
} while (fscanf(file, "%d", ¤t) == 1);
|
||||
|
||||
ar_mean = sum / count;
|
||||
return ((sq_sum - (sum * ar_mean * 2)) / count) + ipow(ar_mean, 2);
|
||||
}
|
9
Fifth/max_deviation.h
Normal file
9
Fifth/max_deviation.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef MAX_DEVIATION
|
||||
#define MAX_DEVIATION
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int ipow(int number, int power);
|
||||
int maxDeviation(FILE * file);
|
||||
|
||||
#endif // MAX_DEVIATION
|
BIN
Fourth/a.exe
BIN
Fourth/a.exe
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue