All tasks are done!
This commit is contained in:
parent
ab0fbf59d0
commit
48b359f34c
131 changed files with 2713 additions and 438 deletions
2
2025.03.14/04Ex/a.txt
Normal file
2
2025.03.14/04Ex/a.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
1 2 3
|
||||
4 5 6
|
40
2025.03.14/04Ex/array_io.c
Normal file
40
2025.03.14/04Ex/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/04Ex/array_io.h
Normal file
11
2025.03.14/04Ex/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
|
3
2025.03.14/04Ex/b.txt
Normal file
3
2025.03.14/04Ex/b.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
3
|
||||
2
|
||||
1
|
30
2025.03.14/04Ex/init_f.c
Normal file
30
2025.03.14/04Ex/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/04Ex/init_f.h
Normal file
9
2025.03.14/04Ex/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/04Ex/io_status.h
Normal file
13
2025.03.14/04Ex/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
|
161
2025.03.14/04Ex/main.c
Normal file
161
2025.03.14/04Ex/main.c
Normal file
|
@ -0,0 +1,161 @@
|
|||
#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_a [filename_a] k_b [filename_b] k_x [filename_x] */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
double t, res, *a, *b, *x;
|
||||
int n, m, p, k_a, k_b, k_x, task = 4;
|
||||
char *name_a = 0, *name_b = 0, *name_x = 0;
|
||||
|
||||
if (!((argc >= 7 && argc <= 10) &&
|
||||
sscanf(argv[1], "%d", &n) == 1 &&
|
||||
sscanf(argv[2], "%d", &m) == 1 &&
|
||||
sscanf(argv[3], "%d", &p) == 1 &&
|
||||
sscanf(argv[4], "%d", &k_a) == 1 &&
|
||||
(k_a >= 0 && k_a <= 4) &&
|
||||
(!(k_a == 0 && argc == 7)) &&
|
||||
((k_a == 0 && sscanf(argv[6], "%d", &k_b) == 1) ||
|
||||
(k_a != 0 && sscanf(argv[5], "%d", &k_b) == 1)) &&
|
||||
(k_b >= 0 && k_b <= 4) &&
|
||||
(!(k_b == 0 && ((argc <= 7) || (k_a == 0 && argc <= 8)))) &&
|
||||
((k_a == 0 && k_b == 0 && sscanf(argv[8], "%d", &k_x) == 1) ||
|
||||
(k_a != 0 && k_b != 0 && sscanf(argv[6], "%d", &k_x) == 1) ||
|
||||
(sscanf(argv[7], "%d", &k_x) == 1)) &&
|
||||
(k_x >= 0 && k_x <= 4) &&
|
||||
(!(k_x == 0 && ((k_a == 0 && k_b == 0 && argc != 10) ||
|
||||
(k_a != 0 && k_b != 0 && argc != 8) ||
|
||||
(k_a != k_b && argc != 9))))))
|
||||
{
|
||||
printf("Usage: %s n m p k_a [filename_a] k_b [filename_b] k_x [filename_x]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (argc != 7)
|
||||
{
|
||||
int i_b = 6;
|
||||
if (k_a == 0) { name_a = argv[5]; i_b++; }
|
||||
if (k_b == 0) { name_b = argv[i_b]; i_b+=2; }
|
||||
else i_b++;
|
||||
if (k_x == 0) name_x = argv[i_b];
|
||||
}
|
||||
|
||||
a = (double *)malloc(n * m * sizeof(double));
|
||||
if (!a)
|
||||
{
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
b = (double *)malloc(n * sizeof(double));
|
||||
if (!b)
|
||||
{
|
||||
free(a);
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
x = (double *)malloc(m * sizeof(double));
|
||||
if (!x)
|
||||
{
|
||||
free(a);
|
||||
free(b);
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (name_a)
|
||||
{ /* Читаем матрицу A из файла */
|
||||
io_status ret;
|
||||
ret = read_matrix(a, n, m, name_a);
|
||||
do {
|
||||
switch (ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name_a);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name_a);
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
free(b);
|
||||
free(x);
|
||||
return 3;
|
||||
} while (0);
|
||||
}
|
||||
else init_matrix(a, n, m, k_a);
|
||||
|
||||
if (name_b)
|
||||
{ /* Читаем матрицу B из файла */
|
||||
io_status ret;
|
||||
ret = read_matrix(b, n, 1, name_b);
|
||||
do {
|
||||
switch (ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name_b);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name_b);
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
free(b);
|
||||
free(x);
|
||||
return 3;
|
||||
} while (0);
|
||||
}
|
||||
else init_matrix(b, n, 1, k_b);
|
||||
|
||||
if (name_x)
|
||||
{ /* Читаем матрицу X из файла */
|
||||
io_status ret;
|
||||
ret = read_matrix(x, m, 1, name_x);
|
||||
do {
|
||||
switch (ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name_x);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name_x);
|
||||
break;
|
||||
}
|
||||
free(a);
|
||||
free(b);
|
||||
free(x);
|
||||
return 3;
|
||||
} while (0);
|
||||
}
|
||||
else init_matrix(x, m, 1, k_x);
|
||||
|
||||
printf("Matrix:\n");
|
||||
print_matrix(a, n, m, p);
|
||||
printf("Initial vector b:\n");
|
||||
print_matrix(b, n, 1, p);
|
||||
printf("Initial vector x:\n");
|
||||
print_matrix(x, m, 1, p);
|
||||
|
||||
t = clock();
|
||||
res = t4_solve(a, x, b, m, n);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
|
||||
|
||||
free(a);
|
||||
free(b);
|
||||
free(x);
|
||||
|
||||
return 0;
|
||||
}
|
29
2025.03.14/04Ex/solve.c
Normal file
29
2025.03.14/04Ex/solve.c
Normal file
|
@ -0,0 +1,29 @@
|
|||
#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 t4_solve(const double *A, const double *X, double *B, int m, int n)
|
||||
{
|
||||
double maximum = 0, cur;
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
cur = -B[i];
|
||||
for (j = 0; j < m; j++)
|
||||
cur += A[i * m + j] * X[j];
|
||||
cur = fabs(cur);
|
||||
if (compare(cur, maximum) == 1) maximum = cur;
|
||||
}
|
||||
|
||||
return maximum;
|
||||
}
|
||||
|
7
2025.03.14/04Ex/solve.h
Normal file
7
2025.03.14/04Ex/solve.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
int compare(const double a, const double b);
|
||||
double t4_solve(const double *A, const double *X, double *B, int m, int n);
|
||||
|
||||
#endif
|
3
2025.03.14/04Ex/x.txt
Normal file
3
2025.03.14/04Ex/x.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
1
|
||||
2
|
||||
3
|
Loading…
Add table
Add a link
Reference in a new issue