The Second and Third Tasks are done
This commit is contained in:
parent
136c9f6b00
commit
69ff8c30a1
31 changed files with 995 additions and 53 deletions
|
@ -1,47 +0,0 @@
|
|||
#include "solve.h"
|
||||
|
||||
#include "array.h"
|
||||
|
||||
void t8_solve(char **a, char **b, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
int i = 2, j, margin;
|
||||
do {
|
||||
int hf_i = i/2;
|
||||
for (j = 0; j < n; j += i)
|
||||
{
|
||||
if (n < j+i)
|
||||
{
|
||||
margin = n-(j+hf_i);
|
||||
if (margin <= 0) break;
|
||||
} else margin = hf_i;
|
||||
|
||||
unite(a+j, a+(j+hf_i), b, hf_i, margin, cmp);
|
||||
for (int k = 0; k < hf_i+margin; k++) a[i+j] = b[i];
|
||||
}
|
||||
|
||||
printf("\ni: %d:\n", i);
|
||||
print_array(a, n, n);
|
||||
i *= 2;
|
||||
} while (i < n);
|
||||
}
|
||||
|
||||
void unite(char **arr_a, char **arr_b, char **arr_c, int n, int m, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
int i, j, k, cntr, len = n + m; // cntr - contrast
|
||||
for (i = 0, j = 0, k = 0; k < len; ++k)
|
||||
{
|
||||
if (i >= n)
|
||||
{
|
||||
arr_c[k] = arr_b[j++];
|
||||
} else if (j >= m)
|
||||
{
|
||||
arr_c[k] = arr_a[i++];
|
||||
} else
|
||||
{
|
||||
cntr = cmp(arr_a[i], arr_b[j]);
|
||||
if (cntr <= 0) arr_c[k] = arr_a[i++];
|
||||
else arr_c[k] = arr_b[j++];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -55,6 +55,7 @@ int main(int argc, char *argv[])
|
|||
break;
|
||||
}
|
||||
free(a);
|
||||
free(b);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
1.000e+00 2.000e+00 3.000e+00 4.000e+00
|
||||
2.000e+00 3.000e+00 4.000e+00 5.000e+00
|
||||
3.000e+00 4.000e+00 5.000e+00 6.000e+00
|
||||
4.000e+00 5.000e+00 6.000e+00 7.000e+00
|
|
@ -7,7 +7,7 @@ int t1_solve(double *a, int n)
|
|||
{
|
||||
int i, j;
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = 0; j < n; j++)
|
||||
for (j = i; j < n; j++)
|
||||
if (i != j)
|
||||
if (fabs(a[i * n + j] - a[j * n + i]) > eps) return 0;
|
||||
return n;
|
||||
|
|
|
@ -41,6 +41,97 @@
|
|||
"n": 3,
|
||||
"p": 3,
|
||||
"expected_res": 3
|
||||
},
|
||||
{
|
||||
"name": "File Matrix (Asymmetric)",
|
||||
"k": 0,
|
||||
"n": 4,
|
||||
"p": 4,
|
||||
"matrix": "1 2 3 4\n1 2 3 4\n1 2 3 4\n1 2 3 4",
|
||||
"expected_res": 0
|
||||
},
|
||||
{
|
||||
"name": "File Matrix (Symmetric)",
|
||||
"k": 0,
|
||||
"n": 4,
|
||||
"p": 4,
|
||||
"matrix": "1 2 3 4\n2 3 4 5\n3 4 5 6\n4 5 6 7",
|
||||
"expected_res": 4
|
||||
},
|
||||
{
|
||||
"name": "File Matrix (Diagonal Symmetric)",
|
||||
"k": 0,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"matrix": "5 0 0\n0 5 0\n0 0 5",
|
||||
"expected_res": 3
|
||||
},
|
||||
{
|
||||
"name": "Generated Matrix (k=1, Symmetric)",
|
||||
"k": 1,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"expected_res": 3
|
||||
},
|
||||
{
|
||||
"name": "Generated Matrix (k=2, Symmetric)",
|
||||
"k": 2,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"expected_res": 3
|
||||
},
|
||||
{
|
||||
"name": "Generated Matrix (k=3, Symmetric)",
|
||||
"k": 3,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"expected_res": 3
|
||||
},
|
||||
{
|
||||
"name": "Generated Matrix (k=4, Symmetric)",
|
||||
"k": 4,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"expected_res": 3
|
||||
},
|
||||
{
|
||||
"name": "1x1 Matrix (Always Symmetric)",
|
||||
"k": 0,
|
||||
"n": 1,
|
||||
"p": 1,
|
||||
"matrix": "42",
|
||||
"expected_res": 1
|
||||
},
|
||||
{
|
||||
"name": "2x2 Symmetric Matrix",
|
||||
"k": 0,
|
||||
"n": 2,
|
||||
"p": 2,
|
||||
"matrix": "1 2\n2 3",
|
||||
"expected_res": 2
|
||||
},
|
||||
{
|
||||
"name": "2x2 Asymmetric Matrix",
|
||||
"k": 0,
|
||||
"n": 2,
|
||||
"p": 2,
|
||||
"matrix": "1 0\n2 3",
|
||||
"expected_res": 0
|
||||
},
|
||||
{
|
||||
"name": "4x4 Nearly Symmetric Matrix",
|
||||
"k": 0,
|
||||
"n": 4,
|
||||
"p": 4,
|
||||
"matrix": "1 2 3 4\n2 3 4 5\n3 4 5 6\n4 5 6 8",
|
||||
"expected_res": 4
|
||||
},
|
||||
{
|
||||
"name": "1000x1000 Large Symmetric Matrix",
|
||||
"k": 1,
|
||||
"n": 1000,
|
||||
"p": 5,
|
||||
"expected_res": 1000
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -144,6 +144,14 @@ def run_test(test_suite, test):
|
|||
# Run the program
|
||||
result = run_command(cmd)
|
||||
|
||||
if test.k == 0 and test.matrix:
|
||||
# Cleanup test files
|
||||
try:
|
||||
os.remove(filename)
|
||||
except (FileNotFoundError, PermissionError):
|
||||
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
||||
|
||||
|
||||
# Extract and format output matrix
|
||||
matrix_output = parse_matrix_output(result.stdout) if result else None
|
||||
|
||||
|
|
19
2025.03.07/2Ex/Makefile
Normal file
19
2025.03.07/2Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a02.exe: main.o array_io.o solve.o init_f.o
|
||||
gcc main.o solve.o array_io.o init_f.o -o a02.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array_io.o: array_io.c
|
||||
gcc $(CFLAGS) -c array_io.c
|
||||
|
||||
init_f.o: init_f.c
|
||||
gcc $(CFLAGS) -c init_f.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
39
2025.03.07/2Ex/array_io.c
Normal file
39
2025.03.07/2Ex/array_io.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include <stdio.h>
|
||||
#include "array_io.h"
|
||||
|
||||
io_status read_sq_matrix(double *a, int n, 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 < n; j++)
|
||||
if (fscanf(fp, "%lf", a + i * n + j) != 1)
|
||||
{fclose(fp); return ERROR_READ;}
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void print_sq_matrix(const double *a, int n, int p)
|
||||
{
|
||||
int np = (n > p ? p : n);
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < np; i++)
|
||||
{
|
||||
for (j = 0; j < np; j++)
|
||||
printf(" %10.3e", a[i * n + j]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void init_sq_matrix(double *a, int n, 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 < n; j++)
|
||||
a[i * n + j] = q(n, n, i+1, j+1);
|
||||
}
|
11
2025.03.07/2Ex/array_io.h
Normal file
11
2025.03.07/2Ex/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_sq_matrix(double *a, int n, const char *name);
|
||||
void print_sq_matrix(const double *a, int n, int p);
|
||||
void init_sq_matrix(double *a, int n, int k);
|
||||
|
||||
#endif
|
30
2025.03.07/2Ex/init_f.c
Normal file
30
2025.03.07/2Ex/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.07/2Ex/init_f.h
Normal file
9
2025.03.07/2Ex/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.07/2Ex/io_status.h
Normal file
14
2025.03.07/2Ex/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
|
64
2025.03.07/2Ex/main.c
Normal file
64
2025.03.07/2Ex/main.c
Normal file
|
@ -0,0 +1,64 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "array_io.h"
|
||||
#include "io_status.h"
|
||||
#include "solve.h"
|
||||
|
||||
/* ./a.out n p k [filename] */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
double t, res, *a;
|
||||
int n, p, k, task = 2;
|
||||
char *name = 0;
|
||||
|
||||
if (!((argc == 4 || argc == 5) &&
|
||||
sscanf(argv[1], "%d", &n) == 1 &&
|
||||
sscanf(argv[2], "%d", &p) == 1 &&
|
||||
sscanf(argv[3], "%d", &k) == 1 &&
|
||||
k >= 0 && k <= 4 && (!(k == 0 && argc != 5))))
|
||||
{
|
||||
printf("Usage: %s n p k [filename]\n", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
if (argc == 5) name = argv[4];
|
||||
|
||||
a = (double *)malloc(n * n * sizeof(double));
|
||||
if (!a)
|
||||
{
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (name)
|
||||
{ /* из файла */
|
||||
io_status ret;
|
||||
ret = read_sq_matrix(a, n, 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_sq_matrix(a, n, k);
|
||||
|
||||
printf("initial matrix:\n");
|
||||
print_sq_matrix(a, n, p);
|
||||
|
||||
t = clock();
|
||||
res = t2_solve(a, n);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("Result = %10.3e\n", res);
|
||||
printf("%s : Task = %d Elapsed = %.2f\n", argv[0], task, t);
|
||||
free(a);
|
||||
return 0;
|
||||
}
|
11
2025.03.07/2Ex/solve.c
Normal file
11
2025.03.07/2Ex/solve.c
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include "solve.h"
|
||||
#include "math.h"
|
||||
|
||||
double t2_solve(double *a, int n)
|
||||
{
|
||||
double trace = 0;
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
trace += a[i * n + i];
|
||||
return trace;
|
||||
}
|
6
2025.03.07/2Ex/solve.h
Normal file
6
2025.03.07/2Ex/solve.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
double t2_solve(double *a, int n);
|
||||
|
||||
#endif
|
4
2025.03.07/2Ex/t.txt
Normal file
4
2025.03.07/2Ex/t.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
1 2 3 4
|
||||
1 2 3 4
|
||||
1 2 3 4
|
||||
1 2 3 4
|
97
2025.03.07/2Ex/test_cases.json
Normal file
97
2025.03.07/2Ex/test_cases.json
Normal file
|
@ -0,0 +1,97 @@
|
|||
{
|
||||
"exe": "a02.exe",
|
||||
"filename": "matrix.txt",
|
||||
"tests": [
|
||||
{
|
||||
"name": "File Matrix (Basic Trace)",
|
||||
"k": 0,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"matrix": "1 2 3\n4 5 6\n7 8 9",
|
||||
"expected_res": 15
|
||||
},
|
||||
{
|
||||
"name": "File Matrix (Negative Numbers)",
|
||||
"k": 0,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"matrix": "-1 -2 -3\n-4 -5 -6\n-7 -8 -9",
|
||||
"expected_res": -15
|
||||
},
|
||||
{
|
||||
"name": "File Matrix (Mixed Positive and Negative)",
|
||||
"k": 0,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"matrix": "1 -2 3\n-4 5 -6\n7 -8 9",
|
||||
"expected_res": 15
|
||||
},
|
||||
{
|
||||
"name": "File Matrix (Floating Point Values)",
|
||||
"k": 0,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"matrix": "1.1 2.2 3.3\n4.4 5.5 6.6\n7.7 8.8 9.9",
|
||||
"expected_res": 16.5
|
||||
},
|
||||
{
|
||||
"name": "Generated Matrix (k=1)",
|
||||
"k": 1,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"expected_res": 6
|
||||
},
|
||||
{
|
||||
"name": "Generated Matrix (k=2)",
|
||||
"k": 2,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"expected_res": 6
|
||||
},
|
||||
{
|
||||
"name": "Generated Matrix (k=3)",
|
||||
"k": 3,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"expected_res": 0
|
||||
},
|
||||
{
|
||||
"name": "Generated Matrix (k=4, Fractional Values)",
|
||||
"k": 4,
|
||||
"n": 3,
|
||||
"p": 3,
|
||||
"expected_res": 1.533
|
||||
},
|
||||
{
|
||||
"name": "1x1 Matrix (Single Element)",
|
||||
"k": 0,
|
||||
"n": 1,
|
||||
"p": 1,
|
||||
"matrix": "42",
|
||||
"expected_res": 42
|
||||
},
|
||||
{
|
||||
"name": "2x2 Matrix",
|
||||
"k": 0,
|
||||
"n": 2,
|
||||
"p": 2,
|
||||
"matrix": "1 2\n3 4",
|
||||
"expected_res": 5
|
||||
},
|
||||
{
|
||||
"name": "4x4 Identity Matrix",
|
||||
"k": 0,
|
||||
"n": 4,
|
||||
"p": 4,
|
||||
"matrix": "1 0 0 0\n0 1 0 0\n0 0 1 0\n0 0 0 1",
|
||||
"expected_res": 4
|
||||
},
|
||||
{
|
||||
"name": "1000x1000 Large Matrix",
|
||||
"k": 1,
|
||||
"n": 1000,
|
||||
"p": 5,
|
||||
"expected_res": 500500
|
||||
}
|
||||
]
|
||||
}
|
189
2025.03.07/2Ex/test_runner.py
Normal file
189
2025.03.07/2Ex/test_runner.py
Normal file
|
@ -0,0 +1,189 @@
|
|||
import json
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
import platform
|
||||
import re
|
||||
import signal
|
||||
from colorama import Fore, Style, init
|
||||
|
||||
# Enable color support in Windows
|
||||
init(autoreset=True)
|
||||
|
||||
def color_text(text, color):
|
||||
"""Returns colored text"""
|
||||
return color + text + Style.RESET_ALL
|
||||
|
||||
def cleanup_and_exit():
|
||||
"""Handles cleanup on Ctrl+C or forced exit"""
|
||||
print(color_text("\n[ABORT] Operation interrupted. Cleaning up...", Fore.RED))
|
||||
run_command("make clean")
|
||||
exit(1)
|
||||
|
||||
# Register Ctrl+C handler
|
||||
signal.signal(signal.SIGINT, lambda sig, frame: cleanup_and_exit())
|
||||
|
||||
class TestCase:
|
||||
"""Represents a single test case"""
|
||||
def __init__(self, k, matrix=None, n=None, p=None, expected_res=0, debug=False, name=None):
|
||||
self.k = k
|
||||
self.matrix = matrix
|
||||
self.n = n
|
||||
self.p = p
|
||||
self.expected_res = expected_res
|
||||
self.debug = debug
|
||||
self.name = name if name else f"Test k={k}, n={n if n else 'auto'}, p={p if p else 'auto'}"
|
||||
|
||||
# Compute `n` if missing and `k == 0`
|
||||
if self.k == 0 and not self.n and self.matrix:
|
||||
self.n = len(self.matrix.strip().split("\n"))
|
||||
|
||||
# Compute `p`
|
||||
self.p = self.p if self.p else self.n
|
||||
|
||||
def validate_inputs(self):
|
||||
"""Ensures input values are valid"""
|
||||
if self.k < 0 or (self.k == 0 and not self.matrix):
|
||||
print(color_text(f"[ERROR] Invalid test parameters: {self.name}", Fore.RED))
|
||||
return False
|
||||
return True
|
||||
|
||||
class TestSuite:
|
||||
"""Handles loading and running test cases"""
|
||||
def __init__(self, config_file):
|
||||
self.config = self.load_config(config_file)
|
||||
self.exe = self.config["exe"]
|
||||
self.filename = self.config["filename"]
|
||||
self.tests = [TestCase(**test) for test in self.config["tests"]]
|
||||
|
||||
@staticmethod
|
||||
def load_config(filename):
|
||||
"""Loads test cases from JSON"""
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
def run_command(cmd, exit_on_error=False):
|
||||
"""Runs a shell command and handles errors"""
|
||||
try:
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||
return result
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(color_text(f"[ERROR] Command failed: {cmd}", Fore.RED))
|
||||
print(e.stderr)
|
||||
if exit_on_error:
|
||||
exit(1)
|
||||
return None
|
||||
|
||||
def wait_for_executable(exe):
|
||||
"""Waits for the executable file to appear after compilation"""
|
||||
print(color_text(f"[WAIT] Waiting for {exe} to be compiled...", Fore.YELLOW))
|
||||
while not os.path.exists(exe):
|
||||
time.sleep(0.1) # Reduce CPU usage
|
||||
print(color_text(f"[READY] {exe} compiled successfully.", Fore.GREEN))
|
||||
|
||||
def format_matrix(matrix):
|
||||
"""Formats a matrix to match `printf("%10.3e")` output"""
|
||||
formatted = []
|
||||
for row in matrix.strip().split("\n"):
|
||||
formatted.append(" ".join(f"{float(num):10.3e}" for num in row.split()))
|
||||
return "\n".join(formatted)
|
||||
|
||||
def parse_matrix_output(output):
|
||||
"""Extracts and formats matrix from program output"""
|
||||
parts = output.split("initial matrix:\n")[1].split("Result")
|
||||
if len(parts) > 1:
|
||||
matrix = "\n".join(parts[0].strip().split("\n")) # Remove last line with "Result = ..."
|
||||
return format_matrix(matrix)
|
||||
return ""
|
||||
|
||||
def check_result(output, expected_res):
|
||||
"""Checks if Result matches expected value"""
|
||||
match = re.search(r"Result\s*=\s*(-?\d+\.\d{3}e[+-]?\d+)", output)
|
||||
if match:
|
||||
result_value = float(match.group(1))
|
||||
if result_value != expected_res:
|
||||
print(color_text(f"[FAIL] Test failed: Result = {result_value} (expected {expected_res})", Fore.RED))
|
||||
return False
|
||||
return True
|
||||
|
||||
def generate_expected_matrix(n, p, k):
|
||||
"""Generates expected matrix based on k"""
|
||||
def f(k, i, j):
|
||||
if k == 1:
|
||||
return max(n, n) - max(i, j) + 1
|
||||
elif k == 2:
|
||||
return max(i, j)
|
||||
elif k == 3:
|
||||
return abs(i - j)
|
||||
elif k == 4:
|
||||
return 1.0 / (i + j - 1) if (i + j - 1) != 0 else 0
|
||||
return 0
|
||||
|
||||
matrix = []
|
||||
for i in range(min(n, p)):
|
||||
row = [f(k, i + 1, j + 1) for j in range(min(n, p))]
|
||||
matrix.append(" ".join(f"{num:10.3e}" for num in row))
|
||||
return "\n".join(matrix)
|
||||
|
||||
def run_test(test_suite, test):
|
||||
"""Runs the program and checks its result"""
|
||||
if not test.validate_inputs():
|
||||
return
|
||||
|
||||
exe, filename = test_suite.exe, test_suite.filename
|
||||
|
||||
# If matrix is given, write it to the file
|
||||
if test.k == 0 and test.matrix:
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
f.write(format_matrix(test.matrix.strip()) + "\n")
|
||||
|
||||
cmd = [exe, str(test.n), str(test.p), str(test.k)]
|
||||
if test.k == 0:
|
||||
cmd.append(filename)
|
||||
|
||||
# Run the program
|
||||
result = run_command(cmd)
|
||||
|
||||
if test.k == 0 and test.matrix:
|
||||
# Cleanup test files
|
||||
try:
|
||||
os.remove(filename)
|
||||
except (FileNotFoundError, PermissionError):
|
||||
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
||||
|
||||
# Extract and format output matrix
|
||||
matrix_output = parse_matrix_output(result.stdout) if result else None
|
||||
|
||||
# Generate expected matrix
|
||||
expected_matrix = format_matrix(test.matrix) if test.k == 0 else generate_expected_matrix(test.n, test.p, test.k)
|
||||
|
||||
if matrix_output.strip() != expected_matrix.strip():
|
||||
print(color_text(f"[FAIL] Test '{test.name}' matrix mismatch.", Fore.RED))
|
||||
print(f"Expected:\n{expected_matrix}")
|
||||
print(f"Got:\n{matrix_output}")
|
||||
return
|
||||
|
||||
# Check Result
|
||||
if not check_result(result.stdout, test.expected_res):
|
||||
return
|
||||
|
||||
print(color_text(f"[PASS] Test '{test.name}' passed.", Fore.GREEN))
|
||||
|
||||
def main():
|
||||
print(color_text("[CLEAN] Cleaning project...", Fore.CYAN))
|
||||
run_command("make clean", exit_on_error=True)
|
||||
|
||||
print(color_text("[BUILD] Compiling project...", Fore.CYAN))
|
||||
run_command("make", exit_on_error=True)
|
||||
|
||||
test_suite = TestSuite("test_cases.json")
|
||||
wait_for_executable(test_suite.exe)
|
||||
|
||||
for test in test_suite.tests:
|
||||
run_test(test_suite, test)
|
||||
|
||||
print(color_text("[CLEAN] Final cleanup...", Fore.CYAN))
|
||||
run_command("make clean")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
19
2025.03.07/3Ex/Makefile
Normal file
19
2025.03.07/3Ex/Makefile
Normal file
|
@ -0,0 +1,19 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a03.exe: main.o array_io.o solve.o init_f.o
|
||||
gcc main.o solve.o array_io.o init_f.o -o a03.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array_io.o: array_io.c
|
||||
gcc $(CFLAGS) -c array_io.c
|
||||
|
||||
init_f.o: init_f.c
|
||||
gcc $(CFLAGS) -c init_f.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
39
2025.03.07/3Ex/array_io.c
Normal file
39
2025.03.07/3Ex/array_io.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include <stdio.h>
|
||||
#include "array_io.h"
|
||||
|
||||
io_status read_sq_matrix(double *a, int n, 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 < n; j++)
|
||||
if (fscanf(fp, "%lf", a + i * n + j) != 1)
|
||||
{fclose(fp); return ERROR_READ;}
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void print_sq_matrix(const double *a, int n, int p)
|
||||
{
|
||||
int np = (n > p ? p : n);
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < np; i++)
|
||||
{
|
||||
for (j = 0; j < np; j++)
|
||||
printf(" %10.3e", a[i * n + j]);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void init_sq_matrix(double *a, int n, 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 < n; j++)
|
||||
a[i * n + j] = q(n, n, i+1, j+1);
|
||||
}
|
11
2025.03.07/3Ex/array_io.h
Normal file
11
2025.03.07/3Ex/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_sq_matrix(double *a, int n, const char *name);
|
||||
void print_sq_matrix(const double *a, int n, int p);
|
||||
void init_sq_matrix(double *a, int n, int k);
|
||||
|
||||
#endif
|
30
2025.03.07/3Ex/init_f.c
Normal file
30
2025.03.07/3Ex/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.07/3Ex/init_f.h
Normal file
9
2025.03.07/3Ex/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.07/3Ex/io_status.h
Normal file
14
2025.03.07/3Ex/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
|
65
2025.03.07/3Ex/main.c
Normal file
65
2025.03.07/3Ex/main.c
Normal file
|
@ -0,0 +1,65 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "array_io.h"
|
||||
#include "io_status.h"
|
||||
#include "solve.h"
|
||||
|
||||
/* ./a.out n p k [filename] */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
double t, res, *a;
|
||||
int n, p, k, task = 3;
|
||||
char *name = 0;
|
||||
|
||||
if (!((argc == 4 || argc == 5) &&
|
||||
sscanf(argv[1], "%d", &n) == 1 &&
|
||||
sscanf(argv[2], "%d", &p) == 1 &&
|
||||
sscanf(argv[3], "%d", &k) == 1 &&
|
||||
k >= 0 && k <= 4 && (!(k == 0 && argc != 5))))
|
||||
{
|
||||
printf("Usage: %s n p k [filename]\n", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
if (argc == 5) name = argv[4];
|
||||
|
||||
a = (double *)malloc(n * n * sizeof(double));
|
||||
if (!a)
|
||||
{
|
||||
printf("Not enough memory\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (name)
|
||||
{ /* из файла */
|
||||
io_status ret;
|
||||
ret = read_sq_matrix(a, n, 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_sq_matrix(a, n, k);
|
||||
|
||||
printf("Initial matrix:\n");
|
||||
print_sq_matrix(a, n, p);
|
||||
|
||||
t = clock();
|
||||
t3_solve(a, n);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("Result matrix:\n");
|
||||
print_sq_matrix(a, n, p);
|
||||
printf("%s : Task = %d Elapsed = %.2f\n", argv[0], task, t);
|
||||
free(a);
|
||||
return 0;
|
||||
}
|
4
2025.03.07/3Ex/matrix.txt
Normal file
4
2025.03.07/3Ex/matrix.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
1.000e+00 2.000e+00 3.000e+00 4.000e+00
|
||||
1.000e+00 2.000e+00 3.000e+00 4.000e+00
|
||||
1.000e+00 2.000e+00 3.000e+00 4.000e+00
|
||||
1.000e+00 2.000e+00 3.000e+00 4.000e+00
|
15
2025.03.07/3Ex/solve.c
Normal file
15
2025.03.07/3Ex/solve.c
Normal file
|
@ -0,0 +1,15 @@
|
|||
#include "solve.h"
|
||||
#include "math.h"
|
||||
|
||||
void t3_solve(double *a, int n)
|
||||
{
|
||||
double temp;
|
||||
int i, j;
|
||||
for (i = 0; i < n; i++)
|
||||
for (j = i; j < n; j++)
|
||||
if (i != j) {
|
||||
temp = a[i * n + j];
|
||||
a[i * n + j] = a[j * n + i];
|
||||
a[j * n + i] = temp;
|
||||
}
|
||||
}
|
6
2025.03.07/3Ex/solve.h
Normal file
6
2025.03.07/3Ex/solve.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
void t3_solve(double *a, int n);
|
||||
|
||||
#endif
|
4
2025.03.07/3Ex/t.txt
Normal file
4
2025.03.07/3Ex/t.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
1 2 3 4
|
||||
1 2 3 4
|
||||
1 2 3 4
|
||||
1 2 3 4
|
23
2025.03.07/3Ex/test_cases.json
Normal file
23
2025.03.07/3Ex/test_cases.json
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"exe": "a03.exe",
|
||||
"filename": "matrix.txt",
|
||||
"tests": [
|
||||
{
|
||||
"name": "File Matrix Test",
|
||||
"k": 0,
|
||||
"matrix": "1 2 3 4\n1 2 3 4\n1 2 3 4\n1 2 3 4"
|
||||
},
|
||||
{
|
||||
"name": "Generated Matrix (k=1)",
|
||||
"k": 1,
|
||||
"n": 3,
|
||||
"p": 3
|
||||
},
|
||||
{
|
||||
"name": "Generated Matrix (k=3)",
|
||||
"k": 3,
|
||||
"n": 3,
|
||||
"p": 3
|
||||
}
|
||||
]
|
||||
}
|
161
2025.03.07/3Ex/test_runner.py
Normal file
161
2025.03.07/3Ex/test_runner.py
Normal file
|
@ -0,0 +1,161 @@
|
|||
import json
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
import platform
|
||||
import re
|
||||
import signal
|
||||
from colorama import Fore, Style, init
|
||||
|
||||
# Enable color support in Windows
|
||||
init(autoreset=True)
|
||||
|
||||
def color_text(text, color):
|
||||
"""Returns colored text"""
|
||||
return color + text + Style.RESET_ALL
|
||||
|
||||
def cleanup_and_exit():
|
||||
"""Handles cleanup on Ctrl+C or forced exit"""
|
||||
print(color_text("\n[ABORT] Operation interrupted. Cleaning up...", Fore.RED))
|
||||
run_command("make clean")
|
||||
exit(1)
|
||||
|
||||
# Register Ctrl+C handler
|
||||
signal.signal(signal.SIGINT, lambda sig, frame: cleanup_and_exit())
|
||||
|
||||
class TestCase:
|
||||
"""Represents a single test case"""
|
||||
def __init__(self, k, matrix=None, n=None, p=None, debug=False, name=None):
|
||||
self.k = k
|
||||
self.matrix = matrix
|
||||
self.n = n
|
||||
self.p = p
|
||||
self.debug = debug
|
||||
self.name = name if name else f"Test k={k}, n={n if n else 'auto'}, p={p if p else 'auto'}"
|
||||
|
||||
# Compute `n` if missing and `k == 0`
|
||||
if self.k == 0 and not self.n and self.matrix:
|
||||
self.n = len(self.matrix.strip().split("\n"))
|
||||
|
||||
# Compute `p`
|
||||
self.p = self.p if self.p else self.n
|
||||
|
||||
def validate_inputs(self):
|
||||
"""Ensures input values are valid"""
|
||||
if self.k < 0 or (self.k == 0 and not self.matrix):
|
||||
print(color_text(f"[ERROR] Invalid test parameters: {self.name}", Fore.RED))
|
||||
return False
|
||||
return True
|
||||
|
||||
class TestSuite:
|
||||
"""Handles loading and running test cases"""
|
||||
def __init__(self, config_file):
|
||||
self.config = self.load_config(config_file)
|
||||
self.exe = self.config["exe"]
|
||||
self.filename = self.config["filename"]
|
||||
self.tests = [TestCase(**test) for test in self.config["tests"]]
|
||||
|
||||
@staticmethod
|
||||
def load_config(filename):
|
||||
"""Loads test cases from JSON"""
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
def run_command(cmd, exit_on_error=False):
|
||||
"""Runs a shell command and handles errors"""
|
||||
try:
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||
return result
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(color_text(f"[ERROR] Command failed: {cmd}", Fore.RED))
|
||||
print(e.stderr)
|
||||
if exit_on_error:
|
||||
exit(1)
|
||||
return None
|
||||
|
||||
def wait_for_executable(exe):
|
||||
"""Waits for the executable file to appear after compilation"""
|
||||
print(color_text(f"[WAIT] Waiting for {exe} to be compiled...", Fore.YELLOW))
|
||||
while not os.path.exists(exe):
|
||||
time.sleep(0.1) # Reduce CPU usage
|
||||
print(color_text(f"[READY] {exe} compiled successfully.", Fore.GREEN))
|
||||
|
||||
def format_matrix(matrix):
|
||||
"""Formats a matrix to match `printf("%10.3e")` output"""
|
||||
formatted = []
|
||||
for row in matrix.strip().split("\n"):
|
||||
formatted.append(" ".join(f"{float(num):10.3e}" for num in row.split()))
|
||||
return "\n".join(formatted)
|
||||
|
||||
def parse_matrix_output(output, label):
|
||||
"""Extracts and formats a labeled matrix from program output"""
|
||||
parts = output.split(f"{label}:\n")
|
||||
if len(parts) > 1:
|
||||
matrix_lines = parts[1].strip().split("\n")
|
||||
return format_matrix("\n".join(matrix_lines[:-1])) # Убираем Task = ... строку
|
||||
return ""
|
||||
|
||||
def transpose_matrix(matrix):
|
||||
"""Transposes a matrix given as a formatted string"""
|
||||
rows = matrix.strip().split("\n")
|
||||
transposed = zip(*[row.split() for row in rows])
|
||||
return "\n".join(" ".join(f"{float(num):10.3e}" for num in row) for row in transposed)
|
||||
|
||||
def run_test(test_suite, test):
|
||||
"""Runs the program and checks its result"""
|
||||
if not test.validate_inputs():
|
||||
return
|
||||
|
||||
exe, filename = test_suite.exe, test_suite.filename
|
||||
|
||||
# If matrix is given, write it to the file
|
||||
if test.k == 0 and test.matrix:
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
f.write(format_matrix(test.matrix.strip()) + "\n")
|
||||
|
||||
cmd = [exe, str(test.n), str(test.p), str(test.k)]
|
||||
if test.k == 0:
|
||||
cmd.append(filename)
|
||||
|
||||
# Run the program
|
||||
result = run_command(cmd)
|
||||
|
||||
# Extract both initial and result matrices
|
||||
initial_matrix = parse_matrix_output(result.stdout, "Initial matrix")
|
||||
result_matrix = parse_matrix_output(result.stdout, "Result matrix")
|
||||
|
||||
# Compute expected transposed matrix
|
||||
expected_transposed = transpose_matrix(initial_matrix)
|
||||
|
||||
if result_matrix.strip() != expected_transposed.strip():
|
||||
print(color_text(f"[FAIL] Test '{test.name}' matrix mismatch.", Fore.RED))
|
||||
print(f"Expected:\n{expected_transposed}")
|
||||
print(f"Got:\n{result_matrix}")
|
||||
return
|
||||
|
||||
print(color_text(f"[PASS] Test '{test.name}' passed.", Fore.GREEN))
|
||||
|
||||
# Cleanup test file
|
||||
try:
|
||||
os.remove(filename)
|
||||
except (FileNotFoundError, PermissionError):
|
||||
print(color_text(f"[WARNING] Could not delete {filename}, Windows may be locking it.", Fore.RED))
|
||||
|
||||
def main():
|
||||
print(color_text("[CLEAN] Cleaning project...", Fore.BLUE))
|
||||
run_command("make clean", exit_on_error=True)
|
||||
|
||||
print(color_text("[BUILD] Compiling project...", Fore.BLUE))
|
||||
run_command("make", exit_on_error=True)
|
||||
|
||||
test_suite = TestSuite("test_cases.json")
|
||||
wait_for_executable(test_suite.exe)
|
||||
|
||||
for test in test_suite.tests:
|
||||
run_test(test_suite, test)
|
||||
|
||||
print(color_text("[CLEAN] Final cleanup...", Fore.BLUE))
|
||||
run_command("make clean")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue