From cd08e7ba3a8e5ebe4541af1c58e1d4b6f1b3b203 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Sun, 18 May 2025 19:39:04 +0300 Subject: [PATCH] Task 2 is done --- 2025.05.23/02Ex/Makefile | 42 ++++++++++++++ 2025.05.23/02Ex/io_node.c | 83 ++++++++++++++++++++++++++++ 2025.05.23/02Ex/io_node.h | 12 ++++ 2025.05.23/02Ex/main.c | 67 ++++++++++++++++++++++ 2025.05.23/02Ex/node.h | 14 +++++ 2025.05.23/02Ex/solve.c | 20 +++++++ 2025.05.23/02Ex/solve.h | 8 +++ 2025.05.23/02Ex/status.h | 16 ++++++ 2025.05.23/tests/file_test.sh | 39 +++++++++++++ 2025.05.23/tests/inputs/input_1.txt | 13 +++++ 2025.05.23/tests/inputs/input_10.txt | 8 +++ 2025.05.23/tests/inputs/input_2.txt | 10 ++++ 2025.05.23/tests/inputs/input_3.txt | 7 +++ 2025.05.23/tests/inputs/input_4.txt | 9 +++ 2025.05.23/tests/inputs/input_5.txt | 8 +++ 2025.05.23/tests/inputs/input_6.txt | 8 +++ 2025.05.23/tests/inputs/input_7.txt | 10 ++++ 2025.05.23/tests/inputs/input_8.txt | 7 +++ 2025.05.23/tests/inputs/input_9.txt | 9 +++ 2025.05.23/tests/main_test.sh | 63 +++++++++++++++++++++ 20 files changed, 453 insertions(+) create mode 100644 2025.05.23/02Ex/Makefile create mode 100644 2025.05.23/02Ex/io_node.c create mode 100644 2025.05.23/02Ex/io_node.h create mode 100644 2025.05.23/02Ex/main.c create mode 100644 2025.05.23/02Ex/node.h create mode 100644 2025.05.23/02Ex/solve.c create mode 100644 2025.05.23/02Ex/solve.h create mode 100644 2025.05.23/02Ex/status.h create mode 100755 2025.05.23/tests/file_test.sh create mode 100755 2025.05.23/tests/inputs/input_1.txt create mode 100755 2025.05.23/tests/inputs/input_10.txt create mode 100755 2025.05.23/tests/inputs/input_2.txt create mode 100755 2025.05.23/tests/inputs/input_3.txt create mode 100755 2025.05.23/tests/inputs/input_4.txt create mode 100755 2025.05.23/tests/inputs/input_5.txt create mode 100755 2025.05.23/tests/inputs/input_6.txt create mode 100755 2025.05.23/tests/inputs/input_7.txt create mode 100755 2025.05.23/tests/inputs/input_8.txt create mode 100755 2025.05.23/tests/inputs/input_9.txt create mode 100755 2025.05.23/tests/main_test.sh diff --git a/2025.05.23/02Ex/Makefile b/2025.05.23/02Ex/Makefile new file mode 100644 index 0000000..7e3c3b5 --- /dev/null +++ b/2025.05.23/02Ex/Makefile @@ -0,0 +1,42 @@ +WFLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign + +LDFLAGS = -std=gnu99 -mfpmath=sse -O3 +LDLIBS = -lm + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LDLIBS += -lssp +else + EXE = out + CLEAN = rm -f +endif + +TARGET = a02.$(EXE) +OBJ = main.o solve.o io_node.o + +%.o: %.c + gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LDLIBS) + +# Отладочная сборка (gdb) +gdb: LDFLAGS = -std=gnu99 -mfpmath=sse -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: LDFLAGS += -pg +prof: LDLIBS += -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *$(EXE) diff --git a/2025.05.23/02Ex/io_node.c b/2025.05.23/02Ex/io_node.c new file mode 100644 index 0000000..ef5458d --- /dev/null +++ b/2025.05.23/02Ex/io_node.c @@ -0,0 +1,83 @@ +#include "io_node.h" +#include "node.h" +#include "status.h" + +#include +#include +#include + +int get_length (node * head) +{ + node *curr; + + int i = 0; + for (curr = head; curr; curr=curr->next) + i++; + + return i; +} + +void delete_list (node * head) +{ + node *curr, *next; + for (curr = head; curr; curr = next) { + next = curr->next; + free(curr->string); + free(curr); + } +} + +io_status read_list (node **list, const char *filename) +{ + FILE *fp = 0; + char *string, buf[LEN_STR]; + node *head, *lunit, *unit; + + head = lunit = unit = NULL; + + if (!(fp = fopen(filename, "r"))) + return ERR_OPEN; + + while (fgets(buf, LEN_STR, fp)) + { + unit = (node *)malloc(sizeof(node)); + if (!unit) { + fclose(fp); + delete_list(head); + + return ERR_MEM; + } + + unit->next = NULL; + + buf[strcspn(buf, "\n")] = '\0'; + string = strdup(buf); + if (!string) { + fclose(fp); + free(unit); + delete_list(head); + + return ERR_MEM; + } + + unit->string = string; + if (!lunit) + head = unit; + else + lunit->next = unit; + + lunit = unit; + } + + *list = head; + + fclose(fp); + return SUCCESS; +} + +void print_list (node *head, const int p) +{ + for (int i = 0; head && i < p; head = head->next, i++) + printf("%s\n", head->string); +} + diff --git a/2025.05.23/02Ex/io_node.h b/2025.05.23/02Ex/io_node.h new file mode 100644 index 0000000..0040f0d --- /dev/null +++ b/2025.05.23/02Ex/io_node.h @@ -0,0 +1,12 @@ +#ifndef IO_NODE_H +#define IO_NODE_H + +#include "node.h" +#include "status.h" + +int get_length (node * head); +void delete_list (node * head); +io_status read_list (node **list, const char *filename); +void print_list (node *head, const int p); + +#endif diff --git a/2025.05.23/02Ex/main.c b/2025.05.23/02Ex/main.c new file mode 100644 index 0000000..1ccab64 --- /dev/null +++ b/2025.05.23/02Ex/main.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#include "solve.h" +#include "node.h" +#include "io_node.h" +#include "status.h" + +/* ./a01.out p filename */ +int main (int argc, char *argv[]) +{ + node *head; + char *name; + double t; + int p, res, task = 2; + io_status ret; + + if ( + !((argc == 3) && + sscanf(argv[1], "%d", &p) == 1 && + argv[2]) + ) { + fprintf(stderr, "Usage: %s p filename\n", argv[0]); + return -1; + } + + name = argv[2]; + + ret = read_list(&head, name); + do { + switch (ret) { + case SUCCESS: + continue; + case ERR_OPEN: + fprintf (stderr, "%s '%s'!\n", ERR_MSG_OPEN, name); + break; + case ERR_READ: + fprintf (stderr, "%s '%s'!\n", ERR_MSG_READ, name); + break; + case ERR_MEM: + fprintf (stderr, "%s!\n", ERR_MSG_MEM); + break; + } + + delete_list(head); + return -2; + } while (0); + + print_list(head, p); + + t = clock(); + res = t2_solve(head); + t = (clock() - t) / CLOCKS_PER_SEC; + + delete_list(head); + + if (res < 0) { + fprintf (stderr, "%s : Task = %d Undefined error Elapsed = %.2f\n", argv[0], task, t); + return -3; + } else { + fprintf (stdout, "%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t); + return 0; + } +} + diff --git a/2025.05.23/02Ex/node.h b/2025.05.23/02Ex/node.h new file mode 100644 index 0000000..18cff38 --- /dev/null +++ b/2025.05.23/02Ex/node.h @@ -0,0 +1,14 @@ +#ifndef NODE_H +#define NODE_H + +#define LEN_STR 1234 + +struct _node; + +typedef struct _node +{ + char *string; + struct _node *next; +} node; + +#endif diff --git a/2025.05.23/02Ex/solve.c b/2025.05.23/02Ex/solve.c new file mode 100644 index 0000000..acfc958 --- /dev/null +++ b/2025.05.23/02Ex/solve.c @@ -0,0 +1,20 @@ +#include "solve.h" +#include "node.h" + +#include + +int t2_solve (node *head) +{ + char *last = head->string; + int count = 1; + + for (head = head->next; head; head = head->next) + { + if (strcmp(head->string, last) > 0) + count++; + last = head->string; + } + + return count; +} + diff --git a/2025.05.23/02Ex/solve.h b/2025.05.23/02Ex/solve.h new file mode 100644 index 0000000..8cd007e --- /dev/null +++ b/2025.05.23/02Ex/solve.h @@ -0,0 +1,8 @@ +#ifndef SOLVE_H +#define SOLVE_H + +#include "node.h" + +int t2_solve (node *head); + +#endif diff --git a/2025.05.23/02Ex/status.h b/2025.05.23/02Ex/status.h new file mode 100644 index 0000000..38b6e67 --- /dev/null +++ b/2025.05.23/02Ex/status.h @@ -0,0 +1,16 @@ +#ifndef STATUS_H +#define STATUS_H + +#define ERR_MSG_MEM "Error: Not enough memory" +#define ERR_MSG_OPEN "Error: Cannot open file" +#define ERR_MSG_READ "Error: Cannot read file" + +typedef enum +{ + SUCCESS, + ERR_OPEN = -1, + ERR_READ = -2, + ERR_MEM = -3 +} io_status; + +#endif diff --git a/2025.05.23/tests/file_test.sh b/2025.05.23/tests/file_test.sh new file mode 100755 index 0000000..cc0e05c --- /dev/null +++ b/2025.05.23/tests/file_test.sh @@ -0,0 +1,39 @@ +script_name="$(basename "$0")" +script_path="$(realpath "$0")" +script_dir="$(dirname "$script_path")" + +maxpr="100" + +mkdir -p tests + +if [ -f Makefile ]; then + echo "Компиляция..." + make clean + make +fi + +outlog="$(pwd)/tests/out_$script_name.log" +errlog="$(pwd)/tests/err_$script_name.log" + +rm -f "$outlog" "$errlog" + +echo "Тест запущен..." + +for npr in {1..7} ; do + num=$(printf "%02d" "$npr") + prog="a$num.out" + if [ -f $prog ]; then + for ntst in {1..10} ; do + cmd="./$prog $maxpr $script_dir/inputs/input_$ntst.txt" + echo "$cmd" + eval "$cmd" + done + fi +done >$outlog 2>$errlog + +echo "Тест записан в $outlog" +echo "Ошибки записаны в $errlog" +echo "Тест завершен" + +make clean + diff --git a/2025.05.23/tests/inputs/input_1.txt b/2025.05.23/tests/inputs/input_1.txt new file mode 100755 index 0000000..61e9d17 --- /dev/null +++ b/2025.05.23/tests/inputs/input_1.txt @@ -0,0 +1,13 @@ +pffbvlv +ajinkzj +ujfehpd +ntncamuam +kgjb +gfdnncjrop +fwi +zzzzzzz +zzzzzzz +wpfa +udjjxc +zzzzzzz +jsmk diff --git a/2025.05.23/tests/inputs/input_10.txt b/2025.05.23/tests/inputs/input_10.txt new file mode 100755 index 0000000..d2c48f1 --- /dev/null +++ b/2025.05.23/tests/inputs/input_10.txt @@ -0,0 +1,8 @@ +aaa +aaa +bbb +bbb +ccc +ccc +ccc +ddd diff --git a/2025.05.23/tests/inputs/input_2.txt b/2025.05.23/tests/inputs/input_2.txt new file mode 100755 index 0000000..0f39578 --- /dev/null +++ b/2025.05.23/tests/inputs/input_2.txt @@ -0,0 +1,10 @@ +aaa +wvr +dseeky +dseekyz +dseekyzz +ioeenuz +qptsadk +scpgynd +xqovzvmxvr +xqovzvmxvrz diff --git a/2025.05.23/tests/inputs/input_3.txt b/2025.05.23/tests/inputs/input_3.txt new file mode 100755 index 0000000..0076fb3 --- /dev/null +++ b/2025.05.23/tests/inputs/input_3.txt @@ -0,0 +1,7 @@ +aaa +ccc +bbb +ddd +bbb +eee +ddd diff --git a/2025.05.23/tests/inputs/input_4.txt b/2025.05.23/tests/inputs/input_4.txt new file mode 100755 index 0000000..22bd3a8 --- /dev/null +++ b/2025.05.23/tests/inputs/input_4.txt @@ -0,0 +1,9 @@ +aaa +ccc +bbb +bbb +eee +ddd +fff +ddd +ccc diff --git a/2025.05.23/tests/inputs/input_5.txt b/2025.05.23/tests/inputs/input_5.txt new file mode 100755 index 0000000..255f430 --- /dev/null +++ b/2025.05.23/tests/inputs/input_5.txt @@ -0,0 +1,8 @@ +a +ab +abc +abcd +abcde +abc +abb +ac diff --git a/2025.05.23/tests/inputs/input_6.txt b/2025.05.23/tests/inputs/input_6.txt new file mode 100755 index 0000000..d2c48f1 --- /dev/null +++ b/2025.05.23/tests/inputs/input_6.txt @@ -0,0 +1,8 @@ +aaa +aaa +bbb +bbb +ccc +ccc +ccc +ddd diff --git a/2025.05.23/tests/inputs/input_7.txt b/2025.05.23/tests/inputs/input_7.txt new file mode 100755 index 0000000..0313dee --- /dev/null +++ b/2025.05.23/tests/inputs/input_7.txt @@ -0,0 +1,10 @@ +aaa +aaa +bbb +ccc +ddd +eee +fff +ggg +hhh +hhh diff --git a/2025.05.23/tests/inputs/input_8.txt b/2025.05.23/tests/inputs/input_8.txt new file mode 100755 index 0000000..0076fb3 --- /dev/null +++ b/2025.05.23/tests/inputs/input_8.txt @@ -0,0 +1,7 @@ +aaa +ccc +bbb +ddd +bbb +eee +ddd diff --git a/2025.05.23/tests/inputs/input_9.txt b/2025.05.23/tests/inputs/input_9.txt new file mode 100755 index 0000000..22bd3a8 --- /dev/null +++ b/2025.05.23/tests/inputs/input_9.txt @@ -0,0 +1,9 @@ +aaa +ccc +bbb +bbb +eee +ddd +fff +ddd +ccc diff --git a/2025.05.23/tests/main_test.sh b/2025.05.23/tests/main_test.sh new file mode 100755 index 0000000..a501b07 --- /dev/null +++ b/2025.05.23/tests/main_test.sh @@ -0,0 +1,63 @@ +script_name="$(basename "$0")" + +iter="1000" +eps="1e-14" + +if [ "$#" -ne 1 ]; then + echo "Не указан как параметр номер программы" + exit 1 +fi + +num=$(printf "%02d" "$1") +prog="a$num.out" + +mkdir -p tests + +if [ -f Makefile ]; then + echo "Компиляция..." + make clean + make +fi + +if [ ! -f $prog ]; then + echo "Отсутствует исполняемый файл... [$prog]" + echo "Завершение..." + exit 2 +fi + +outlog="$(pwd)/tests/out_a${num}_$script_name.log" +errlog="$(pwd)/tests/err_a${num}_$script_name.log" + +rm -f "$outlog" "$errlog" + +echo "Тест запущен..." + +i=2 + +for (( k = 3 ; k < 7; k++ )); do + for (( a = -100 ; a < -40 ; a++ )); do + for (( b = -9 ; b < 10 ; b++ )); do + x="$(echo "$a / 10" | bc -l)" + y="$(echo "$b / 10" | bc -l)" + cmd="./$prog $poly_deg $x $y $eps $iter $k" + echo "$cmd" + echo "$i $(eval "$cmd")" + ((i+=2)) + done + done + for (( a = -9 ; a < 10 ; a++ )); do + for (( b = 11 ; b < 100 ; b++ )); do + x="$(echo "$a / 10" | bc -l)" + y="$(echo "$b / 10" | bc -l)" + cmd="./$prog $poly_deg $x $y $eps $iter $k" + echo "$cmd" + echo "$i $(eval "$cmd")" + ((i+=2)) + done + done +done >$outlog 2>$errlog + +echo "Тест записан в $outlog" +echo "Ошибки записаны в $errlog" +echo "Тест завершен" +