Task 10 is done
This commit is contained in:
parent
50e9bb6d59
commit
80daf08c78
13 changed files with 349 additions and 0 deletions
42
2025.05.23/10Ex/Makefile
Normal file
42
2025.05.23/10Ex/Makefile
Normal file
|
@ -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 = a10.$(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)
|
9
2025.05.23/10Ex/input/a.txt
Normal file
9
2025.05.23/10Ex/input/a.txt
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
5
|
||||||
|
4
|
||||||
|
3
|
||||||
|
3
|
||||||
|
2
|
||||||
|
1
|
||||||
|
1
|
||||||
|
1
|
||||||
|
0
|
0
2025.05.23/10Ex/input/e.txt
Normal file
0
2025.05.23/10Ex/input/e.txt
Normal file
7
2025.05.23/10Ex/input/f.txt
Normal file
7
2025.05.23/10Ex/input/f.txt
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
3
|
||||||
|
3
|
||||||
|
2
|
5
2025.05.23/10Ex/input/o.txt
Normal file
5
2025.05.23/10Ex/input/o.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
8
2025.05.23/10Ex/input/s.txt
Normal file
8
2025.05.23/10Ex/input/s.txt
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
3
|
||||||
|
2
|
||||||
|
2
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
96
2025.05.23/10Ex/io_node.c
Normal file
96
2025.05.23/10Ex/io_node.c
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
#include "io_node.h"
|
||||||
|
#include "node.h"
|
||||||
|
#include "status.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int get_length (node * head)
|
||||||
|
{
|
||||||
|
node *curr;
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (curr = head; curr; curr=curr->next)
|
||||||
|
i++;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete_nodes (node *head, const int count)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < count; ++i)
|
||||||
|
{
|
||||||
|
node *temp = head;
|
||||||
|
head = head->next;
|
||||||
|
|
||||||
|
delete_node(temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
21
2025.05.23/10Ex/io_node.h
Normal file
21
2025.05.23/10Ex/io_node.h
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
#ifndef IO_NODE_H
|
||||||
|
#define IO_NODE_H
|
||||||
|
|
||||||
|
#include "node.h"
|
||||||
|
#include "status.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static inline void delete_node (node *head)
|
||||||
|
{
|
||||||
|
free(head->string);
|
||||||
|
free(head);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_length (node *head);
|
||||||
|
void delete_nodes (node *head, const int count);
|
||||||
|
void delete_list (node *head);
|
||||||
|
io_status read_list (node **list, const char *filename);
|
||||||
|
void print_list (node *head, const int p);
|
||||||
|
|
||||||
|
#endif
|
67
2025.05.23/10Ex/main.c
Normal file
67
2025.05.23/10Ex/main.c
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <float.h>
|
||||||
|
|
||||||
|
#include "solve.h"
|
||||||
|
#include "node.h"
|
||||||
|
#include "io_node.h"
|
||||||
|
#include "status.h"
|
||||||
|
|
||||||
|
/* ./a10.out p filename */
|
||||||
|
int main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
node *head;
|
||||||
|
char *name;
|
||||||
|
double t;
|
||||||
|
int p, len, task = 10;
|
||||||
|
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 = t10_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;
|
||||||
|
}
|
||||||
|
|
14
2025.05.23/10Ex/node.h
Normal file
14
2025.05.23/10Ex/node.h
Normal file
|
@ -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
|
56
2025.05.23/10Ex/solve.c
Normal file
56
2025.05.23/10Ex/solve.c
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
#include "solve.h"
|
||||||
|
#include "node.h"
|
||||||
|
#include "io_node.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
node * t10_solve (node *head)
|
||||||
|
{
|
||||||
|
int down = 0;
|
||||||
|
node *last, *curr, *next,
|
||||||
|
*start;
|
||||||
|
last=next=start=NULL;
|
||||||
|
curr = head;
|
||||||
|
|
||||||
|
for (next=curr->next; next; next=curr->next)
|
||||||
|
{
|
||||||
|
if (strcmp(next->string, curr->string) <= 0) {
|
||||||
|
if (!down)
|
||||||
|
start = last;
|
||||||
|
down++;
|
||||||
|
} else {
|
||||||
|
if (down) {
|
||||||
|
if (start) {
|
||||||
|
delete_nodes(start->next, down+1);
|
||||||
|
start->next = next;
|
||||||
|
last = start;
|
||||||
|
} else {
|
||||||
|
delete_nodes(head, down+1);
|
||||||
|
head = next;
|
||||||
|
last = NULL;
|
||||||
|
}
|
||||||
|
curr = NULL;
|
||||||
|
}
|
||||||
|
down = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
last = curr;
|
||||||
|
curr = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (down) {
|
||||||
|
if (start) {
|
||||||
|
delete_nodes(start->next, down+1);
|
||||||
|
start->next = NULL;
|
||||||
|
} else {
|
||||||
|
delete_nodes(head, down+1);
|
||||||
|
head = NULL;
|
||||||
|
}
|
||||||
|
curr = NULL;
|
||||||
|
last = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
8
2025.05.23/10Ex/solve.h
Normal file
8
2025.05.23/10Ex/solve.h
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef SOLVE_H
|
||||||
|
#define SOLVE_H
|
||||||
|
|
||||||
|
#include "node.h"
|
||||||
|
|
||||||
|
node * t10_solve (node *head);
|
||||||
|
|
||||||
|
#endif
|
16
2025.05.23/10Ex/status.h
Normal file
16
2025.05.23/10Ex/status.h
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue