Strange Error in 5Ex if: 2 3 5 2 3
This commit is contained in:
parent
48d6198ca3
commit
ab0fbf59d0
13 changed files with 372 additions and 6 deletions
106
.gitignore
vendored
106
.gitignore
vendored
|
@ -2,17 +2,113 @@
|
|||
*.exe
|
||||
*.out
|
||||
*.o
|
||||
*.elf
|
||||
*.app
|
||||
*.bin
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Vim files
|
||||
# Object files and build artifacts
|
||||
*.obj
|
||||
*.a
|
||||
*.lib
|
||||
*.la
|
||||
*.lo
|
||||
*.def
|
||||
*.exp
|
||||
*.pdb
|
||||
|
||||
# Vim swap and backup files
|
||||
*~
|
||||
.*.sw*
|
||||
*.swo
|
||||
*.swn
|
||||
*.bak
|
||||
|
||||
*output*
|
||||
|
||||
# Clion folders and files
|
||||
cmake-build-debug/
|
||||
# IDE and editor files
|
||||
.idea/
|
||||
.vscode/
|
||||
*.sublime-workspace
|
||||
*.sublime-project
|
||||
*.code-workspace
|
||||
|
||||
# CLion and CMake build folders
|
||||
cmake-build-debug/
|
||||
cmake-build-release/
|
||||
CMakeLists.txt
|
||||
CMakeCache.txt
|
||||
CMakeFiles/
|
||||
Makefile
|
||||
build/
|
||||
dist/
|
||||
*.cmake
|
||||
|
||||
# Python files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*.egg-info/
|
||||
*.eggs/
|
||||
*.pyo
|
||||
|
||||
# Java and Kotlin
|
||||
*.class
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
.gradle/
|
||||
build/
|
||||
|
||||
# Node.js and frontend
|
||||
node_modules/
|
||||
npm-debug.log
|
||||
yarn-error.log
|
||||
*.log
|
||||
*.lock
|
||||
*.cache/
|
||||
dist/
|
||||
out/
|
||||
coverage/
|
||||
|
||||
# Rust
|
||||
target/
|
||||
Cargo.lock
|
||||
|
||||
# Go
|
||||
bin/
|
||||
pkg/
|
||||
*.test
|
||||
|
||||
# Logs and temporary files
|
||||
*.log
|
||||
*.tmp
|
||||
*.pid
|
||||
*.seed
|
||||
*.old
|
||||
*.gz
|
||||
*.tar
|
||||
*.tar.gz
|
||||
*.rar
|
||||
*.7z
|
||||
*.bak
|
||||
|
||||
# FreeFileSync files
|
||||
*.ffs_db
|
||||
|
||||
# macOS system files
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Windows system files
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
Desktop.ini
|
||||
$RECYCLE.BIN/
|
||||
System Volume Information/
|
||||
|
||||
# Linux system files
|
||||
lost+found/
|
||||
|
|
|
@ -8,6 +8,9 @@ void matrix_multiply(const double *A, const double *B, double *c, int n, int m,
|
|||
for (k = 0; k < m; k++)
|
||||
{
|
||||
for (j = 0; j < l; j++)
|
||||
{
|
||||
if (k == 0) c[i * l + j] = 0;
|
||||
c[i * l + j] += A[i * m + k] * B[k * l + j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ int compare(const double a, const double b)
|
|||
|
||||
double t4_solve(const double *A, const double *X, double *B, int m, int n)
|
||||
{
|
||||
double maximum = -1, cur;
|
||||
double maximum = 0, cur;
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
|
@ -23,6 +23,7 @@ double t4_solve(const double *A, const double *X, double *B, int m, int n)
|
|||
cur = fabs(cur);
|
||||
if (compare(cur, maximum) == 1) maximum = cur;
|
||||
}
|
||||
|
||||
return maximum;
|
||||
}
|
||||
|
||||
|
|
5
2025.03.14/5Ex/a.txt
Normal file
5
2025.03.14/5Ex/a.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
1 2 3 4
|
||||
4 3 2 1
|
||||
1 2 3 4
|
||||
4 3 2 1
|
||||
1 2 3 4
|
40
2025.03.14/5Ex/array_io.c
Normal file
40
2025.03.14/5Ex/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/5Ex/array_io.h
Normal file
11
2025.03.14/5Ex/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
|
4
2025.03.14/5Ex/b.txt
Normal file
4
2025.03.14/5Ex/b.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
1
|
||||
2
|
||||
3
|
||||
4
|
30
2025.03.14/5Ex/init_f.c
Normal file
30
2025.03.14/5Ex/init_f.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include "init_f.h"
|
||||
#include <math.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/5Ex/init_f.h
Normal file
9
2025.03.14/5Ex/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
|
14
2025.03.14/5Ex/io_status.h
Normal file
14
2025.03.14/5Ex/io_status.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
110
2025.03.14/5Ex/main.c
Normal file
110
2025.03.14/5Ex/main.c
Normal file
|
@ -0,0 +1,110 @@
|
|||
#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] */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
double t, res, *a, *b;
|
||||
int n, m, p, k_a, k_b, task = 5;
|
||||
char *name_a = 0, *name_b = 0;
|
||||
if (!((argc == 6 || argc == 7 || argc == 8) &&
|
||||
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 == 6)) &&
|
||||
((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 == 6)) &&
|
||||
(!((k_a == 0 && k_b == 0) && argc != 8))))
|
||||
{
|
||||
printf("Usage: %s n m p k_a [filename_a] k_b [filename_b]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
if (argc != 6)
|
||||
{
|
||||
int i_b = 6;
|
||||
if (k_a == 0) {name_a = argv[5];i_b++;}
|
||||
if (k_b == 0) name_b = argv[i_b];
|
||||
}
|
||||
|
||||
a = (double *)malloc(n * m * sizeof(double));
|
||||
if (!a)
|
||||
{
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
b = (double *)malloc(m * n * sizeof(double));
|
||||
if (!b)
|
||||
{
|
||||
free(a);
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (name_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);
|
||||
return 3;
|
||||
} while (0);
|
||||
} else init_matrix(a, n, m, k_a);
|
||||
|
||||
if (name_b)
|
||||
{
|
||||
io_status ret;
|
||||
ret = read_matrix(b, m, n, 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);
|
||||
return 3;
|
||||
} while (0);
|
||||
} else init_matrix(b, m, n, k_b);
|
||||
|
||||
printf("Initial matrix A:\n");
|
||||
print_matrix(a, n, m, p);
|
||||
printf("Initial matrix b:\n");
|
||||
print_matrix(b, m, n, p);
|
||||
|
||||
t = clock();
|
||||
t5_solve(a, b, n, m);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
|
||||
|
||||
free(a);
|
||||
free(b);
|
||||
|
||||
return 0;
|
||||
}
|
36
2025.03.14/5Ex/solve.c
Normal file
36
2025.03.14/5Ex/solve.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
#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 t5_solve(const double *A, const double *b, int n, int m)
|
||||
{
|
||||
double temp, summa, maximum = 0;
|
||||
int i, j, k;
|
||||
|
||||
if (n <= 0 || m <= 0) return 0;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
summa = 0;
|
||||
for (k = 0; k < n; k++) {
|
||||
temp = 0;
|
||||
for (j = 0; j < m; j++)
|
||||
temp += A[i * m + j] * b[j * n + k];
|
||||
|
||||
if (k == i) temp--;
|
||||
summa += fabs(temp);
|
||||
}
|
||||
if (compare(summa, maximum) == 1) maximum = summa;
|
||||
}
|
||||
|
||||
return maximum;
|
||||
}
|
||||
|
7
2025.03.14/5Ex/solve.h
Normal file
7
2025.03.14/5Ex/solve.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
int compare(const double a, const double b);
|
||||
double t5_solve(const double *A, const double *b, int n, int m);
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue