diff --git a/2025.05.23/01Ex/io_node.c b/2025.05.23/01Ex/io_node.c index ef5458d..254db40 100644 --- a/2025.05.23/01Ex/io_node.c +++ b/2025.05.23/01Ex/io_node.c @@ -68,6 +68,9 @@ io_status read_list (node **list, const char *filename) lunit = unit; } + + if (!head) + return ERR_READ; *list = head; diff --git a/2025.05.23/02Ex/io_node.c b/2025.05.23/02Ex/io_node.c index ef5458d..254db40 100644 --- a/2025.05.23/02Ex/io_node.c +++ b/2025.05.23/02Ex/io_node.c @@ -68,6 +68,9 @@ io_status read_list (node **list, const char *filename) lunit = unit; } + + if (!head) + return ERR_READ; *list = head; diff --git a/2025.05.23/03Ex/io_node.c b/2025.05.23/03Ex/io_node.c index ef5458d..254db40 100644 --- a/2025.05.23/03Ex/io_node.c +++ b/2025.05.23/03Ex/io_node.c @@ -68,6 +68,9 @@ io_status read_list (node **list, const char *filename) lunit = unit; } + + if (!head) + return ERR_READ; *list = head; diff --git a/2025.05.23/05Ex/io_node.c b/2025.05.23/05Ex/io_node.c index ef5458d..254db40 100644 --- a/2025.05.23/05Ex/io_node.c +++ b/2025.05.23/05Ex/io_node.c @@ -68,6 +68,9 @@ io_status read_list (node **list, const char *filename) lunit = unit; } + + if (!head) + return ERR_READ; *list = head; diff --git a/2025.05.23/06Ex/Makefile b/2025.05.23/06Ex/Makefile new file mode 100644 index 0000000..ad1005f --- /dev/null +++ b/2025.05.23/06Ex/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 = a06.$(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/06Ex/input/a.txt b/2025.05.23/06Ex/input/a.txt new file mode 100644 index 0000000..1637522 --- /dev/null +++ b/2025.05.23/06Ex/input/a.txt @@ -0,0 +1,10 @@ +a +a +a +b +b +b +c +d +d +d diff --git a/2025.05.23/06Ex/io_node.c b/2025.05.23/06Ex/io_node.c new file mode 100644 index 0000000..254db40 --- /dev/null +++ b/2025.05.23/06Ex/io_node.c @@ -0,0 +1,86 @@ +#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; + } + + 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/06Ex/io_node.h b/2025.05.23/06Ex/io_node.h new file mode 100644 index 0000000..0040f0d --- /dev/null +++ b/2025.05.23/06Ex/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/06Ex/main.c b/2025.05.23/06Ex/main.c new file mode 100644 index 0000000..9c21d7c --- /dev/null +++ b/2025.05.23/06Ex/main.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#include "solve.h" +#include "node.h" +#include "io_node.h" +#include "status.h" + +/* ./a06.out p filename */ +int main (int argc, char *argv[]) +{ + node *head; + char *name; + double t; + int p, res, task = 6; + 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 = t6_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/06Ex/node.h b/2025.05.23/06Ex/node.h new file mode 100644 index 0000000..18cff38 --- /dev/null +++ b/2025.05.23/06Ex/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/06Ex/solve.c b/2025.05.23/06Ex/solve.c new file mode 100644 index 0000000..5210f6e --- /dev/null +++ b/2025.05.23/06Ex/solve.c @@ -0,0 +1,20 @@ +#include "solve.h" +#include "node.h" + +#include + +int t6_solve (node *head) +{ + char *last = head->string; + int count = 1; + + for (head = head->next; head; head = head->next) + { + if (strcmp(head->string, last)) + count++; + last = head->string; + } + + return count; +} + diff --git a/2025.05.23/06Ex/solve.h b/2025.05.23/06Ex/solve.h new file mode 100644 index 0000000..dbf48a3 --- /dev/null +++ b/2025.05.23/06Ex/solve.h @@ -0,0 +1,8 @@ +#ifndef SOLVE_H +#define SOLVE_H + +#include "node.h" + +int t6_solve (node *head); + +#endif diff --git a/2025.05.23/06Ex/status.h b/2025.05.23/06Ex/status.h new file mode 100644 index 0000000..38b6e67 --- /dev/null +++ b/2025.05.23/06Ex/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/07Ex/Makefile b/2025.05.23/07Ex/Makefile new file mode 100644 index 0000000..7271288 --- /dev/null +++ b/2025.05.23/07Ex/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 = a07.$(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/07Ex/input/a.txt b/2025.05.23/07Ex/input/a.txt new file mode 100644 index 0000000..06e607e --- /dev/null +++ b/2025.05.23/07Ex/input/a.txt @@ -0,0 +1,16 @@ +a +b +d +e +c +c +c + +d +d +d +d +a +a +a + diff --git a/2025.05.23/07Ex/input/f.txt b/2025.05.23/07Ex/input/f.txt new file mode 100644 index 0000000..e69de29 diff --git a/2025.05.23/07Ex/io_node.c b/2025.05.23/07Ex/io_node.c new file mode 100644 index 0000000..254db40 --- /dev/null +++ b/2025.05.23/07Ex/io_node.c @@ -0,0 +1,86 @@ +#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; + } + + 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/07Ex/io_node.h b/2025.05.23/07Ex/io_node.h new file mode 100644 index 0000000..0040f0d --- /dev/null +++ b/2025.05.23/07Ex/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/07Ex/main.c b/2025.05.23/07Ex/main.c new file mode 100644 index 0000000..19405b8 --- /dev/null +++ b/2025.05.23/07Ex/main.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#include "solve.h" +#include "node.h" +#include "io_node.h" +#include "status.h" + +/* ./a07.out p filename */ +int main (int argc, char *argv[]) +{ + node *head; + char *name; + double t; + int p, res, task = 7; + 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 = t7_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/07Ex/node.h b/2025.05.23/07Ex/node.h new file mode 100644 index 0000000..18cff38 --- /dev/null +++ b/2025.05.23/07Ex/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/07Ex/solve.c b/2025.05.23/07Ex/solve.c new file mode 100644 index 0000000..70463eb --- /dev/null +++ b/2025.05.23/07Ex/solve.c @@ -0,0 +1,28 @@ +#include "solve.h" +#include "node.h" + +#include + +int t7_solve (node *head) +{ + char *last = head->string; + int maximum = 0, count = 1; + + for (head = head->next; head; head = head->next) + { + if (strcmp(head->string, last)) + count++; + else { + if (maximum < (count-1)) + maximum = count-1; + count = 0; + } + last = head->string; + } + + if (maximum < count) + maximum = count; + + return maximum; +} + diff --git a/2025.05.23/07Ex/solve.h b/2025.05.23/07Ex/solve.h new file mode 100644 index 0000000..e35b281 --- /dev/null +++ b/2025.05.23/07Ex/solve.h @@ -0,0 +1,8 @@ +#ifndef SOLVE_H +#define SOLVE_H + +#include "node.h" + +int t7_solve (node *head); + +#endif diff --git a/2025.05.23/07Ex/status.h b/2025.05.23/07Ex/status.h new file mode 100644 index 0000000..38b6e67 --- /dev/null +++ b/2025.05.23/07Ex/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