From 23eff9e484dd2239638caf70dc5a18d9efdc9225 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Sun, 18 May 2025 18:31:04 +0300 Subject: [PATCH] Task 1 is done --- 2025.05.02/03Ex/init_f.c | 20 ++--- .../.nfs000000000e4a0ec40000000a | Bin 4096 -> 0 bytes 2025.05.23/01Ex/Makefile | 42 +++++++++ 2025.05.23/01Ex/io_node.c | 83 ++++++++++++++++++ 2025.05.23/01Ex/io_node.h | 12 +++ 2025.05.23/01Ex/main.c | 67 ++++++++++++++ 2025.05.23/01Ex/node.h | 14 +++ 2025.05.23/01Ex/solve.c | 23 +++++ 2025.05.23/01Ex/solve.h | 8 ++ 2025.05.23/01Ex/status.h | 16 ++++ 2025.05.23/Example/Makefile | 42 +++++++++ 2025.05.23/Example/io_node.c | 83 ++++++++++++++++++ 2025.05.23/Example/io_node.h | 12 +++ 2025.05.23/Example/main.c | 67 ++++++++++++++ 2025.05.23/Example/node.h | 14 +++ 2025.05.23/Example/solve.c | 23 +++++ 2025.05.23/Example/solve.h | 8 ++ 2025.05.23/Example/status.h | 16 ++++ 2025.05.23/tests/a.txt | 13 +++ 19 files changed, 553 insertions(+), 10 deletions(-) delete mode 100644 2025.05.09/dist/Krivoruchenko_SK/.nfs000000000e4a0ec40000000a create mode 100644 2025.05.23/01Ex/Makefile create mode 100644 2025.05.23/01Ex/io_node.c create mode 100644 2025.05.23/01Ex/io_node.h create mode 100644 2025.05.23/01Ex/main.c create mode 100644 2025.05.23/01Ex/node.h create mode 100644 2025.05.23/01Ex/solve.c create mode 100644 2025.05.23/01Ex/solve.h create mode 100644 2025.05.23/01Ex/status.h create mode 100644 2025.05.23/Example/Makefile create mode 100644 2025.05.23/Example/io_node.c create mode 100644 2025.05.23/Example/io_node.h create mode 100644 2025.05.23/Example/main.c create mode 100644 2025.05.23/Example/node.h create mode 100644 2025.05.23/Example/solve.c create mode 100644 2025.05.23/Example/solve.h create mode 100644 2025.05.23/Example/status.h create mode 100644 2025.05.23/tests/a.txt diff --git a/2025.05.02/03Ex/init_f.c b/2025.05.02/03Ex/init_f.c index 7314dd0..94fbd70 100644 --- a/2025.05.02/03Ex/init_f.c +++ b/2025.05.02/03Ex/init_f.c @@ -26,34 +26,34 @@ double f1 (double x) double f2 (double x) { - double x_2 = x * x; + // double x_2 = x * x; cl++; - return 4 - x_2; + return 4 - x * x; } double f3 (double x) { - double x_2 = x * x; - double x_3 = x * x_2; + //double x_2 = x * x; + //double x_3 = x * x_2; cl++; - return x_3 + 3 * x_2 + 16; + return x * x * x + 3 * x * x + 16; } double f4 (double x) { - double x_2 = x * x; - double x_4 = x_2 * x_2; + //double x_2 = x * x; + //double x_4 = x_2 * x_2; cl++; - return 3 - 2 * x_2 - x_4; + return 3 - 2 * x * x - x * x * x * x; } double f5 (double x) { - double sq_x = sqrt(fabs(x) + 1); + //double sq_x = sqrt(fabs(x) + 1); cl++; - return sq_x - 2; + return sqrt(fabs(x) + 1) - 2; } double f6 (double x) diff --git a/2025.05.09/dist/Krivoruchenko_SK/.nfs000000000e4a0ec40000000a b/2025.05.09/dist/Krivoruchenko_SK/.nfs000000000e4a0ec40000000a deleted file mode 100644 index cdc89135cbb72a5c54b0f0ced2296205f004c315..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4096 zcmYc?2=nw+u+TGP00IF926=9^j3?|~d|cuT4BkbVW%)&=$r-76+4=Fo-XJB|Kwhz- zp&2#_G_g8#1NDvKgHv-;i%U|A^o{aTK%97|{PaYijb-{q21cfO20&z~pORTzqK~1u zI6tQ>RWBLMl2Ptx2#kgRW(bs)r0H7lh8i0g8h|8~l@t|(g+ejajf#whz-S1JhQMeD XjE2By2#kinXb6mkz-R~z+YkT%%RVl| diff --git a/2025.05.23/01Ex/Makefile b/2025.05.23/01Ex/Makefile new file mode 100644 index 0000000..a0667a5 --- /dev/null +++ b/2025.05.23/01Ex/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 = a01.$(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/01Ex/io_node.c b/2025.05.23/01Ex/io_node.c new file mode 100644 index 0000000..ef5458d --- /dev/null +++ b/2025.05.23/01Ex/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/01Ex/io_node.h b/2025.05.23/01Ex/io_node.h new file mode 100644 index 0000000..0040f0d --- /dev/null +++ b/2025.05.23/01Ex/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/01Ex/main.c b/2025.05.23/01Ex/main.c new file mode 100644 index 0000000..4a1fdd0 --- /dev/null +++ b/2025.05.23/01Ex/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 = 1; + 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 = t1_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/01Ex/node.h b/2025.05.23/01Ex/node.h new file mode 100644 index 0000000..18cff38 --- /dev/null +++ b/2025.05.23/01Ex/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/01Ex/solve.c b/2025.05.23/01Ex/solve.c new file mode 100644 index 0000000..e9253a3 --- /dev/null +++ b/2025.05.23/01Ex/solve.c @@ -0,0 +1,23 @@ +#include "solve.h" +#include "node.h" + +#include + +int t1_solve (node *head) +{ + char *maximum = head->string; + int count = 1; + + for (head = head->next; head; head = head->next) + { + int cmp = strcmp(head->string, maximum); + if (cmp > 0) { + maximum = head->string; + count = 1; + } else if (!cmp) + count++; + } + + return count; +} + diff --git a/2025.05.23/01Ex/solve.h b/2025.05.23/01Ex/solve.h new file mode 100644 index 0000000..69742a9 --- /dev/null +++ b/2025.05.23/01Ex/solve.h @@ -0,0 +1,8 @@ +#ifndef SOLVE_H +#define SOLVE_H + +#include "node.h" + +int t1_solve (node *head); + +#endif diff --git a/2025.05.23/01Ex/status.h b/2025.05.23/01Ex/status.h new file mode 100644 index 0000000..38b6e67 --- /dev/null +++ b/2025.05.23/01Ex/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/Example/Makefile b/2025.05.23/Example/Makefile new file mode 100644 index 0000000..a0667a5 --- /dev/null +++ b/2025.05.23/Example/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 = a01.$(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/Example/io_node.c b/2025.05.23/Example/io_node.c new file mode 100644 index 0000000..ef5458d --- /dev/null +++ b/2025.05.23/Example/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/Example/io_node.h b/2025.05.23/Example/io_node.h new file mode 100644 index 0000000..0040f0d --- /dev/null +++ b/2025.05.23/Example/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/Example/main.c b/2025.05.23/Example/main.c new file mode 100644 index 0000000..4a1fdd0 --- /dev/null +++ b/2025.05.23/Example/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 = 1; + 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 = t1_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/Example/node.h b/2025.05.23/Example/node.h new file mode 100644 index 0000000..18cff38 --- /dev/null +++ b/2025.05.23/Example/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/Example/solve.c b/2025.05.23/Example/solve.c new file mode 100644 index 0000000..e9253a3 --- /dev/null +++ b/2025.05.23/Example/solve.c @@ -0,0 +1,23 @@ +#include "solve.h" +#include "node.h" + +#include + +int t1_solve (node *head) +{ + char *maximum = head->string; + int count = 1; + + for (head = head->next; head; head = head->next) + { + int cmp = strcmp(head->string, maximum); + if (cmp > 0) { + maximum = head->string; + count = 1; + } else if (!cmp) + count++; + } + + return count; +} + diff --git a/2025.05.23/Example/solve.h b/2025.05.23/Example/solve.h new file mode 100644 index 0000000..69742a9 --- /dev/null +++ b/2025.05.23/Example/solve.h @@ -0,0 +1,8 @@ +#ifndef SOLVE_H +#define SOLVE_H + +#include "node.h" + +int t1_solve (node *head); + +#endif diff --git a/2025.05.23/Example/status.h b/2025.05.23/Example/status.h new file mode 100644 index 0000000..38b6e67 --- /dev/null +++ b/2025.05.23/Example/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/a.txt b/2025.05.23/tests/a.txt new file mode 100644 index 0000000..9e2750c --- /dev/null +++ b/2025.05.23/tests/a.txt @@ -0,0 +1,13 @@ +a +b +c +cd +ab +bc +cd +cd +cd + +cd + +