Сделал 1 Задание со списками
This commit is contained in:
parent
5dbf4901b8
commit
632a718df1
71 changed files with 106 additions and 0 deletions
38
ProcessingSequences/Second/count_elements.c
Normal file
38
ProcessingSequences/Second/count_elements.c
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include "count_elements.h"
|
||||
|
||||
double detectNumber(double number, double count) {
|
||||
if ((1 - number > INACCURACY) || (number - 5 > INACCURACY)) {
|
||||
return 0;
|
||||
}
|
||||
for (double i = 1.; i < 6; i++) {
|
||||
// printf("%lf ? \n", abs(number - i));
|
||||
if (fabs(number - i) < INACCURACY) return (1 - (int)(fmod(count, pow(10., i)) / pow(10., i - 1))) * pow(10., i - 1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
double summa(double info_count) {
|
||||
int count = 0;
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
count += fmod(info_count, 10.);
|
||||
info_count /= 10;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
double countElements(FILE * file) {
|
||||
double current;
|
||||
double count = 100000.;
|
||||
|
||||
if (fscanf(file, "%lf", ¤t) != 1)
|
||||
{
|
||||
printf("File is empty!\n");
|
||||
return -1;
|
||||
}
|
||||
do {
|
||||
count += detectNumber(current, count);
|
||||
} while (fscanf(file, "%lf", ¤t) == 1);
|
||||
|
||||
return summa(count);
|
||||
}
|
||||
13
ProcessingSequences/Second/count_elements.h
Normal file
13
ProcessingSequences/Second/count_elements.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#ifndef COUNT_ELEMENTS
|
||||
#define COUNT_ELEMENTS
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#define INACCURACY 1.e-10
|
||||
|
||||
double detectNumber(double number, double count);
|
||||
double summa(double count);
|
||||
double countElements(FILE * file);
|
||||
|
||||
#endif
|
||||
23
ProcessingSequences/Second/get_file.c
Normal file
23
ProcessingSequences/Second/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
ProcessingSequences/Second/get_file.h
Normal file
8
ProcessingSequences/Second/get_file.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef GET_FILE
|
||||
#define GET_FILE
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
FILE * getFile(void);
|
||||
|
||||
#endif
|
||||
1
ProcessingSequences/Second/input.txt
Normal file
1
ProcessingSequences/Second/input.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
0.123 -1.12 1 2 1 2 1 2 3.4 5
|
||||
12
ProcessingSequences/Second/main.c
Normal file
12
ProcessingSequences/Second/main.c
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#include <stdio.h>
|
||||
#include "get_file.h"
|
||||
#include "count_elements.h"
|
||||
|
||||
int main(void) {
|
||||
FILE * file = getFile();
|
||||
if (file == NULL) {
|
||||
return -1;
|
||||
}
|
||||
printf("Count of numbers between 1 to 5 is %.0lf", countElements(file));
|
||||
return 0;
|
||||
}
|
||||
11
ProcessingSequences/Second/makefile
Normal file
11
ProcessingSequences/Second/makefile
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
all: main.o count_elements.o get_file.o
|
||||
gcc main.o count_elements.o get_file.o && del *.o
|
||||
|
||||
main.o: main.c
|
||||
gcc -c main.c
|
||||
|
||||
count_elements.o: count_elements.c
|
||||
gcc -c count_elements.c
|
||||
|
||||
get_file.o: get_file.c
|
||||
gcc -c get_file.c
|
||||
Loading…
Add table
Add a link
Reference in a new issue