From d7ac3be8573465adade3008d9a96bcce4debcca6 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Mon, 19 May 2025 19:56:04 +0300 Subject: [PATCH] Started task 9 --- 2025.05.23/09Ex/Makefile | 42 ++++++++++++++++++ 2025.05.23/09Ex/input/a.txt | 6 +++ 2025.05.23/09Ex/input/e.txt | 0 2025.05.23/09Ex/input/f.txt | 5 +++ 2025.05.23/09Ex/input/m.txt | 7 +++ 2025.05.23/09Ex/input/o.txt | 5 +++ 2025.05.23/09Ex/input/s.txt | 5 +++ 2025.05.23/09Ex/io_node.c | 85 +++++++++++++++++++++++++++++++++++++ 2025.05.23/09Ex/io_node.h | 20 +++++++++ 2025.05.23/09Ex/main.c | 67 +++++++++++++++++++++++++++++ 2025.05.23/09Ex/node.h | 14 ++++++ 2025.05.23/09Ex/solve.c | 78 ++++++++++++++++++++++++++++++++++ 2025.05.23/09Ex/solve.h | 8 ++++ 2025.05.23/09Ex/status.h | 16 +++++++ 14 files changed, 358 insertions(+) create mode 100644 2025.05.23/09Ex/Makefile create mode 100644 2025.05.23/09Ex/input/a.txt create mode 100644 2025.05.23/09Ex/input/e.txt create mode 100644 2025.05.23/09Ex/input/f.txt create mode 100644 2025.05.23/09Ex/input/m.txt create mode 100644 2025.05.23/09Ex/input/o.txt create mode 100644 2025.05.23/09Ex/input/s.txt create mode 100644 2025.05.23/09Ex/io_node.c create mode 100644 2025.05.23/09Ex/io_node.h create mode 100644 2025.05.23/09Ex/main.c create mode 100644 2025.05.23/09Ex/node.h create mode 100644 2025.05.23/09Ex/solve.c create mode 100644 2025.05.23/09Ex/solve.h create mode 100644 2025.05.23/09Ex/status.h diff --git a/2025.05.23/09Ex/Makefile b/2025.05.23/09Ex/Makefile new file mode 100644 index 0000000..c27a658 --- /dev/null +++ b/2025.05.23/09Ex/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 = a09.$(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/09Ex/input/a.txt b/2025.05.23/09Ex/input/a.txt new file mode 100644 index 0000000..5ee4e75 --- /dev/null +++ b/2025.05.23/09Ex/input/a.txt @@ -0,0 +1,6 @@ +2 +2 +2 +2 +2 +2 diff --git a/2025.05.23/09Ex/input/e.txt b/2025.05.23/09Ex/input/e.txt new file mode 100644 index 0000000..e69de29 diff --git a/2025.05.23/09Ex/input/f.txt b/2025.05.23/09Ex/input/f.txt new file mode 100644 index 0000000..8a1218a --- /dev/null +++ b/2025.05.23/09Ex/input/f.txt @@ -0,0 +1,5 @@ +1 +2 +3 +4 +5 diff --git a/2025.05.23/09Ex/input/m.txt b/2025.05.23/09Ex/input/m.txt new file mode 100644 index 0000000..8c5fda9 --- /dev/null +++ b/2025.05.23/09Ex/input/m.txt @@ -0,0 +1,7 @@ +1 +2 +3 +3 +3 +2 +1 diff --git a/2025.05.23/09Ex/input/o.txt b/2025.05.23/09Ex/input/o.txt new file mode 100644 index 0000000..86e24d1 --- /dev/null +++ b/2025.05.23/09Ex/input/o.txt @@ -0,0 +1,5 @@ +5 +4 +3 +2 +1 diff --git a/2025.05.23/09Ex/input/s.txt b/2025.05.23/09Ex/input/s.txt new file mode 100644 index 0000000..45e168a --- /dev/null +++ b/2025.05.23/09Ex/input/s.txt @@ -0,0 +1,5 @@ +2 +1 +3 +4 +5 diff --git a/2025.05.23/09Ex/io_node.c b/2025.05.23/09Ex/io_node.c new file mode 100644 index 0000000..2a8e254 --- /dev/null +++ b/2025.05.23/09Ex/io_node.c @@ -0,0 +1,85 @@ +#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; + delete_node(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; + } + + if (!head) + return ERR_READ; + + *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/09Ex/io_node.h b/2025.05.23/09Ex/io_node.h new file mode 100644 index 0000000..e4b5399 --- /dev/null +++ b/2025.05.23/09Ex/io_node.h @@ -0,0 +1,20 @@ +#ifndef IO_NODE_H +#define IO_NODE_H + +#include "node.h" +#include "status.h" + +#include + +static inline void delete_node (node *head) +{ + free(head->string); + free(head); +} + +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/09Ex/main.c b/2025.05.23/09Ex/main.c new file mode 100644 index 0000000..82dec41 --- /dev/null +++ b/2025.05.23/09Ex/main.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#include "solve.h" +#include "node.h" +#include "io_node.h" +#include "status.h" + +/* ./a09.out p filename */ +int main (int argc, char *argv[]) +{ + node *head; + char *name; + double t; + int p, len, task = 9; + 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); + + fprintf (stdout, "Source list:\n"); + print_list(head, p); + + t = clock(); + head = t9_solve(head); + t = (clock() - t) / CLOCKS_PER_SEC; + + fprintf (stdout, "\nFinal list:\n"); + print_list(head, p); + len = get_length(head); + + delete_list(head); + + fprintf (stdout, "%s : Task = %d Length = %d Elapsed = %.2f\n", argv[0], task, len, t); + return 0; +} + diff --git a/2025.05.23/09Ex/node.h b/2025.05.23/09Ex/node.h new file mode 100644 index 0000000..18cff38 --- /dev/null +++ b/2025.05.23/09Ex/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/09Ex/solve.c b/2025.05.23/09Ex/solve.c new file mode 100644 index 0000000..eae4baa --- /dev/null +++ b/2025.05.23/09Ex/solve.c @@ -0,0 +1,78 @@ +#include "solve.h" +#include "node.h" +#include "io_node.h" + +#include +#include + +node * t9_solve (node *head) +{ + node *last, *curr = head, *next, + *deleted = NULL, *last_cmp; + int cmp; + + next = head->next; + if (!next) + return head; + + cmp = strcmp(curr->string, next->string); + if (cmp >= 0) { + deleted = curr; + + head = next; + curr = next; + next = next->next; + + if (!next) { + delete_node(deleted); + if (cmp == 0) + delete_node(curr); + return NULL; + } + } + + for (next = curr->next; next->next;) + { + last_cmp = curr; + + last = curr; + curr = next; + next = curr->next; + + if (deleted) + last_cmp = deleted; + + if ( + (strcmp(curr->string, next->string) >= 0) && + (strcmp(curr->string, last_cmp->string) >= 0) + ) { + if (deleted) + delete_node(deleted); + deleted = curr; + + last->next = next; + curr = last; + } else { + if (deleted) + delete_node(deleted); + deleted = NULL; + } + } + + if (deleted) + last_cmp = deleted; + else + last_cmp = curr; + + if (strcmp(next->string, last_cmp->string) >= 0) { + curr->next = NULL; + + delete_node(next); + } + + if (deleted) + delete_node(deleted); + + return head; +} + diff --git a/2025.05.23/09Ex/solve.h b/2025.05.23/09Ex/solve.h new file mode 100644 index 0000000..30a5df5 --- /dev/null +++ b/2025.05.23/09Ex/solve.h @@ -0,0 +1,8 @@ +#ifndef SOLVE_H +#define SOLVE_H + +#include "node.h" + +node * t9_solve (node *head); + +#endif diff --git a/2025.05.23/09Ex/status.h b/2025.05.23/09Ex/status.h new file mode 100644 index 0000000..38b6e67 --- /dev/null +++ b/2025.05.23/09Ex/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