Added last three homeworks
This commit is contained in:
parent
f7b2367bc4
commit
0e3d948c9f
363 changed files with 18214 additions and 0 deletions
16
2025.02.21/1Ex/Makefile
Normal file
16
2025.02.21/1Ex/Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a01.exe: main.o array.o solve.o
|
||||
gcc main.o solve.o array.o -o a01.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
75
2025.02.21/1Ex/array.c
Normal file
75
2025.02.21/1Ex/array.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int delete_string(char ** a, int n, int i)
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = a[n - 1];
|
||||
a[n - 1] = 0;
|
||||
return n - 1;
|
||||
}
|
||||
|
16
2025.02.21/1Ex/array.h
Normal file
16
2025.02.21/1Ex/array.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef ARRAY
|
||||
#define ARRAY
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
io_status read_array(char *a[], int n, const char * name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int delete_string(char ** a, int n, int i);
|
||||
|
||||
#endif
|
12
2025.02.21/1Ex/io_status.h
Normal file
12
2025.02.21/1Ex/io_status.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
63
2025.02.21/1Ex/main.c
Normal file
63
2025.02.21/1Ex/main.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 1;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status ret;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
ret = read_array(a, n, name);
|
||||
do {
|
||||
switch (ret) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
ret = t1_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, ret, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, ret, t);
|
||||
free_array(a, ret);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
16
2025.02.21/1Ex/solve.c
Normal file
16
2025.02.21/1Ex/solve.c
Normal file
|
@ -0,0 +1,16 @@
|
|||
#include "solve.h"
|
||||
|
||||
int t1_solve(char ** a, int n, char *s) {
|
||||
int i, j;
|
||||
for (i = 0, j = 0; i < n; ++i) {
|
||||
if (strcmp(a[i], s) >= 0)
|
||||
{
|
||||
if (i != j) a[j] = a[i];
|
||||
j++;
|
||||
} else free(a[i]);
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
10
2025.02.21/1Ex/solve.h
Normal file
10
2025.02.21/1Ex/solve.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef SOLVE
|
||||
#define SOLVE
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "array.h"
|
||||
|
||||
int t1_solve(char **arr, int n, char *s);
|
||||
|
||||
#endif
|
3
2025.02.21/1Ex/t.txt
Normal file
3
2025.02.21/1Ex/t.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
abcdef
|
||||
abcefg
|
||||
zyxwvf
|
16
2025.02.21/2Ex/Makefile
Normal file
16
2025.02.21/2Ex/Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a02.exe: main.o array.o solve.o
|
||||
gcc main.o solve.o array.o -o a02.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
75
2025.02.21/2Ex/array.c
Normal file
75
2025.02.21/2Ex/array.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int delete_string(char ** a, int n, int i)
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = a[n - 1];
|
||||
a[n - 1] = 0;
|
||||
return n - 1;
|
||||
}
|
||||
|
16
2025.02.21/2Ex/array.h
Normal file
16
2025.02.21/2Ex/array.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef ARRAY
|
||||
#define ARRAY
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
io_status read_array(char *a[], int n, const char * name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int delete_string(char ** a, int n, int i);
|
||||
|
||||
#endif
|
12
2025.02.21/2Ex/io_status.h
Normal file
12
2025.02.21/2Ex/io_status.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
63
2025.02.21/2Ex/main.c
Normal file
63
2025.02.21/2Ex/main.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 2;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t2_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
18
2025.02.21/2Ex/solve.c
Normal file
18
2025.02.21/2Ex/solve.c
Normal file
|
@ -0,0 +1,18 @@
|
|||
#include "solve.h"
|
||||
|
||||
int t2_solve(char ** a, int n, char *s) {
|
||||
int i, j;
|
||||
for (i = 0, j = 0; i < n; ++i) {
|
||||
if (!strstr(s, a[i])) {
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
} else free(a[i]);
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
10
2025.02.21/2Ex/solve.h
Normal file
10
2025.02.21/2Ex/solve.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef SOLVE
|
||||
#define SOLVE
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "array.h"
|
||||
|
||||
int t2_solve(char **arr, int n, char *s);
|
||||
|
||||
#endif
|
5
2025.02.21/2Ex/t.txt
Normal file
5
2025.02.21/2Ex/t.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
hello
|
||||
friends
|
||||
we
|
||||
are
|
||||
here
|
16
2025.02.21/3Ex/Makefile
Normal file
16
2025.02.21/3Ex/Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a03.exe: main.o array.o solve.o
|
||||
gcc main.o solve.o array.o -o a03.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
75
2025.02.21/3Ex/array.c
Normal file
75
2025.02.21/3Ex/array.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int delete_string(char ** a, int n, int i)
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = a[n - 1];
|
||||
a[n - 1] = 0;
|
||||
return n - 1;
|
||||
}
|
||||
|
16
2025.02.21/3Ex/array.h
Normal file
16
2025.02.21/3Ex/array.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef ARRAY
|
||||
#define ARRAY
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
io_status read_array(char *a[], int n, const char * name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int delete_string(char ** a, int n, int i);
|
||||
|
||||
#endif
|
12
2025.02.21/3Ex/io_status.h
Normal file
12
2025.02.21/3Ex/io_status.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
63
2025.02.21/3Ex/main.c
Normal file
63
2025.02.21/3Ex/main.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 3;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t3_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
20
2025.02.21/3Ex/solve.c
Normal file
20
2025.02.21/3Ex/solve.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include "solve.h"
|
||||
|
||||
int t3_solve(char ** a, int n, char *s) {
|
||||
int i, j;
|
||||
for (i = 0, j = 0; i < n; ++i) {
|
||||
if (strpbrk(s, a[i])) {
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
} else {
|
||||
free(a[i]);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
10
2025.02.21/3Ex/solve.h
Normal file
10
2025.02.21/3Ex/solve.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef SOLVE
|
||||
#define SOLVE
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "array.h"
|
||||
|
||||
int t3_solve(char **arr, int n, char *s);
|
||||
|
||||
#endif
|
5
2025.02.21/3Ex/t.txt
Normal file
5
2025.02.21/3Ex/t.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
hello
|
||||
friends
|
||||
we
|
||||
are
|
||||
here
|
16
2025.02.21/4Ex/Makefile
Normal file
16
2025.02.21/4Ex/Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a04.exe: main.o array.o solve.o
|
||||
gcc main.o solve.o array.o -o a04.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
75
2025.02.21/4Ex/array.c
Normal file
75
2025.02.21/4Ex/array.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int delete_string(char ** a, int n, int i)
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = a[n - 1];
|
||||
a[n - 1] = 0;
|
||||
return n - 1;
|
||||
}
|
||||
|
16
2025.02.21/4Ex/array.h
Normal file
16
2025.02.21/4Ex/array.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef ARRAY
|
||||
#define ARRAY
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
io_status read_array(char *a[], int n, const char * name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int delete_string(char ** a, int n, int i);
|
||||
|
||||
#endif
|
12
2025.02.21/4Ex/io_status.h
Normal file
12
2025.02.21/4Ex/io_status.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
63
2025.02.21/4Ex/main.c
Normal file
63
2025.02.21/4Ex/main.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 4;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t4_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
29
2025.02.21/4Ex/solve.c
Normal file
29
2025.02.21/4Ex/solve.c
Normal file
|
@ -0,0 +1,29 @@
|
|||
#include "solve.h"
|
||||
|
||||
int t4_solve(char ** a, int n, char *s) {
|
||||
size_t len_s = strlen(s);
|
||||
int i, j, k;
|
||||
for (i = 0, j = 0, k = 0; i < n; ++i) {
|
||||
if (len_s <= strlen(a[i])) {
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
k = 0;
|
||||
} else {
|
||||
if (k == 0) {
|
||||
if (i != j)
|
||||
a[j] = a[i];
|
||||
j++;
|
||||
} else {
|
||||
free(a[i]);
|
||||
}
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
10
2025.02.21/4Ex/solve.h
Normal file
10
2025.02.21/4Ex/solve.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef SOLVE
|
||||
#define SOLVE
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "array.h"
|
||||
|
||||
int t4_solve(char **arr, int n, char *s);
|
||||
|
||||
#endif
|
11
2025.02.21/4Ex/t.txt
Normal file
11
2025.02.21/4Ex/t.txt
Normal file
|
@ -0,0 +1,11 @@
|
|||
1
|
||||
22
|
||||
333
|
||||
4444
|
||||
55555
|
||||
666666
|
||||
333
|
||||
22
|
||||
1
|
||||
7777777
|
||||
22
|
16
2025.02.21/5Ex/Makefile
Normal file
16
2025.02.21/5Ex/Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a05.exe: main.o array.o solve.o
|
||||
gcc main.o solve.o array.o -o a05.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
75
2025.02.21/5Ex/array.c
Normal file
75
2025.02.21/5Ex/array.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int delete_string(char ** a, int n, int i)
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = a[n - 1];
|
||||
a[n - 1] = 0;
|
||||
return n - 1;
|
||||
}
|
||||
|
16
2025.02.21/5Ex/array.h
Normal file
16
2025.02.21/5Ex/array.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef ARRAY
|
||||
#define ARRAY
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
io_status read_array(char *a[], int n, const char * name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int delete_string(char ** a, int n, int i);
|
||||
|
||||
#endif
|
12
2025.02.21/5Ex/io_status.h
Normal file
12
2025.02.21/5Ex/io_status.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
63
2025.02.21/5Ex/main.c
Normal file
63
2025.02.21/5Ex/main.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 5;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t5_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
44
2025.02.21/5Ex/solve.c
Normal file
44
2025.02.21/5Ex/solve.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include "solve.h"
|
||||
|
||||
int t5_solve(char ** a, int n, char *s) {
|
||||
int i, j, k;
|
||||
for (i = 0, j = 0, k = 0; i < n; ++i) {
|
||||
if (!check(a[i], s)) {
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
k = 0;
|
||||
} else {
|
||||
if (k == 0) {
|
||||
if (i != j)
|
||||
a[j] = a[i];
|
||||
j++;
|
||||
} else {
|
||||
free(a[i]);
|
||||
}
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
|
||||
bool check(char *a, char *s) {
|
||||
int i;
|
||||
int len_s = (int)strlen(s);
|
||||
int len_a = (int)strlen(a);
|
||||
int diff = len_a - len_s;
|
||||
|
||||
if (diff < 0) return false;
|
||||
|
||||
for (i = (len_a - 1); i > (diff - 1); --i) {
|
||||
if (a[i] != s[i - diff]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
12
2025.02.21/5Ex/solve.h
Normal file
12
2025.02.21/5Ex/solve.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef SOLVE
|
||||
#define SOLVE
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "array.h"
|
||||
|
||||
int t5_solve(char **arr, int n, char *s);
|
||||
bool check(char *a, char *s);
|
||||
|
||||
#endif
|
3
2025.02.21/5Ex/t.txt
Normal file
3
2025.02.21/5Ex/t.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
xyz
|
||||
pqr
|
||||
zzz
|
16
2025.02.21/6Ex/Makefile
Normal file
16
2025.02.21/6Ex/Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a06.exe: main.o array.o solve.o
|
||||
gcc main.o solve.o array.o -o a06.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
75
2025.02.21/6Ex/array.c
Normal file
75
2025.02.21/6Ex/array.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int delete_string(char ** a, int n, int i)
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = a[n - 1];
|
||||
a[n - 1] = 0;
|
||||
return n - 1;
|
||||
}
|
||||
|
16
2025.02.21/6Ex/array.h
Normal file
16
2025.02.21/6Ex/array.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef ARRAY
|
||||
#define ARRAY
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
io_status read_array(char *a[], int n, const char * name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int delete_string(char ** a, int n, int i);
|
||||
|
||||
#endif
|
12
2025.02.21/6Ex/io_status.h
Normal file
12
2025.02.21/6Ex/io_status.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
63
2025.02.21/6Ex/main.c
Normal file
63
2025.02.21/6Ex/main.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 6;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t6_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
39
2025.02.21/6Ex/solve.c
Normal file
39
2025.02.21/6Ex/solve.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include "solve.h"
|
||||
|
||||
int t6_solve(char ** a, int n, char *s) {
|
||||
int i, j, k;
|
||||
for (i = 0, j = 0, k = 0; i < n; ++i) {
|
||||
if (strpbrk(a[i], s)) {
|
||||
if (k > 1)
|
||||
{
|
||||
for (int x = 2; x < k; x++) free(a[i - x]);
|
||||
a[j++] = a[i - 1];
|
||||
}
|
||||
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
k = 0;
|
||||
} else {
|
||||
if (k == 0) {
|
||||
if (i != j)
|
||||
a[j] = a[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
if (k > 1)
|
||||
{
|
||||
for (int x = 2; x < k; x++) free(a[i - x]);
|
||||
a[j++] = a[i - 1];
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
11
2025.02.21/6Ex/solve.h
Normal file
11
2025.02.21/6Ex/solve.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef SOLVE
|
||||
#define SOLVE
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "array.h"
|
||||
|
||||
int t6_solve(char **arr, int n, char *s);
|
||||
|
||||
#endif
|
3
2025.02.21/6Ex/t.txt
Normal file
3
2025.02.21/6Ex/t.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
xyz
|
||||
pqr
|
||||
zzz
|
16
2025.02.21/7Ex/Makefile
Normal file
16
2025.02.21/7Ex/Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a07.exe: main.o array.o solve.o
|
||||
gcc main.o solve.o array.o -o a07.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
75
2025.02.21/7Ex/array.c
Normal file
75
2025.02.21/7Ex/array.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int delete_string(char ** a, int n, int i)
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = a[n - 1];
|
||||
a[n - 1] = 0;
|
||||
return n - 1;
|
||||
}
|
||||
|
16
2025.02.21/7Ex/array.h
Normal file
16
2025.02.21/7Ex/array.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef ARRAY
|
||||
#define ARRAY
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
io_status read_array(char *a[], int n, const char * name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int delete_string(char ** a, int n, int i);
|
||||
|
||||
#endif
|
12
2025.02.21/7Ex/io_status.h
Normal file
12
2025.02.21/7Ex/io_status.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
63
2025.02.21/7Ex/main.c
Normal file
63
2025.02.21/7Ex/main.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 7;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t7_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
39
2025.02.21/7Ex/solve.c
Normal file
39
2025.02.21/7Ex/solve.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include "solve.h"
|
||||
|
||||
int t7_solve(char ** a, int n, char *s) {
|
||||
bool fl_lst = false, fl_cur = false, fl_nxt = false;
|
||||
int i, j;
|
||||
|
||||
if (strcmp(a[0], s) < 0) fl_cur = true;
|
||||
|
||||
for (i = 1, j = 0; i < n; ++i) {
|
||||
if (strcmp(a[i], s) < 0) {
|
||||
fl_lst = true;
|
||||
fl_nxt = true;
|
||||
}
|
||||
|
||||
if (!fl_lst) {
|
||||
if (j != (i-1)) {
|
||||
a[j] = a[i-1];
|
||||
}
|
||||
j++;
|
||||
} else {
|
||||
free(a[i-1]);
|
||||
}
|
||||
|
||||
fl_lst = fl_cur;
|
||||
fl_cur = fl_nxt;
|
||||
fl_nxt = false;
|
||||
}
|
||||
|
||||
if (fl_lst)
|
||||
{
|
||||
free(a[i-1]);
|
||||
} else {
|
||||
a[j++] = a[i-1];
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
11
2025.02.21/7Ex/solve.h
Normal file
11
2025.02.21/7Ex/solve.h
Normal file
|
@ -0,0 +1,11 @@
|
|||
#ifndef SOLVE
|
||||
#define SOLVE
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "array.h"
|
||||
|
||||
int t7_solve(char **arr, int n, char *s);
|
||||
|
||||
#endif
|
3
2025.02.21/7Ex/t.txt
Normal file
3
2025.02.21/7Ex/t.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
apple
|
||||
banana
|
||||
coffee
|
16
2025.02.21/8Ex/Makefile
Normal file
16
2025.02.21/8Ex/Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
a08.exe: main.o array.o solve.o
|
||||
gcc main.o solve.o array.o -o a08.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(FLAGS) -c solve.c
|
||||
|
||||
array.o: array.c
|
||||
gcc $(CFLAGS) -c array.c
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
75
2025.02.21/8Ex/array.c
Normal file
75
2025.02.21/8Ex/array.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
int delete_string(char ** a, int n, int i)
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = a[n - 1];
|
||||
a[n - 1] = 0;
|
||||
return n - 1;
|
||||
}
|
||||
|
16
2025.02.21/8Ex/array.h
Normal file
16
2025.02.21/8Ex/array.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef ARRAY
|
||||
#define ARRAY
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
io_status read_array(char *a[], int n, const char * name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
int delete_string(char ** a, int n, int i);
|
||||
|
||||
#endif
|
12
2025.02.21/8Ex/io_status.h
Normal file
12
2025.02.21/8Ex/io_status.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
63
2025.02.21/8Ex/main.c
Normal file
63
2025.02.21/8Ex/main.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 8;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t8_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
77
2025.02.21/8Ex/solve.c
Normal file
77
2025.02.21/8Ex/solve.c
Normal file
|
@ -0,0 +1,77 @@
|
|||
#include "solve.h"
|
||||
|
||||
#define INF 97
|
||||
#define EXTR 122
|
||||
#define TO_UPPER 32
|
||||
|
||||
#define LOWER(a) (INF <= a && a <= EXTR)
|
||||
|
||||
int t8_solve(char ** a, int n, char *s) {
|
||||
bool fl_lst = false, fl_cur = false, fl_nxt = false;
|
||||
int i, j;
|
||||
|
||||
fl_cur = is_inside(a[0], s);
|
||||
|
||||
for (i = 1, j = 0; i < n; ++i) {
|
||||
if (is_inside(a[i], s)) {
|
||||
fl_lst = true;
|
||||
fl_nxt = true;
|
||||
}
|
||||
|
||||
if (!fl_lst) {
|
||||
if (j != (i-1)) {
|
||||
a[j] = a[i-1];
|
||||
}
|
||||
j++;
|
||||
} else {
|
||||
free(a[i-1]);
|
||||
}
|
||||
|
||||
fl_lst = fl_cur;
|
||||
fl_cur = fl_nxt;
|
||||
fl_nxt = false;
|
||||
}
|
||||
|
||||
if (fl_lst)
|
||||
{
|
||||
free(a[i-1]);
|
||||
} else {
|
||||
a[j++] = a[i-1];
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
bool is_inside(char *a, char *s)
|
||||
{
|
||||
int len_s = (int)strlen(s);
|
||||
int len_a = (int)strlen(a);
|
||||
int i, j;
|
||||
|
||||
if (len_s < len_a) return false;
|
||||
|
||||
for (i = 0; i < len_s; ++i) {
|
||||
if (!ccmp(a[0], s[i])) {
|
||||
if (len_a > (len_s-len_a)) return false;
|
||||
|
||||
for (j = 0; j < len_a; ++j) {
|
||||
if (ccmp(a[j], s[i + j]) != 0) break;
|
||||
}
|
||||
|
||||
if (j == len_a) return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int ccmp(char a, char b) {
|
||||
if (LOWER(a)) a -= TO_UPPER;
|
||||
if (LOWER(b)) b -= TO_UPPER;
|
||||
|
||||
if (a < b) return -1;
|
||||
if (a > b) return 1;
|
||||
return 0;
|
||||
}
|
13
2025.02.21/8Ex/solve.h
Normal file
13
2025.02.21/8Ex/solve.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#ifndef SOLVE
|
||||
#define SOLVE
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "array.h"
|
||||
|
||||
int t8_solve(char **arr, int n, char *s);
|
||||
bool is_inside(char *a, char *s);
|
||||
int ccmp(char a, char s);
|
||||
|
||||
#endif
|
9
2025.02.21/8Ex/t.txt
Normal file
9
2025.02.21/8Ex/t.txt
Normal file
|
@ -0,0 +1,9 @@
|
|||
anysome
|
||||
hello
|
||||
anybody
|
||||
here
|
||||
someone?!
|
||||
somehelp
|
||||
I
|
||||
need
|
||||
cosomello
|
68
2025.02.21/Example/array.c
Normal file
68
2025.02.21/Example/array.c
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
delete_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == "\n")
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
delete_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void delete_array(char ** a. int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
||||
|
||||
|
13
2025.02.21/Example/io_status.h
Normal file
13
2025.02.21/Example/io_status.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
#typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
READ_ERROR,
|
||||
SIZE_ERROR,
|
||||
FORMAT_ERROR,
|
||||
MEMORY_ERROR
|
||||
} io_status;
|
||||
|
||||
#endif
|
52
2025.02.21/Example/main.c
Normal file
52
2025.02.21/Example/main.c
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include <stdio.h>
|
||||
#include "io_status.h"
|
||||
|
||||
|
||||
int main(int argc, char * srgv[]) {
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char ** a = 0;
|
||||
io_status ret;
|
||||
|
||||
if (!(argc == 4 && fscanf(argv[1], "%d", &n) == 1 && fscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return MEMORY_ERROR;
|
||||
}
|
||||
ret = read_array(a, n, name);
|
||||
do {
|
||||
switch (ret) {
|
||||
case SUCCESS;
|
||||
continue;
|
||||
case ERROR_OPEN;
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ;
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM;
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
// TODO: Замер времени
|
||||
l = solve(a, n);
|
||||
// TODO: Замер времени
|
||||
print_array(a, l, m);
|
||||
delete_array(a, l);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
BIN
2025.02.21/Krivoruchenko_SK.zip
Normal file
BIN
2025.02.21/Krivoruchenko_SK.zip
Normal file
Binary file not shown.
13
2025.02.21/Linux/Makefile
Normal file
13
2025.02.21/Linux/Makefile
Normal file
|
@ -0,0 +1,13 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
%.out: %.o solve.o
|
||||
gcc $(FLAGS) $^ -o $@
|
||||
%.o: %.c
|
||||
gcc -c $(FLAGS) $<
|
||||
|
||||
all: a01.out a02.out a03.out a04.out a05.out a06.out a07.out a08.out
|
||||
|
||||
solve.o: solve.c solve.h
|
||||
|
||||
clean:
|
||||
rm -f *.o *.out
|
63
2025.02.21/Linux/a01.c
Normal file
63
2025.02.21/Linux/a01.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 1;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status ret;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
ret = read_array(a, n, name);
|
||||
do {
|
||||
switch (ret) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
ret = t1_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, ret, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, ret, t);
|
||||
free_array(a, ret);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/Linux/a02.c
Normal file
63
2025.02.21/Linux/a02.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 2;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t2_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/Linux/a03.c
Normal file
63
2025.02.21/Linux/a03.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 3;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t3_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/Linux/a04.c
Normal file
63
2025.02.21/Linux/a04.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 4;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t4_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/Linux/a05.c
Normal file
63
2025.02.21/Linux/a05.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 5;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t5_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/Linux/a06.c
Normal file
63
2025.02.21/Linux/a06.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 6;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t6_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/Linux/a07.c
Normal file
63
2025.02.21/Linux/a07.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 7;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t7_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/Linux/a08.c
Normal file
63
2025.02.21/Linux/a08.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 8;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t8_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
66
2025.02.21/Linux/array.c
Normal file
66
2025.02.21/Linux/array.c
Normal file
|
@ -0,0 +1,66 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
15
2025.02.21/Linux/array.h
Normal file
15
2025.02.21/Linux/array.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef ARRAY
|
||||
#define ARRAY
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
io_status read_array(char *a[], int n, const char * name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
|
||||
#endif
|
12
2025.02.21/Linux/io_status.h
Normal file
12
2025.02.21/Linux/io_status.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
269
2025.02.21/Linux/solve.c
Normal file
269
2025.02.21/Linux/solve.c
Normal file
|
@ -0,0 +1,269 @@
|
|||
#include "solve.h"
|
||||
|
||||
int t1_solve(char ** a, int n, char *s) {
|
||||
int i, j;
|
||||
for (i = 0, j = 0; i < n; ++i) {
|
||||
if (strcmp(a[i], s) >= 0)
|
||||
{
|
||||
if (i != j) a[j] = a[i];
|
||||
j++;
|
||||
} else free(a[i]);
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
int t2_solve(char ** a, int n, char *s) {
|
||||
int i, j;
|
||||
for (i = 0, j = 0; i < n; ++i) {
|
||||
if (!strstr(s, a[i])) {
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
} else free(a[i]);
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
int t3_solve(char ** a, int n, char *s) {
|
||||
int i, j;
|
||||
for (i = 0, j = 0; i < n; ++i) {
|
||||
if (strpbrk(s, a[i])) {
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
} else {
|
||||
free(a[i]);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
int t4_solve(char ** a, int n, char *s) {
|
||||
size_t len_s = strlen(s);
|
||||
int i, j, k;
|
||||
for (i = 0, j = 0, k = 0; i < n; ++i) {
|
||||
if (len_s <= strlen(a[i])) {
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
k = 0;
|
||||
} else {
|
||||
if (k == 0) {
|
||||
if (i != j)
|
||||
a[j] = a[i];
|
||||
j++;
|
||||
} else {
|
||||
free(a[i]);
|
||||
}
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
int t5_solve(char ** a, int n, char *s) {
|
||||
int i, j, k;
|
||||
for (i = 0, j = 0, k = 0; i < n; ++i) {
|
||||
if (!check(a[i], s)) {
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
k = 0;
|
||||
} else {
|
||||
if (k == 0) {
|
||||
if (i != j)
|
||||
a[j] = a[i];
|
||||
j++;
|
||||
} else {
|
||||
free(a[i]);
|
||||
}
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
|
||||
bool check(char *a, char *s) {
|
||||
int i;
|
||||
int len_s = (int)strlen(s);
|
||||
int len_a = (int)strlen(a);
|
||||
int diff = len_a - len_s;
|
||||
|
||||
if (diff < 0) return false;
|
||||
|
||||
for (i = (len_a - 1); i > (diff - 1); --i) {
|
||||
if (a[i] != s[i - diff]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int t6_solve(char ** a, int n, char *s) {
|
||||
int i, j, k;
|
||||
for (i = 0, j = 0, k = 0; i < n; ++i) {
|
||||
if (strpbrk(a[i], s)) {
|
||||
if (k > 1)
|
||||
{
|
||||
for (int x = 2; x < k; x++) free(a[i - x]);
|
||||
a[j++] = a[i - 1];
|
||||
}
|
||||
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
k = 0;
|
||||
} else {
|
||||
if (k == 0) {
|
||||
if (i != j)
|
||||
a[j] = a[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
if (k > 1)
|
||||
{
|
||||
for (int x = 2; x < k; x++) free(a[i - x]);
|
||||
a[j++] = a[i - 1];
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
int t7_solve(char ** a, int n, char *s) {
|
||||
bool fl_lst = false, fl_cur = false, fl_nxt = false;
|
||||
int i, j;
|
||||
|
||||
if (strcmp(a[0], s) < 0) fl_cur = true;
|
||||
|
||||
for (i = 1, j = 0; i < n; ++i) {
|
||||
if (strcmp(a[i], s) < 0) {
|
||||
fl_lst = true;
|
||||
fl_nxt = true;
|
||||
}
|
||||
|
||||
if (!fl_lst) {
|
||||
if (j != (i-1)) {
|
||||
a[j] = a[i-1];
|
||||
}
|
||||
j++;
|
||||
} else {
|
||||
free(a[i-1]);
|
||||
}
|
||||
|
||||
fl_lst = fl_cur;
|
||||
fl_cur = fl_nxt;
|
||||
fl_nxt = false;
|
||||
}
|
||||
|
||||
if (fl_lst)
|
||||
{
|
||||
free(a[i-1]);
|
||||
} else {
|
||||
a[j++] = a[i-1];
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
|
||||
int t8_solve(char ** a, int n, char *s) {
|
||||
bool fl_lst = false, fl_cur = false, fl_nxt = false;
|
||||
int i, j;
|
||||
|
||||
fl_cur = is_inside(a[0], s);
|
||||
|
||||
for (i = 1, j = 0; i < n; ++i) {
|
||||
if (is_inside(a[i], s)) {
|
||||
fl_lst = true;
|
||||
fl_nxt = true;
|
||||
}
|
||||
|
||||
if (!fl_lst) {
|
||||
if (j != (i-1)) {
|
||||
a[j] = a[i-1];
|
||||
}
|
||||
j++;
|
||||
} else {
|
||||
free(a[i-1]);
|
||||
}
|
||||
|
||||
fl_lst = fl_cur;
|
||||
fl_cur = fl_nxt;
|
||||
fl_nxt = false;
|
||||
}
|
||||
|
||||
if (fl_lst)
|
||||
{
|
||||
free(a[i-1]);
|
||||
} else {
|
||||
a[j++] = a[i-1];
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
bool is_inside(char *a, char *s)
|
||||
{
|
||||
int len_s = (int)strlen(s);
|
||||
int len_a = (int)strlen(a);
|
||||
int i, j;
|
||||
|
||||
if (len_s < len_a) return false;
|
||||
|
||||
for (i = 0; i < len_s; ++i) {
|
||||
if (!ccmp(a[0], s[i])) {
|
||||
if (len_a > (len_s-len_a)) return false;
|
||||
|
||||
for (j = 0; j < len_a; ++j) {
|
||||
if (ccmp(a[j], s[i + j]) != 0) break;
|
||||
}
|
||||
|
||||
if (j == len_a) return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int ccmp(char a, char b) {
|
||||
if (LOWER(a)) a -= TO_UPPER;
|
||||
if (LOWER(b)) b -= TO_UPPER;
|
||||
|
||||
if (a < b) return -1;
|
||||
if (a > b) return 1;
|
||||
return 0;
|
||||
}
|
27
2025.02.21/Linux/solve.h
Normal file
27
2025.02.21/Linux/solve.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
#ifndef SOLVE
|
||||
#define SOLVE
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "array.h"
|
||||
|
||||
#define INF 97
|
||||
#define EXTR 122
|
||||
#define TO_UPPER 32
|
||||
|
||||
#define LOWER(a) (INF <= a && a <= EXTR)
|
||||
|
||||
int t1_solve(char **arr, int n, char *s);
|
||||
int t2_solve(char **arr, int n, char *s);
|
||||
int t3_solve(char **arr, int n, char *s);
|
||||
int t4_solve(char **arr, int n, char *s);
|
||||
int t5_solve(char **arr, int n, char *s);
|
||||
bool check(char *a, char *s);
|
||||
int t6_solve(char **arr, int n, char *s);
|
||||
int t7_solve(char **arr, int n, char *s);
|
||||
int t8_solve(char **arr, int n, char *s);
|
||||
bool is_inside(char *a, char *s);
|
||||
int ccmp(char a, char s);
|
||||
|
||||
#endif
|
14
2025.02.21/Makefile
Normal file
14
2025.02.21/Makefile
Normal file
|
@ -0,0 +1,14 @@
|
|||
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
|
||||
|
||||
%.out: %.o solve.o array.o
|
||||
gcc $(FLAGS) $^ -o $@
|
||||
%.o: %.c
|
||||
gcc -c $(FLAGS) $<
|
||||
|
||||
all: a01.out a02.out a03.out a04.out a05.out a06.out a07.out a08.out
|
||||
|
||||
solve.o: solve.c solve.h
|
||||
array.o: array.c array.h
|
||||
|
||||
clean:
|
||||
rm -f *.o *.out
|
BIN
2025.02.21/Tasks02.pdf
Normal file
BIN
2025.02.21/Tasks02.pdf
Normal file
Binary file not shown.
63
2025.02.21/a01.c
Normal file
63
2025.02.21/a01.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 1;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status ret;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
ret = read_array(a, n, name);
|
||||
do {
|
||||
switch (ret) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
ret = t1_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, ret, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, ret, t);
|
||||
free_array(a, ret);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/a02.c
Normal file
63
2025.02.21/a02.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 2;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t2_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/a03.c
Normal file
63
2025.02.21/a03.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 3;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t3_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/a04.c
Normal file
63
2025.02.21/a04.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 4;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t4_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/a05.c
Normal file
63
2025.02.21/a05.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 5;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t5_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/a06.c
Normal file
63
2025.02.21/a06.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 6;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t6_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/a07.c
Normal file
63
2025.02.21/a07.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 7;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t7_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
63
2025.02.21/a08.c
Normal file
63
2025.02.21/a08.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
#include "solve.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int task = 8;
|
||||
int n = 0, m = 0;
|
||||
char * name = 0;
|
||||
char * s = 0;
|
||||
char ** a = 0;
|
||||
io_status res;
|
||||
double t;
|
||||
|
||||
if (!(argc == 5 && sscanf(argv[1], "%d", &n) == 1 && sscanf(argv[2], "%d", &m) == 1))
|
||||
{
|
||||
printf("Usage: %s n m file string\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
name = argv[3];
|
||||
s = argv[4];
|
||||
a = (char **)malloc(n * sizeof(char **));
|
||||
if (!a)
|
||||
{
|
||||
printf("Can not allocate array len %d\n", n);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
res = read_array(a, n, name);
|
||||
do {
|
||||
switch (res) {
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Can not open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Can not read %s\n", name);
|
||||
break;
|
||||
case ERROR_MEM:
|
||||
printf("Not enough memory\n");
|
||||
break;
|
||||
}
|
||||
|
||||
free(a);
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
print_array(a, n, m);
|
||||
t = clock();
|
||||
res = t8_solve(a, n, s);
|
||||
t = (clock() - t);
|
||||
printf("New array:\n");
|
||||
print_array(a, res, m);
|
||||
printf("%s : Task = %d Result = %d Elapsed = %.2f\n",
|
||||
argv[0], task, res, t);
|
||||
free_array(a, res);
|
||||
free(a);
|
||||
a = 0;
|
||||
return 0;
|
||||
}
|
66
2025.02.21/array.c
Normal file
66
2025.02.21/array.c
Normal file
|
@ -0,0 +1,66 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "io_status.h"
|
||||
#include "array.h"
|
||||
|
||||
|
||||
io_status read_array(char* a[], int n, const char * name)
|
||||
{
|
||||
char buf[LEN] = {0};
|
||||
FILE *fp = 0;
|
||||
int i, j;
|
||||
|
||||
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||
for (i = 0; i < n; i++) {
|
||||
if (!fgets(buf, sizeof(buf), fp))
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_READ;
|
||||
}
|
||||
|
||||
for (j = 0; buf[j]; j++)
|
||||
{
|
||||
if (buf[j] == '\n')
|
||||
{
|
||||
buf[j] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
a[i] = (char *)malloc((j+1) * sizeof(char));
|
||||
if (!a[i])
|
||||
{
|
||||
fclose(fp);
|
||||
free_array(a, i);
|
||||
return ERROR_MEM;
|
||||
}
|
||||
|
||||
strcpy(a[i], buf);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void free_array(char ** a, int n)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < n; ++i)
|
||||
{
|
||||
if (a[i])
|
||||
{
|
||||
free(a[i]);
|
||||
a[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_array(char ** a, int n, int m)
|
||||
{
|
||||
int l = (n > m ? m : n);
|
||||
int i;
|
||||
for (i = 0; i < l; ++i) printf("%s\n", a[i]);
|
||||
}
|
15
2025.02.21/array.h
Normal file
15
2025.02.21/array.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef ARRAY
|
||||
#define ARRAY
|
||||
|
||||
#define LEN 1234
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "io_status.h"
|
||||
|
||||
io_status read_array(char *a[], int n, const char * name);
|
||||
void free_array(char **a, int n);
|
||||
void print_array(char **a, int n, int m);
|
||||
|
||||
#endif
|
12
2025.02.21/io_status.h
Normal file
12
2025.02.21/io_status.h
Normal file
|
@ -0,0 +1,12 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ,
|
||||
ERROR_MEM
|
||||
} io_status;
|
||||
|
||||
#endif
|
269
2025.02.21/solve.c
Normal file
269
2025.02.21/solve.c
Normal file
|
@ -0,0 +1,269 @@
|
|||
#include "solve.h"
|
||||
|
||||
int t1_solve(char ** a, int n, char *s) {
|
||||
int i, j;
|
||||
for (i = 0, j = 0; i < n; ++i) {
|
||||
if (strcmp(a[i], s) >= 0)
|
||||
{
|
||||
if (i != j) a[j] = a[i];
|
||||
j++;
|
||||
} else free(a[i]);
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
int t2_solve(char ** a, int n, char *s) {
|
||||
int i, j;
|
||||
for (i = 0, j = 0; i < n; ++i) {
|
||||
if (!strstr(s, a[i])) {
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
} else free(a[i]);
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
int t3_solve(char ** a, int n, char *s) {
|
||||
int i, j;
|
||||
for (i = 0, j = 0; i < n; ++i) {
|
||||
if (strpbrk(s, a[i])) {
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
} else {
|
||||
free(a[i]);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
int t4_solve(char ** a, int n, char *s) {
|
||||
size_t len_s = strlen(s);
|
||||
int i, j, k;
|
||||
for (i = 0, j = 0, k = 0; i < n; ++i) {
|
||||
if (len_s <= strlen(a[i])) {
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
k = 0;
|
||||
} else {
|
||||
if (k == 0) {
|
||||
if (i != j)
|
||||
a[j] = a[i];
|
||||
j++;
|
||||
} else {
|
||||
free(a[i]);
|
||||
}
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
int t5_solve(char ** a, int n, char *s) {
|
||||
int i, j, k;
|
||||
for (i = 0, j = 0, k = 0; i < n; ++i) {
|
||||
if (!check(a[i], s)) {
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
k = 0;
|
||||
} else {
|
||||
if (k == 0) {
|
||||
if (i != j)
|
||||
a[j] = a[i];
|
||||
j++;
|
||||
} else {
|
||||
free(a[i]);
|
||||
}
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
|
||||
bool check(char *a, char *s) {
|
||||
int i;
|
||||
int len_s = (int)strlen(s);
|
||||
int len_a = (int)strlen(a);
|
||||
int diff = len_a - len_s;
|
||||
|
||||
if (diff < 0) return false;
|
||||
|
||||
for (i = (len_a - 1); i > (diff - 1); --i) {
|
||||
if (a[i] != s[i - diff]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int t6_solve(char ** a, int n, char *s) {
|
||||
int i, j, k;
|
||||
for (i = 0, j = 0, k = 0; i < n; ++i) {
|
||||
if (strpbrk(a[i], s)) {
|
||||
if (k > 1)
|
||||
{
|
||||
for (int x = 2; x < k; x++) free(a[i - x]);
|
||||
a[j++] = a[i - 1];
|
||||
}
|
||||
|
||||
if (i != j)
|
||||
{
|
||||
a[j] = a[i];
|
||||
}
|
||||
j++;
|
||||
k = 0;
|
||||
} else {
|
||||
if (k == 0) {
|
||||
if (i != j)
|
||||
a[j] = a[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
if (k > 1)
|
||||
{
|
||||
for (int x = 2; x < k; x++) free(a[i - x]);
|
||||
a[j++] = a[i - 1];
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
int t7_solve(char ** a, int n, char *s) {
|
||||
bool fl_lst = false, fl_cur = false, fl_nxt = false;
|
||||
int i, j;
|
||||
|
||||
if (strcmp(a[0], s) < 0) fl_cur = true;
|
||||
|
||||
for (i = 1, j = 0; i < n; ++i) {
|
||||
if (strcmp(a[i], s) < 0) {
|
||||
fl_lst = true;
|
||||
fl_nxt = true;
|
||||
}
|
||||
|
||||
if (!fl_lst) {
|
||||
if (j != (i-1)) {
|
||||
a[j] = a[i-1];
|
||||
}
|
||||
j++;
|
||||
} else {
|
||||
free(a[i-1]);
|
||||
}
|
||||
|
||||
fl_lst = fl_cur;
|
||||
fl_cur = fl_nxt;
|
||||
fl_nxt = false;
|
||||
}
|
||||
|
||||
if (fl_lst)
|
||||
{
|
||||
free(a[i-1]);
|
||||
} else {
|
||||
a[j++] = a[i-1];
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
|
||||
int t8_solve(char ** a, int n, char *s) {
|
||||
bool fl_lst = false, fl_cur = false, fl_nxt = false;
|
||||
int i, j;
|
||||
|
||||
fl_cur = is_inside(a[0], s);
|
||||
|
||||
for (i = 1, j = 0; i < n; ++i) {
|
||||
if (is_inside(a[i], s)) {
|
||||
fl_lst = true;
|
||||
fl_nxt = true;
|
||||
}
|
||||
|
||||
if (!fl_lst) {
|
||||
if (j != (i-1)) {
|
||||
a[j] = a[i-1];
|
||||
}
|
||||
j++;
|
||||
} else {
|
||||
free(a[i-1]);
|
||||
}
|
||||
|
||||
fl_lst = fl_cur;
|
||||
fl_cur = fl_nxt;
|
||||
fl_nxt = false;
|
||||
}
|
||||
|
||||
if (fl_lst)
|
||||
{
|
||||
free(a[i-1]);
|
||||
} else {
|
||||
a[j++] = a[i-1];
|
||||
}
|
||||
|
||||
for (i = j; i < n; ++i) a[i] = 0;
|
||||
|
||||
return j;
|
||||
}
|
||||
|
||||
bool is_inside(char *a, char *s)
|
||||
{
|
||||
int len_s = (int)strlen(s);
|
||||
int len_a = (int)strlen(a);
|
||||
int i, j;
|
||||
|
||||
if (len_s < len_a) return false;
|
||||
|
||||
for (i = 0; i < len_s; ++i) {
|
||||
if (!ccmp(a[0], s[i])) {
|
||||
if (len_a > (len_s-len_a)) return false;
|
||||
|
||||
for (j = 0; j < len_a; ++j) {
|
||||
if (ccmp(a[j], s[i + j]) != 0) break;
|
||||
}
|
||||
|
||||
if (j == len_a) return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int ccmp(char a, char b) {
|
||||
if (LOWER(a)) a -= TO_UPPER;
|
||||
if (LOWER(b)) b -= TO_UPPER;
|
||||
|
||||
if (a < b) return -1;
|
||||
if (a > b) return 1;
|
||||
return 0;
|
||||
}
|
27
2025.02.21/solve.h
Normal file
27
2025.02.21/solve.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
#ifndef SOLVE
|
||||
#define SOLVE
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "array.h"
|
||||
|
||||
#define INF 97
|
||||
#define EXTR 122
|
||||
#define TO_UPPER 32
|
||||
|
||||
#define LOWER(a) (INF <= a && a <= EXTR)
|
||||
|
||||
int t1_solve(char **arr, int n, char *s);
|
||||
int t2_solve(char **arr, int n, char *s);
|
||||
int t3_solve(char **arr, int n, char *s);
|
||||
int t4_solve(char **arr, int n, char *s);
|
||||
int t5_solve(char **arr, int n, char *s);
|
||||
bool check(char *a, char *s);
|
||||
int t6_solve(char **arr, int n, char *s);
|
||||
int t7_solve(char **arr, int n, char *s);
|
||||
int t8_solve(char **arr, int n, char *s);
|
||||
bool is_inside(char *a, char *s);
|
||||
int ccmp(char a, char s);
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue