All tasks are done!
This commit is contained in:
parent
ab0fbf59d0
commit
48b359f34c
131 changed files with 2713 additions and 438 deletions
3
2025.03.14/08Ex/a.txt
Normal file
3
2025.03.14/08Ex/a.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
1 2 3 4
|
||||
1 1 1 1
|
||||
2 2 2 2
|
40
2025.03.14/08Ex/array_io.c
Normal file
40
2025.03.14/08Ex/array_io.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#include "array_io.h"
|
||||
|
||||
io_status read_matrix(double *a, int n, int m, const char *name)
|
||||
{
|
||||
int i, j;
|
||||
FILE *fp;
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < m; j++)
|
||||
if (fscanf(fp, "%lf", a + i * m + j) != 1)
|
||||
{fclose(fp); return ERROR_READ;}
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void print_matrix(const double *a, int n, int m, int p)
|
||||
{
|
||||
int np = (n > p ? p : n);
|
||||
int mp = (m > p ? p : m);
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < np; i++)
|
||||
{
|
||||
for (j = 0; j < mp; j++)
|
||||
printf(" %10.3e", a[i * m + j]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void init_matrix(double *a, int n, int m, int k)
|
||||
{
|
||||
double (*q)(int, int, int, int);
|
||||
double (*f[])(int, int, int, int) = {f1, f2, f3, f4};
|
||||
int i, j;
|
||||
q = f[k-1];
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < m; j++)
|
||||
a[i * m + j] = q(n, m, i+1, j+1);
|
||||
}
|
11
2025.03.14/08Ex/array_io.h
Normal file
11
2025.03.14/08Ex/array_io.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef ARRAY_IO_H
|
||||
#define ARRAY_IO_H
|
||||
|
||||
#include "io_status.h"
|
||||
#include "init_f.h"
|
||||
|
||||
io_status read_matrix(double *a, int n, int m, const char *name);
|
||||
void print_matrix(const double *a, int n, int m, int p);
|
||||
void init_matrix(double *a, int n, int m, int k);
|
||||
|
||||
#endif
|
30
2025.03.14/08Ex/init_f.c
Normal file
30
2025.03.14/08Ex/init_f.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include "init_f.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAX(n, m) (n < m ? m : n)
|
||||
|
||||
double f1(int n, int m, int i, int j)
|
||||
{
|
||||
return MAX(n, m) - MAX(i, j) + 1;
|
||||
}
|
||||
|
||||
double f2(int n, int m, int i, int j)
|
||||
{
|
||||
(void)n;
|
||||
(void)m;
|
||||
return MAX(i, j);
|
||||
}
|
||||
|
||||
double f3(int n, int m, int i, int j)
|
||||
{
|
||||
(void)n;
|
||||
(void)m;
|
||||
return abs(i - j);
|
||||
}
|
||||
|
||||
double f4(int n, int m, int i, int j)
|
||||
{
|
||||
(void)n;
|
||||
(void)m;
|
||||
return 1./(i+j-1);
|
||||
}
|
9
2025.03.14/08Ex/init_f.h
Normal file
9
2025.03.14/08Ex/init_f.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef INIT_F_H
|
||||
#define INIT_F_H
|
||||
|
||||
double f1(int n, int m, int i, int j);
|
||||
double f2(int n, int m, int i, int j);
|
||||
double f3(int n, int m, int i, int j);
|
||||
double f4(int n, int m, int i, int j);
|
||||
|
||||
#endif
|
13
2025.03.14/08Ex/io_status.h
Normal file
13
2025.03.14/08Ex/io_status.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ
|
||||
} io_status;
|
||||
|
||||
#endif
|
67
2025.03.14/08Ex/main.c
Normal file
67
2025.03.14/08Ex/main.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "array_io.h"
|
||||
#include "io_status.h"
|
||||
#include "solve.h"
|
||||
|
||||
/* ./a.out n m p k [filename] */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
double t, res, *a;
|
||||
int n, m, p, k, task = 8;
|
||||
char *name = 0;
|
||||
|
||||
if (!((argc == 5 || argc == 6) &&
|
||||
sscanf(argv[1], "%d", &n) == 1 &&
|
||||
sscanf(argv[2], "%d", &m) == 1 &&
|
||||
sscanf(argv[3], "%d", &p) == 1 &&
|
||||
sscanf(argv[4], "%d", &k) == 1 &&
|
||||
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
|
||||
{
|
||||
printf("Usage: %s n m p k [filename]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (argc == 6) name = argv[5];
|
||||
|
||||
a = (double *)malloc(n * m * sizeof(double));
|
||||
if (!a)
|
||||
{
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (name)
|
||||
{ /* из файла */
|
||||
io_status ret;
|
||||
ret = read_matrix(a, n, m, name);
|
||||
do {
|
||||
switch (ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name);
|
||||
}
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
} else init_matrix(a, n, m, k);
|
||||
|
||||
printf("Matrix:\n");
|
||||
print_matrix(a, n, m, p);
|
||||
|
||||
t = clock();
|
||||
res = t8_solve(a, n, m);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("New matrix:\n");
|
||||
print_matrix(a, n, m, p);
|
||||
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
|
||||
|
||||
free(a);
|
||||
return 0;
|
||||
}
|
32
2025.03.14/08Ex/solve.c
Normal file
32
2025.03.14/08Ex/solve.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
#include "solve.h"
|
||||
#include <math.h>
|
||||
|
||||
#define eps 1e-9
|
||||
|
||||
int compare(const double a, const double b)
|
||||
{
|
||||
if (a - b > eps) return 1;
|
||||
else if (b - a > eps) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
double t8_solve(double *a, int n, int m)
|
||||
{
|
||||
int i, j;
|
||||
double cur = 0, maximum = 0;
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < m; j++)
|
||||
if (i == 0 || i == n-1 || j == 0 || j == m-1)
|
||||
if (compare(fabs(a[i*m + j]), maximum) == 1)
|
||||
maximum = fabs(a[i*m + j]);
|
||||
|
||||
for (i = n-2; i > 0; i--)
|
||||
for (j = m-2; j > 0; j--)
|
||||
{
|
||||
cur = (a[(i+1)*m + j] + a[(i-1)*m + j] + a[i*m + (j-1)] + a[i*m + (j+1)]) / 5;
|
||||
if (compare(fabs(cur), maximum) == 1) maximum = fabs(cur);
|
||||
a[i*m + j] = cur;
|
||||
}
|
||||
|
||||
return maximum;
|
||||
}
|
7
2025.03.14/08Ex/solve.h
Normal file
7
2025.03.14/08Ex/solve.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
int compare(const double a, const double b);
|
||||
double t8_solve(double *a, int n, int m);
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue