refactor(structure): normalize folder names with leading zeros for consistency
- Renamed all folders from format NEx (e.g., 1Ex, 2Ex...) to 0NEx (01Ex, 02Ex, etc.) - Updated subdirectories and files accordingly - Removed old main Makefile and tasks (a01.c–a09.c, solve.c, io_status.h), likely obsolete - Cleaned up deprecated task binaries and configs
This commit is contained in:
parent
2cf18a1ff3
commit
8a7aac7c23
385 changed files with 2 additions and 1468 deletions
19
2025.02.28/02Ex/Makefile
Normal file
19
2025.02.28/02Ex/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.o solve.o sort.o
|
||||
gcc main.o solve.o array.o sort.o -o a02.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
sort.o: sort.c
|
||||
gcc $(CFLAGS) -c sort.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
4
2025.02.28/02Ex/a.txt
Normal file
4
2025.02.28/02Ex/a.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
alpha
|
||||
beta
|
||||
gamma
|
||||
delta
|
73
2025.02.28/02Ex/array.c
Normal file
73
2025.02.28/02Ex/array.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
#include "array.h"
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *))
|
||||
{
|
||||
/* Каждый элемент больше следующего */
|
||||
int i; int count = 0;
|
||||
for (i = 1; i < n; i++)
|
||||
{
|
||||
if ((*cmp)(a[i-1], a[i]) > 0)
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
15
2025.02.28/02Ex/array.h
Normal file
15
2025.02.28/02Ex/array.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef ARRAY_H
|
||||
#define ARRAY_H
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
io_status read_array(char *a[], int n, const char *name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
5
2025.02.28/02Ex/b.txt
Normal file
5
2025.02.28/02Ex/b.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
alpha
|
||||
beta
|
||||
gamma
|
||||
delta
|
||||
epsilon
|
14
2025.02.28/02Ex/io_status.h
Normal file
14
2025.02.28/02Ex/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
|
120
2025.02.28/02Ex/main.c
Normal file
120
2025.02.28/02Ex/main.c
Normal file
|
@ -0,0 +1,120 @@
|
|||
#include "solve.h"
|
||||
#include "sort.h"
|
||||
#include "array.h"
|
||||
#include "io_status.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/* ./a02.out c n p_a filename_a m p_b filename_b */
|
||||
int c, p_a, n, p_b, m, diff, task = 2;
|
||||
io_status ret;
|
||||
char *name_a, *name_b, **arr_a, **arr_b, **arr_c;
|
||||
int (*cmp)(const char *, const char *);
|
||||
int (*f[])(const char *, const char *) = {up_strcmp, down_strcmp, up_len, down_len};
|
||||
int len_f = sizeof(f) / sizeof(f[0]);
|
||||
double t;
|
||||
|
||||
if (!(argc == 8 && sscanf(argv[1], "%d", &c) && sscanf(argv[2], "%d", &n) == 1 && sscanf(argv[3], "%d", &p_a) == 1 && sscanf(argv[5], "%d", &m) && sscanf(argv[6], "%d",&p_b) && c >= 1 && c <= len_f))
|
||||
{
|
||||
printf("Usage %s c n p_a filename_a m p_b filename_b\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name_a = argv[4];
|
||||
name_b = argv[7];
|
||||
cmp = f[c-1];
|
||||
|
||||
if (!(arr_a = (char **)malloc(n * sizeof(char *))))
|
||||
{
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
if (!(arr_b = (char **)malloc(m * sizeof(char *))))
|
||||
{
|
||||
free(arr_a);
|
||||
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
if (!(arr_c = (char **)malloc((n+m) * sizeof(char *))))
|
||||
{
|
||||
free(arr_a);
|
||||
free(arr_b);
|
||||
|
||||
printf("Not enough memory: \n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
ret = read_array(arr_a, n, 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;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory");
|
||||
break;
|
||||
}
|
||||
free(arr_a);
|
||||
free(arr_b);
|
||||
free(arr_c);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
ret = read_array(arr_b, m, 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;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory");
|
||||
break;
|
||||
}
|
||||
free_array(arr_a, n);
|
||||
|
||||
free(arr_a);
|
||||
free(arr_b);
|
||||
free(arr_c);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(arr_a, n, p_a);
|
||||
printf("\n");
|
||||
print_array(arr_b, m, p_b);
|
||||
|
||||
t = clock();
|
||||
t2_solve(arr_a, arr_b, arr_c, n, m, cmp);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
diff = check(arr_c, n+m, cmp);
|
||||
|
||||
printf("New array:\n");
|
||||
print_array(arr_c, n+m, p_a+p_b);
|
||||
printf("%s : Task = %d Diff = %d Elapsed = %.2f\n", argv[0], task, diff, t);
|
||||
|
||||
free_array(arr_a, n);
|
||||
free_array(arr_b, m);
|
||||
|
||||
free(arr_a);
|
||||
free(arr_b);
|
||||
free(arr_c);
|
||||
|
||||
return 0;
|
||||
}
|
24
2025.02.28/02Ex/solve.c
Normal file
24
2025.02.28/02Ex/solve.c
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include "solve.h"
|
||||
|
||||
#include "array.h"
|
||||
|
||||
void t2_solve(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++];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
9
2025.02.28/02Ex/solve.h
Normal file
9
2025.02.28/02Ex/solve.h
Normal file
|
@ -0,0 +1,9 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void t2_solve(char **arr_a, char **arr_b, char **arr_c, int n, int m, int (*cmp)(const char *, const char *));
|
||||
int check(char **a, int n, int (*cmp)(const char *, const char *));
|
||||
|
||||
#endif
|
23
2025.02.28/02Ex/sort.c
Normal file
23
2025.02.28/02Ex/sort.c
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include "sort.h"
|
||||
|
||||
int up_strcmp(const char *a, const char *b)
|
||||
{ return strcmp(a, b); }
|
||||
|
||||
int down_strcmp(const char *a, const char *b)
|
||||
{ return -strcmp(a, b); }
|
||||
|
||||
int up_len(const char *a, const char *b)
|
||||
{
|
||||
int i = 0;
|
||||
while (1)
|
||||
{
|
||||
if (a[i] == '\0' && b[i] == '\0') return strcmp(a, b);
|
||||
else if (a[i] == '\0') return -1;
|
||||
else if (b[i] == '\0') return 1;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
int down_len(const char *a, const char *b)
|
||||
{ return -up_len(a, b); }
|
||||
|
11
2025.02.28/02Ex/sort.h
Normal file
11
2025.02.28/02Ex/sort.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef SORT_H
|
||||
#define SORT_H
|
||||
|
||||
#include <string.h>
|
||||
|
||||
int up_strcmp(const char *a, const char *b);
|
||||
int down_strcmp(const char *a, const char *b);
|
||||
int up_len(const char *a, const char *b);
|
||||
int down_len(const char *a, const char *b);
|
||||
|
||||
#endif
|
19
2025.02.28/02Ex/test_cases.json
Normal file
19
2025.02.28/02Ex/test_cases.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"exe": "a02.exe",
|
||||
"filename_a": "a.txt",
|
||||
"filename_b": "b.txt",
|
||||
"tests": [
|
||||
{
|
||||
"c": 1,
|
||||
"a": "1 2 3 4\n5 6 7 8",
|
||||
"b": "9 10 11\n12 13 14",
|
||||
"expected": "1 2 3 4 5 6 7 8 9 10 11 12 13 14"
|
||||
},
|
||||
{
|
||||
"c": 2,
|
||||
"a": "10 20\n30 40 50",
|
||||
"b": "15 25 35\n45 55",
|
||||
"expected": "10 20 30 40 50 15 25 35 45 55"
|
||||
}
|
||||
]
|
||||
}
|
130
2025.02.28/02Ex/test_runner.py
Normal file
130
2025.02.28/02Ex/test_runner.py
Normal file
|
@ -0,0 +1,130 @@
|
|||
import json
|
||||
import subprocess
|
||||
import os
|
||||
import time
|
||||
import platform
|
||||
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
|
||||
|
||||
class TestCase:
|
||||
"""Represents a single test case"""
|
||||
def __init__(self, c, a, b, expected):
|
||||
self.c = c
|
||||
self.a = a
|
||||
self.b = b
|
||||
self.expected = expected
|
||||
|
||||
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_a = self.config["filename_a"]
|
||||
self.filename_b = self.config["filename_b"]
|
||||
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):
|
||||
"""Runs a shell command and handles errors"""
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, shell=True)
|
||||
return result
|
||||
|
||||
def count_lines(filename):
|
||||
"""Counts the number of non-empty lines in a file"""
|
||||
try:
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
return sum(1 for line in f if line.strip())
|
||||
except FileNotFoundError:
|
||||
print(color_text(f"[ERROR] File {filename} not found.", Fore.RED))
|
||||
return 0
|
||||
|
||||
def run_test(test_suite, test):
|
||||
"""Runs the program and checks its result"""
|
||||
exe = test_suite.exe
|
||||
filename_a = test_suite.filename_a
|
||||
filename_b = test_suite.filename_b
|
||||
|
||||
# Write input arrays to files
|
||||
with open(filename_a, "w", encoding="utf-8") as fa:
|
||||
fa.write(test.a)
|
||||
with open(filename_b, "w", encoding="utf-8") as fb:
|
||||
fb.write(test.b)
|
||||
|
||||
# Calculate n and m correctly
|
||||
n = count_lines(filename_a)
|
||||
m = count_lines(filename_b)
|
||||
pa = n
|
||||
pb = m
|
||||
|
||||
# Debug output to check file contents
|
||||
print(color_text(f"[DEBUG] a.txt content:\n{open(filename_a).read()}", Fore.CYAN))
|
||||
print(color_text(f"[DEBUG] b.txt content:\n{open(filename_b).read()}", Fore.CYAN))
|
||||
print(color_text(f"[DEBUG] Calculated n = {n}, m = {m}", Fore.CYAN))
|
||||
|
||||
# Ensure correct execution command on Windows
|
||||
exe_cmd = exe if platform.system() == "Windows" else f"./{exe}"
|
||||
|
||||
# Form command-line arguments as a list
|
||||
cmd = [exe_cmd, str(test.c), str(n), str(pa), filename_a, str(m), str(pb), filename_b]
|
||||
print(color_text(f"[DEBUG] Running command: {' '.join(cmd)}", Fore.CYAN))
|
||||
|
||||
# Run program
|
||||
result = run_command(cmd)
|
||||
if result.returncode != 0:
|
||||
print(color_text(f"[ERROR] Test failed to execute: {' '.join(cmd)}", Fore.RED))
|
||||
print(color_text(f"[EXIT CODE]: {result.returncode}", Fore.YELLOW))
|
||||
print(color_text(f"[STDOUT]: {result.stdout}", Fore.YELLOW))
|
||||
print(color_text(f"[STDERR]: {result.stderr}", Fore.YELLOW))
|
||||
return
|
||||
|
||||
# Extract relevant output
|
||||
output_lines = result.stdout.split("\n")
|
||||
try:
|
||||
start_index = output_lines.index("New array:") + 1
|
||||
end_index = next(i for i, line in enumerate(output_lines) if "Task" in line)
|
||||
output = "\n".join(output_lines[start_index:end_index])
|
||||
except (ValueError, IndexError):
|
||||
print(color_text(f"[FAIL] Could not parse output correctly.", Fore.RED))
|
||||
return
|
||||
|
||||
# Validate output
|
||||
if output == test.expected:
|
||||
print(color_text(f"[PASS] Test passed.", Fore.GREEN))
|
||||
else:
|
||||
print(color_text(f"[FAIL] Test failed.", Fore.RED))
|
||||
print(color_text(f"Expected:\n{test.expected}", Fore.YELLOW))
|
||||
print(color_text(f"Got:\n{output}", Fore.YELLOW))
|
||||
|
||||
# Cleanup files
|
||||
for file in [filename_a, filename_b]:
|
||||
try:
|
||||
os.remove(file)
|
||||
except FileNotFoundError:
|
||||
print(color_text(f"[WARNING] Could not delete {file}, may be locked.", Fore.MAGENTA))
|
||||
|
||||
def main():
|
||||
print(color_text("[CLEAN] Cleaning project...", Fore.MAGENTA))
|
||||
run_command(["make", "clean"])
|
||||
print(color_text("[BUILD] Compiling project...", Fore.MAGENTA))
|
||||
run_command(["make"])
|
||||
|
||||
test_suite = TestSuite("test_cases.json")
|
||||
for test in test_suite.tests:
|
||||
run_test(test_suite, test)
|
||||
|
||||
print(color_text("[CLEAN] Final cleanup...", Fore.MAGENTA))
|
||||
run_command(["make", "clean"])
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue