This commit is contained in:
AZEN-SGG 2025-05-20 10:28:29 +03:00
parent 80daf08c78
commit 255ca54f41
12 changed files with 336 additions and 50 deletions

View file

@ -7,72 +7,67 @@
node * t9_solve (node *head)
{
node *last, *curr = head, *next,
*deleted = NULL, *last_cmp;
int cmp;
int cmp_last, cmp_next;
node *last, *curr, *next,
*deleted;
last=next=deleted=NULL;
cmp_last=cmp_next=0;
curr = head;
if (!curr)
return NULL;
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;)
for (next = curr->next; next; next = curr->next)
{
last_cmp = curr;
last = curr;
curr = next;
next = curr->next;
if (deleted)
last_cmp = deleted;
cmp_last = strcmp(curr->string, deleted->string);
else if (!last)
cmp_last = 1;
else
cmp_last = strcmp(curr->string, last->string);
cmp_next = strcmp(curr->string, next->string); // Как происходит обращение в память для структур?
if (
(strcmp(curr->string, next->string) >= 0) &&
(strcmp(curr->string, last_cmp->string) >= 0)
) {
if ((cmp_last >= 0) && (cmp_next >= 0))
{
if (deleted)
delete_node(deleted);
deleted = curr;
last->next = next;
curr = last;
} else {
if (last) {
last->next = next;
curr = last;
} else {
head = next;
curr = NULL;
}
} else
{
if (deleted)
delete_node(deleted);
deleted = NULL;
}
last = curr;
curr = next;
}
if (deleted)
last_cmp = deleted;
cmp_last = strcmp(curr->string, deleted->string);
else if (!last)
cmp_last = 1;
else
last_cmp = curr;
cmp_last = strcmp(curr->string, last->string);
if (strcmp(next->string, last_cmp->string) >= 0) {
curr->next = NULL;
delete_node(next);
if (cmp_last >= 0)
{
delete_node(curr);
if (last)
last->next = NULL;
else
head = NULL;
curr = NULL;
}
if (deleted)
delete_node(deleted);
return head;
return head;
}

Binary file not shown.

View file

@ -2,17 +2,17 @@ FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pe
OBJ_COMMON = io_node.o
NUMS = 1 2 3 4 5 6 7 8 9 10 11 12
NUMS = 1 2 3 4 5 6 7 8 9 10
OUTS = $(foreach n,$(NUMS),$(shell printf "a%02d.out\n" "$(n)"))
all: $(OUTS)
%.o: %.c
gcc -c $(FLAGS) $<
-gcc -c $(FLAGS) $<
a%.out: main_%.o solve_%.o $(OBJ_COMMON)
gcc $(FLAGS) $^ -o $@ -lm
-gcc $(FLAGS) $^ -o $@ -lm
clean:
rm -f *.o *.out

View file

@ -17,6 +17,17 @@ int get_length (node * head)
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;

View file

@ -13,6 +13,7 @@ static inline void delete_node (node *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);

View file

@ -0,0 +1,67 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include "solve_09.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;
}

View file

@ -0,0 +1,67 @@
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include "solve_10.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;
}

View file

@ -0,0 +1,73 @@
#include "solve_09.h"
#include "node.h"
#include "io_node.h"
#include <string.h>
#include <stdlib.h>
node * t9_solve (node *head)
{
int cmp_last, cmp_next;
node *last, *curr, *next,
*deleted;
last=next=deleted=NULL;
cmp_last=cmp_next=0;
curr = head;
if (!curr)
return NULL;
for (next = curr->next; next; next = curr->next)
{
if (deleted)
cmp_last = strcmp(curr->string, deleted->string);
else if (!last)
cmp_last = 1;
else
cmp_last = strcmp(curr->string, last->string);
cmp_next = strcmp(curr->string, next->string); // Как происходит обращение в память для структур?
if ((cmp_last >= 0) && (cmp_next >= 0))
{
if (deleted)
delete_node(deleted);
deleted = curr;
if (last) {
last->next = next;
curr = last;
} else {
head = next;
curr = NULL;
}
} else
{
if (deleted)
delete_node(deleted);
deleted = NULL;
}
last = curr;
curr = next;
}
if (deleted)
cmp_last = strcmp(curr->string, deleted->string);
else if (!last)
cmp_last = 1;
else
cmp_last = strcmp(curr->string, last->string);
if (cmp_last >= 0)
{
delete_node(curr);
if (last)
last->next = NULL;
else
head = NULL;
curr = NULL;
}
if (deleted)
delete_node(deleted);
return head;
}

View file

@ -0,0 +1,8 @@
#ifndef SOLVE_H
#define SOLVE_H
#include "node.h"
node * t9_solve (node *head);
#endif

View file

@ -0,0 +1,56 @@
#include "solve_10.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;
}

View file

@ -0,0 +1,8 @@
#ifndef SOLVE_H
#define SOLVE_H
#include "node.h"
node * t10_solve (node *head);
#endif

View file

@ -19,7 +19,7 @@ rm -f "$outlog" "$errlog"
echo "Тест запущен..."
for npr in {1..7} ; do
for npr in {1..10} ; do
num=$(printf "%02d" "$npr")
prog="a$num.out"
if [ -f $prog ]; then