Сделал 1 Задание со списками

This commit is contained in:
AZEN-SGG 2024-10-04 10:43:43 +03:00
parent 5dbf4901b8
commit 632a718df1
71 changed files with 106 additions and 0 deletions

View file

@ -0,0 +1,30 @@
#include "count_between.h"
double fmax(double a, double b) {
if ((a - b) > eps) return a;
return b;
}
double fmin(double a, double b) {
if ((a - b) > eps) return b;
return a;
}
int countBetween(FILE * file) {
double first, second, current;
int count = 0;
if ((fscanf(file, "%lf", &first) != 1) || (fscanf(file, "%lf", &second) != 1)) {
printf("There is NO 2 variables\n");
return 1;
}
current = fmax(first, second);
second = fmin(first, second);
first = current;
while (fscanf(file, "%lf", &current) == 1) {
if (((current - second) > eps) && ((first - current) > eps)) count++;
}
return count;
}

View file

@ -0,0 +1,12 @@
#ifndef COUNT_BETWEEN
#define COUNT_BETWEEN
#include <stdio.h>
#define eps 1.e-6
double fmax(double a, double b);
double fmin(double a, double b);
int countBetween(FILE * file);
#endif // COUNT_BETWEEN

View file

@ -0,0 +1 @@
1 5 2 3 4 1.1 10 5.5 0.5 0 -1 -2

View file

@ -0,0 +1,12 @@
#include <stdio.h>
#include "tools.h"
#include "count_between.h"
// 10Ex
int main(void) {
FILE * file = getFile();
if (file == NULL) return 1;
printf("Count of number between first and second is %d", countBetween(file));
}

View file

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

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;
}
}

View file

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

View file

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

View file

@ -0,0 +1,21 @@
#include <stdio.h>
#include "tools.h"
#include "make_increasing.h"
/*
23 Task
Example:
1 2 3 4 5 - possible
2 1 3 5 4 - impossible
*/
int main(void) {
FILE * file = getFile();
if (file == NULL) return 1;
if (makeIncreasing(file) != TRUE) {
printf("No, it is impossible");
return 1;
} else printf("Yes, it is possible");
return 0;
}

View file

@ -0,0 +1,21 @@
#include "make_increasing.h"
int makeIncreasing(FILE * file) {
double current, next;
unsigned short used = 0;
if (fscanf(file, "%lf", &current) != 1) {
printf("File is empty!");
return 0;
}
while (fscanf(file, "%lf", &next) == 1) {
if ((next - current) < DISREG) {
if (used == TRUE) return 0;
used = TRUE;
}
current = next;
}
return 1;
}

View file

@ -0,0 +1,12 @@
#ifndef MAKE_INCREASING
#define MAKE_INCREASING
#include <stdio.h>
#include <math.h>
#define DISREG 1.e-6
#define TRUE 1
int makeIncreasing(FILE * file);
#endif

View file

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

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;
}
}

View file

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

View file

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

View file

@ -0,0 +1,23 @@
#include <stdio.h>
#include "tools.h"
#include "solve_polynomial.h"
/*
Problem 45
Coordinate X: 2
Solution: 129
Derivative: 222
*/
int main(void) {
double derivative, polynomial, x;
FILE * file = getFile();
if (file == NULL) return 1;
printf("Enter x coordinat: ");
scanf("%lf", &x);
if (solvePolynomial(file, x, &derivative, &polynomial)) return 1;
printf("Solve of the polynomial is %lf\nSolve of its derivative is %lf\n", polynomial, derivative);
return 0;
}

View file

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

View file

@ -0,0 +1,20 @@
#include "solve_polynomial.h"
int solvePolynomial(FILE * file, double x, double * derivative, double * polynomial) {
double current, i;
if (fscanf(file, "%lf", &current) != 1) {
printf("File is empty!\n");
return 1;
}
*derivative = *polynomial = i = 0.;
do {
*polynomial += current * pow(x, i);
*derivative += i * current * pow(x, i - 1);
i++;
} while (fscanf(file, "%lf", &current) == 1);
return 0;
}

View file

@ -0,0 +1,9 @@
#ifndef SOLVE_POLYNOMIAL
#define SOLVE_POLYNOMIAL
#include <stdio.h>
#include <math.h>
int solvePolynomial(FILE * file, double x, double * derivative, double * polynomial);
#endif // SOLVE_POLYNOMIAL

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;
}
}

View file

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

View file

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

View file

@ -0,0 +1,22 @@
#include <stdio.h>
#include "tools.h"
#include "solution_polynomial.h"
/*
Example: 5 4 3 2 1
x: 2
Answer: 80 + 32 + 12 + 4 + 1 = 129
160 + 48 + 12 + 2 = 222
*/
int main(void) {
double x, derivative, polynomial;
FILE * file = getFile();
if (file == NULL) return 1;
printf("Enter the x cordinate: ");
scanf("%lf", &x);
if (solutionPolynomial(file, x, &derivative, &polynomial)) return 1;
printf("The solution of the polynomial is %.0lf\nThe solution of the derivative is %.0lf", polynomial, derivative);
}

View file

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

View file

@ -0,0 +1,20 @@
#include "solution_polynomial.h"
int solutionPolynomial(FILE * file, double x, double * derivative, double * polynomial) {
double current;
if (fscanf(file, "%lf", &current) != 1) {
printf("File is empty!");
return 1;
}
*derivative = *polynomial = 0;
do {
*derivative = *derivative * x + *polynomial;
*polynomial = *polynomial * x + current;
// c4x^3 + c3x^2 + c2x + c = c = c2x + c = c3x^2 + c2x + c = ...
} while (fscanf(file, "%lf", &current) == 1);
return 0;
}

View file

@ -0,0 +1,8 @@
#ifndef SOLUTION_POLYNOMIAL
#define SOLUTION_POLYNOMIAL
#include <stdio.h>
int solutionPolynomial(FILE * file, double x, double * derivative, double * polynomial);
#endif // SOLUTION_POLYNOMIAL

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;
}
}

View file

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

View 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;
}
}

View file

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

View file

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

View 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;
}

View 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

View 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", &current) != 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", &current) == 1);
ar_mean = sum / count;
return ((sq_sum - (sum * ar_mean * 2)) / count) + ipow(ar_mean, 2);
}

View 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

View 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;
}
}

View file

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

View file

@ -0,0 +1,7 @@
11
2
3
4
5
6
10

View file

@ -0,0 +1,17 @@
#include <stdio.h>
#include "get_file.h"
#include "max_difference.h"
/*
36 Çàäà÷à
*/
int max(int first, int second);
int maxDifference(FILE * file);
int main(void) {
FILE * file = getFile();
if (file == NULL) return -1;
printf("Maximum difference between elements is %d", maxDifference(file));
return 0;
}

View file

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

View file

@ -0,0 +1,23 @@
#include "max_difference.h"
int max(int first, int second) {
if (first > second) return first;
return second;
}
int maxDifference(FILE * file) {
int current, last, maxim;
if (fscanf(file, "%d", &current) != 1) {
printf("File is empty!\n");
return -1;
}
last = current;
maxim = 0;
while (fscanf(file, "%d", &current) == 1) {
maxim = max(maxim, abs(last - current));
last = current;
}
return maxim;
}

View file

@ -0,0 +1,10 @@
#ifndef MAX_DIFFERENCE
#define MAX_DIFFERENCE
#include <stdio.h>
#include <stdlib.h>
int max(int first, int second);
int maxDifference(FILE * file);
#endif // MAX_DIFFERENCE

View 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", &current) != 1)
{
printf("File is empty!\n");
return -1;
}
do {
count += detectNumber(current, count);
} while (fscanf(file, "%lf", &current) == 1);
return summa(count);
}

View 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

View 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;
}
}

View file

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

View file

@ -0,0 +1 @@
0.123 -1.12 1 2 1 2 1 2 3.4 5

View 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;
}

View 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

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));
}

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

View file

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

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;
}

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

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;
}
}

View file

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

View file

@ -0,0 +1,37 @@
#include "count_max_local.h"
int max(int first, int second) {
if (first > second) {
return first;
}
return second;
}
int getCountMaxLocal(FILE *file) {
int current_n, last, length, maxLen;
if (fscanf(file, "%d", &current_n) != 1) {
printf("File is empty!\n");
return -1;
}
last = current_n;
maxLen = EMPTY;
length = 1;
while (fscanf(file, "%d", &current_n) == 1) {
if (last == current_n) {
if (length) length++;
} else {
if (last > current_n) {
maxLen = max(maxLen, length);
length = EMPTY;
} else {
length = 1;
}
}
last = current_n;
}
return max(maxLen, length);
}

View file

@ -0,0 +1,11 @@
#ifndef COUNT_MAX_LOCAL
#define COUNT_MAX_LOCAL
#include <stdio.h>
#define EMPTY 0
int max(int first, int second);
int getCountMaxLocal(FILE * file);
#endif

View 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;
}
}

View file

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

View file

@ -0,0 +1,18 @@
1
2
2
2
2
2
1
2
2
2
1
2
2
2
2
2
2
2

View file

@ -0,0 +1,13 @@
#include <stdio.h>
#include "get_file.h"
#include "count_max_local.h"
int main(void)
{
FILE * file = getFile();
if (file == NULL) {
return -1;
}
printf("Max length of local maximum is %d", getCountMaxLocal(file));
return 0;
}

View file

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