Added last three homeworks

This commit is contained in:
AZEN-SGG 2025-03-02 14:38:34 +03:00
parent f7b2367bc4
commit 0e3d948c9f
363 changed files with 18214 additions and 0 deletions

BIN
2025.02.14/8Ex/Linux.rar Normal file

Binary file not shown.

View file

@ -0,0 +1,38 @@
CFLAGS = -mfpmath=sse \
-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 \
-D_DEBUG -g \
-c
all: main.o array.o
gcc main.o array.o -o a08.out && rm *.o
main.o: main.c
gcc $(CFLAGS) main.c
array.o: array.c
gcc $(CFLAGS) array.c

View file

@ -0,0 +1,115 @@
#include "array.h"
io_status process_s(const char* s, char* s1, char* s2, const size_t len_s)
{
int i, j;
for (i = 0, j = 0; i < (int)len_s; ++i)
{
if (s[i] == '\\')
{
if (i + 1 == (int)len_s) return ERROR_PATTERN;
else
{
s1[j] = '0';
s2[j++] = s[++i];
}
}
else if ((s[i] == '+') && (j != 0))
{
s1[j - 1] = '1';
}
else
{
s1[j] = '0';
s2[j++] = s[i];
}
}
s2[j] = '\0';
return SUCCESS;
}
io_status process_file(const char* f_in, const char* f_out, const char* s1, const char* s2, int* r)
{
FILE* fp = fopen(f_in, "r");
if (!fp) return ERROR_OPEN;
else
{
char buf[LEN] = {0};
size_t len_s = strlen(s2);
FILE* fw = fopen(f_out, "w");
int i;
if (!fw)
{
fclose(fp);
return ERROR_OPEN;
}
for (i = 0; fgets(buf, sizeof(buf), fp);)
{
size_t len_buf = strlen(buf);
bool is_approach;
if (buf[len_buf - 1] == '\n') buf[--len_buf] = '\0';
is_approach = start(buf, s1, s2, len_s, len_buf);
if (is_approach)
{
i++;
if (buf[len_buf - 1] == '\n') buf[len_buf - 1] = '\0';
fprintf(fw, "%s\n", buf);
}
}
*r = i;
fclose(fw);
}
fclose(fp);
return SUCCESS;
}
bool start(const char* buf, const char* s1, const char* s2, const size_t len_s, const size_t len_buf)
{
size_t char_s = sizeof(char);
if (buf[0] == s2[0])
{
if (s1[0] == '1')
{
if (recursion(buf + char_s, s1, s2, len_s, len_buf - 1)) return true;
}
if (recursion(buf + char_s, s1 + char_s, s2 + char_s, len_s - 1, len_buf - 1)) return true;
}
return false;
}
bool recursion(const char* buf, const char* s1, const char* s2, const size_t len_s, const size_t len_buf)
{
size_t char_s = sizeof(char);
if (len_s == 0) return true;
else if (len_buf == 0) return false;
else
{
int i;
for (i = 0; i < (int)len_buf; ++i)
{
if (buf[i] == s2[i])
{
if (s1[i] == '1')
{
if (recursion(buf + (i + 1) * char_s, s1, s2, len_s, len_buf - 1)) return true;
}
if (i + 1 == (int)len_s) return true;
}
else
{
return false;
}
}
return false;
}
}

View file

@ -0,0 +1,14 @@
#ifndef ARRAY_H
#define ARRAY_H
#include "io_status.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
io_status process_s(const char * s, char * s1, char * s2, const size_t len_s);
io_status process_file(const char* f_in, const char* f_out, const char* s1, const char* s2, int* r);
bool start(const char* buf, const char* s1, const char* s2, const size_t len_s, const size_t len_buf);
bool recursion(const char* buf, const char* s1, const char* s2, const size_t len_s, const size_t len_buf);
#endif

View 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,
ERROR_PATTERN,
} io_status;
#endif

View file

@ -0,0 +1,67 @@
#include <stdio.h>
#include <time.h>
#include "io_status.h"
#include "array.h"
io_status task8(const char* f_in, const char* f_out, const char* s, int* res);
io_status task8(const char* f_in, const char* f_out, const char* s, int* res)
{
if (s == NULL) return ERROR_PATTERN;
else
{
size_t len_s = strlen(s);
io_status status;
char s1[len_s + 1]; // т.к. требуется место для \0
char s2[len_s + 1];
memset(s1, 0, len_s + 1);
memset(s2, 0, len_s + 1);
status = process_s(s, s1, s2, len_s);
if (status != SUCCESS) return status;
else
{
return process_file(f_in, f_out, s1, s2, res);
}
}
}
int main(int argc, char* argv[])
{
int task = 8;
io_status ret;
const char* f_in = 0;
const char* f_out = 0;
const char* s = 0;
int res = 0;
double t;
if (argc != 4)
{
printf("Usage: %s <f_in> <f_out> <s>\n", argv[0]);
return 1;
}
f_in = argv[1];
f_out = argv[2];
s = argv[3];
t = clock();
ret = task8(f_in, f_out, s, &res);
t = (clock() - t) / CLOCKS_PER_SEC;
switch (ret)
{
case SUCCESS:
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
argv[0], task, res, t);
break;
case ERROR_OPEN:
printf("Can not open %s\n", f_out);
return 1;
case ERROR_READ:
printf("Can not read %s\n", f_out);
return 2;
case ERROR_PATTERN:
printf("Error in pattern %s\n", s);
return 3;
}
return 0;
}

View file

@ -0,0 +1 @@
haaaaaaaaha

13
2025.02.14/8Ex/Makefile Normal file
View file

@ -0,0 +1,13 @@
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
a08.exe: main.o solve.o
gcc main.o solve.o -o a08.exe -lssp
main.o: main.c
gcc $(CFLAGS) -c main.c
solve.o: solve.c
gcc $(FLAGS) -c solve.c
clean:
del *.o *.exe

View 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,
ERROR_PATTERN,
} io_status;
#endif

67
2025.02.14/8Ex/main.c Normal file
View file

@ -0,0 +1,67 @@
#include <stdio.h>
#include <time.h>
#include "io_status.h"
#include "solve.h"
io_status task8(const char* f_in, const char* f_out, const char* s, int* res);
io_status task8(const char* f_in, const char* f_out, const char* s, int* res)
{
if (s == NULL) return ERROR_PATTERN;
else
{
size_t len_s = strlen(s);
io_status status;
char s1[len_s + 1]; // т.к. требуется место для \0
char s2[len_s + 1];
memset(s1, 0, len_s + 1);
memset(s2, 0, len_s + 1);
status = t8_process_s(s, s1, s2, len_s);
if (status != SUCCESS) return status;
else
{
return t8_process_file(f_in, f_out, s1, s2, res);
}
}
}
int main(int argc, char* argv[])
{
int task = 8;
io_status ret;
const char* f_in = 0;
const char* f_out = 0;
const char* s = 0;
int res = 0;
double t;
if (argc != 4)
{
printf("Usage: %s <f_in> <f_out> <s>\n", argv[0]);
return 1;
}
f_in = argv[1];
f_out = argv[2];
s = argv[3];
t = clock();
ret = task8(f_in, f_out, s, &res);
t = (clock() - t) / CLOCKS_PER_SEC;
switch (ret)
{
case SUCCESS:
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
argv[0], task, res, t);
break;
case ERROR_OPEN:
printf("Can not open %s\n", f_out);
return 1;
case ERROR_READ:
printf("Can not read %s\n", f_out);
return 2;
case ERROR_PATTERN:
printf("Error in pattern %s\n", s);
return 3;
}
return 0;
}

84
2025.02.14/8Ex/solve.c Normal file
View file

@ -0,0 +1,84 @@
#include "solve.h"
io_status t8_process_s(const char* s, char* s1, char* s2, const size_t len_s)
{
int i, j;
for (i = 0, j = 0; i < (int)len_s; ++i)
{
if (s[i] == '\\')
{
if (i + 1 == (int)len_s) return ERROR_PATTERN;
else
{
s1[j] = '0';
s2[j++] = s[++i];
}
}
else if ((s[i] == '+') && (j != 0))
{
s1[j - 1] = '1';
}
else
{
s1[j] = '0';
s2[j++] = s[i];
}
}
s2[j] = '\0';
return SUCCESS;
}
io_status t8_process_file(const char* f_in, const char* f_out, const char* s1, const char* s2, int* r)
{
FILE* fp = fopen(f_in, "r");
if (!fp) return ERROR_OPEN;
else
{
char buf[LEN] = {0};
size_t len_s = strlen(s2);
FILE* fw = fopen(f_out, "w");
int i;
if (!fw)
{
fclose(fp);
return ERROR_OPEN;
}
for (i = 0; fgets(buf, sizeof(buf), fp);)
{
size_t len_buf = strlen(buf);
bool is_approach;
if (buf[len_buf - 1] == '\n') buf[--len_buf] = '\0';
is_approach = t8_recursion(buf, s1, s2, len_s, len_buf);
if (is_approach)
{
i++;
fprintf(fw, "%s\n", buf);
}
}
*r = i;
fclose(fw);
}
fclose(fp);
return SUCCESS;
}
bool t8_recursion(const char* buf, const char* s1, const char* s2, const size_t len_s, const size_t len_buf)
{
if (len_s == 0) return (len_buf == 0);
else if (len_buf == 0) return false;
if (buf[0] == s2[0])
{
if (s1[0] == '1') if (t8_recursion(buf + 1, s1, s2, len_s, len_buf - 1)) return true;
if (t8_recursion(buf + 1, s1 + 1, s2 + 1, len_s - 1, len_buf - 1)) return true;
}
return false;
}

13
2025.02.14/8Ex/solve.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef ARRAY_H
#define ARRAY_H
#include "io_status.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
io_status t8_process_s(const char * s, char * s1, char * s2, const size_t len_s);
io_status t8_process_file(const char* f_in, const char* f_out, const char* s1, const char* s2, int* r);
bool t8_recursion(const char* buf, const char* s1, const char* s2, const size_t len_s, const size_t len_buf);
#endif

6
2025.02.14/8Ex/t.txt Normal file
View file

@ -0,0 +1,6 @@
aha
a
a+
a++
aaaaaaaa

View file

@ -0,0 +1,212 @@
{
"exe": "a08.exe",
"f_in": "input.txt",
"f_out": "output.txt",
"tests": [
{
"s": "some",
"text": "some\nnotsome\nrandom",
"expected": "some\n"
},
{
"s": "so+me",
"text": "some\nsooooome\nI'm sometimes",
"expected": "some\nsooooome\n"
},
{
"s": "s\\ome\\\\",
"text": "some\\",
"expected": "some\\\n"
},
{
"s": "some\\\\\\",
"text": "some\\",
"expected": "fall"
},
{
"s": "+oho",
"text": "+oho\noho",
"expected": "+oho\n"
},
{
"s": "a+",
"text": "\naha\na\na+\na++\naaaaaaaa\naa\naaaa\nbbb",
"expected": "a\naaaaaaaa\naa\naaaa\n"
},
{
"s": "abs",
"text": "abs\n\\abs\nabsababa\nahahabs\nahahahabsahhaha\n^abs\nahahahabs$jajaja\nha\\abss$hello what!\npbs\nubs\n.abs\nabs\npbt\natn.b.\nbs\nbsaha\n?abs\naaaaaaaaaaaaabs\n+abs+\nabsssssss\n\n",
"expected": "abs\nabs\n"
},
{
"s": "a+++++",
"text": "a\naaa\n\n",
"expected": "a\naaa\n"
},
{
"s": "a\\+++++",
"text": "a\naaa\na+\na++",
"expected": "a+\na++\n"
},
{
"s": "a+",
"text": "a\naa\naaa\naaaaaa\nb\naab\naaaaaax\n",
"expected": "a\naa\naaa\naaaaaa\n"
},
{
"s": "a+b",
"text": "ab\naab\naaaab\nacb",
"expected": "ab\naab\naaaab\n"
},
{
"s": "a++b",
"text": "ab\naab\naaaab",
"expected": "ab\naab\naaaab\n"
},
{
"s": "a+++b",
"text": "ab\naab\naaaab",
"expected": "ab\naab\naaaab\n"
},
{
"s": "+abc",
"text": "+abc\nabc\nxabc",
"expected": "+abc\n"
},
{
"s": "abc+",
"text": "abc\nabcc\nabcccc\nab",
"expected": "abc\nabcc\nabcccc\n"
},
{
"s": "a+b+c",
"text": "abc\naabbcc\naaaabbc",
"expected": "abc\naaaabbc\n"
},
{
"s": "a+b+c",
"text": "ac\nabbc",
"expected": "abbc\n"
},
{
"s": "a+b+c+",
"text": "abc\nabbcc\naaaabccccc",
"expected": "abc\nabbcc\naaaabccccc\n"
},
{
"s": "a+b+c+",
"text": "ac\nabb\n",
"expected": ""
},
{
"s": "a+b+c+d+",
"text": "abcd\naabbdcddd\naaaabbccddddd",
"expected": "abcd\naaaabbccddddd\n"
},
{
"s": "hello+world",
"text": "helloworld\nhelloooooworld\nhelo+world",
"expected": "helloworld\nhelloooooworld\n"
},
{
"s": "a\\+b",
"text": "a+b\nab\naab",
"expected": "a+b\n"
},
{
"s": "a++",
"text": "a\naa\naaa\naaaa",
"expected": "a\naa\naaa\naaaa\n"
},
{
"s": "a+++b+",
"text": "ab\naab\naaaabb\nabb",
"expected": "ab\naab\naaaabb\nabb\n"
},
{
"s": "x+y+z+",
"text": "xyz\nxxyz\nxxxxyzzzz",
"expected": "xyz\nxxyz\nxxxxyzzzz\n"
},
{
"s": "x+y+z+",
"text": "xz\nxyz\nxyzz",
"expected": "xyz\nxyzz\n"
},
{
"s": "+abc",
"text": "+abc\nabc\nxabc",
"expected": "+abc\n"
},
{
"s": "abc+",
"text": "abc\nabcc\nabccc\nab\n",
"expected": "abc\nabcc\nabccc\n"
},
{
"s": "so+me",
"text": "some\nsooooome\nsometimes\n",
"expected": "some\nsooooome\n"
},
{
"s": "a+b",
"text": "ab\naab\naaaab\nacb\n",
"expected": "ab\naab\naaaab\n"
},
{
"s": "a++b",
"text": "ab\naab\naaaab\nazb\n",
"expected": "ab\naab\naaaab\n"
},
{
"s": "a+++++b",
"text": "ab\naab\naaaab\naaaaaab\naxb\n",
"expected": "ab\naab\naaaab\naaaaaab\n"
},
{
"s": "start+end",
"text": "startend\nstartttend\nstart\nstartende\n",
"expected": "startend\nstartttend\n"
},
{
"s": "m++n",
"text": "mn\nmmn\nmmmn\nn\nmnn\n",
"expected": "mn\nmmn\nmmmn\n"
},
{
"s": "z+z",
"text": "z\nzz\nzzz\nzzzz\nzzzza\n",
"expected": "zz\nzzz\nzzzz\n"
},
{
"s": "++abc",
"text": "++abc\n+abc\n+++abc\n++++abc\n++abcd\n",
"expected": "++abc\n+abc\n+++abc\n++++abc\n"
},
{
"s": "slash\\+slash",
"text": "slash+slash\nslashslash\nslash++slash\n",
"expected": "slash+slash\n"
},
{
"s": "abc\\+def",
"text": "abc+def\nabc++def\nabcdef\nabc+defg\n",
"expected": "abc+def\n"
},
{
"s": "my++test",
"text": "mytest\nmyytest\nmtest\nmytests\n",
"expected": "mytest\nmyytest\n"
},
{
"s": "end+",
"text": "end\nendd\nenddd\nen\nendxd\n",
"expected": "end\nendd\nenddd\n"
},
{
"s": "",
"text": "some\nboy\nwhant\nto\nsee\n\n",
"expected": "\n"
}
]
}

View file

@ -0,0 +1,121 @@
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, s, text, expected):
self.s = s
self.text = text
self.expected = expected
def should_fail(self):
"""Checks if the test expects a failure"""
return self.expected.lower() == "fall"
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.f_in = self.config["f_in"]
self.f_out = self.config["f_out"]
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 run_test(test_suite, test):
"""Runs the program and checks its result"""
exe, f_in, f_out = test_suite.exe, test_suite.f_in, test_suite.f_out
# Write input data to a file
with open(f_in, "w", encoding="utf-8") as f:
f.write(test.text)
# Windows fix: remove './' for executables
cmd = [exe, f_in, f_out, test.s] if platform.system() == "Windows" else [f"./{exe}", f_in, f_out, test.s]
# Run the program
result = run_command(cmd)
# Check if test expected failure
if test.should_fail():
if result and result.returncode != 0:
print(color_text(f"[PASS] Test '{test.s}' correctly failed (expected crash).", Fore.GREEN))
else:
print(color_text(f"[FAIL] Test '{test.s}' should have failed but did not.", Fore.RED))
return
# Read output file
try:
with open(f_out, "r", encoding="utf-8") as f:
output = f.read()
except FileNotFoundError:
output = None
# Check result
if output == test.expected:
print(color_text(f"[PASS] Test '{test.s}' passed.", Fore.GREEN))
else:
print(color_text(f"[FAIL] Test '{test.s}' failed.", Fore.RED))
print(f"Expected:\n{test.expected}")
print(f"Got:\n{output}")
# Cleanup test files
for file in [f_in, f_out]:
try:
os.remove(file)
except (FileNotFoundError, PermissionError):
print(color_text(f"[WARNING] Could not delete {file}, 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()

View file

@ -0,0 +1,76 @@
Default:
{
s: 'some'
t: 'some'
}
Проверка, что если часть предложения:
{
s: 'some'
t: 'somebody here!?'
}
Проверка +:
{
s: 'so+me'
t: 'sooooome\nsme\nsome'
}
Проверка \s -> s:
{
s: 's\ome\\'
t: 'some\'
}
Проверка \:
{
s: 'some\\\'
t: 'not important'
}
Проверка \+:
{
s: '+o\+o'
t: '+o+o'
}
Множество +:
{
s: 'ha++++++ha'
t: 'haaaaaaaaha'
}
Проверка на не выход за стэк:
{
s: 'som+e'
t: 'sommmme'
}
abs
\abs
absababa
ahahabs
ahahahabsahhaha
^abs
ahahahabs$jajaja
ha\abss$hello what!
pbs
ubs
.abs
abs
pbt
ats
.b.
bs
bsaha
?abs
aaaaaaaaaaaaabs
+abs+
absssssss
abs*
somePalongPway
some
howPsomePitPbe
howPsomething
whyPanysome