diff --git a/PL/HW/DZf28.03/fmain.c b/PL/HW/DZf28.03/fmain.c index 3fa1854..d590231 100644 --- a/PL/HW/DZf28.03/fmain.c +++ b/PL/HW/DZf28.03/fmain.c @@ -1,12 +1,13 @@ #include "funcHeader.h" - -int main() { +int main() +{ srand(time(0)); - People* professions1 = malloc(sizeof(People) * COUN); - People* professions2 = malloc(sizeof(People) * COUN); + People *professions1 = malloc(sizeof(People) * COUN); + People *professions2 = malloc(sizeof(People) * COUN); - if (professions1 == NULL || professions2 == NULL) { + if (professions1 == NULL || professions2 == NULL) + { printf("Mesta net\n"); exit(1); } @@ -27,7 +28,7 @@ int main() { printf("\nVtoroi massiv posle sort:\n"); print_struc(professions2, COUN); - People* ans = binSearch(professions1, COUN, 15); + People *ans = binSearch(professions1, COUN, 15); if (ans == NULL) printf("\nPeoples net\n"); @@ -47,9 +48,10 @@ int main() { int line_number = lines_numf("professions.txt"); printf("\nNumber of lines in professions.txt is %d\n", line_number); - People* professions3 = malloc(sizeof(People) * line_number); + People *professions3 = malloc(sizeof(People) * line_number); - if (professions3 == NULL) { + if (professions3 == NULL) + { printf("Mesta net\n"); exit(1); } diff --git a/PL/HW/DZf28.03/funcHeader.h b/PL/HW/DZf28.03/funcHeader.h index ab1bd4d..0f91abe 100644 --- a/PL/HW/DZf28.03/funcHeader.h +++ b/PL/HW/DZf28.03/funcHeader.h @@ -7,21 +7,19 @@ #include #include - - - -typedef struct People { +typedef struct People +{ int count; char name[LEN]; } People; -void print_struc(People professions[], int n); -void sort_sel(People professions[], int n); -void st_rand(People professions[], int n); -People* binSearch(People professions[], int n, int k); -void pst(People professions[], int s, int n); -int find_para(People t1[], int n1, People t2[], int n2); -void st_fprintf(People professions[], int n, char path[], char access[]); -int lines_numf(char path[]); -void st_fscanf(People t[], int n, char path[]); -void swap(People* t1, People* t2); \ No newline at end of file +void print_struc(People professions[], int n); +void sort_sel(People professions[], int n); +void st_rand(People professions[], int n); +People *binSearch(People professions[], int n, int k); +void pst(People professions[], int s, int n); +int find_para(People t1[], int n1, People t2[], int n2); +void st_fprintf(People professions[], int n, char path[], char access[]); +int lines_numf(char path[]); +void st_fscanf(People t[], int n, char path[]); +void swap(People *t1, People *t2); \ No newline at end of file diff --git a/PL/HW/DZf28.03/functions.c b/PL/HW/DZf28.03/functions.c index 78587b9..9336f26 100644 --- a/PL/HW/DZf28.03/functions.c +++ b/PL/HW/DZf28.03/functions.c @@ -1,24 +1,30 @@ #include "funcHeader.h" -void print_struc(People professions[], int n) { +void print_struc(People professions[], int n) +{ int i; for (i = 0; i < n; i++) printf("count: %3d; name: %s\n", professions[i].count, professions[i].name); } -void sort_sel(People professions[], int n) { +void sort_sel(People professions[], int n) +{ int i, j; - for (i = 0; i < n - 1; i++) { - for (j = i + 1; j < n; j++) { + for (i = 0; i < n - 1; i++) + { + for (j = i + 1; j < n; j++) + { if (professions[j].count < professions[i].count) swap(professions + i, professions + j); } } } -void st_rand(People professions[], int n) { +void st_rand(People professions[], int n) +{ int i, j; - for (i = 0; i < n; i++) { + for (i = 0; i < n; i++) + { professions[i].count = rand() % 3 + 1; for (j = 0; j < LEN - 1; j++) professions[i].name[j] = 'a' + rand() % 25; @@ -26,94 +32,112 @@ void st_rand(People professions[], int n) { } } -People* binSearch(People professions[], int n, int k) { - People* ans = NULL; +People *binSearch(People professions[], int n, int k) +{ + People *ans = NULL; int right = 0, left = n - 1; - - while (right <= left) { + + while (right <= left) + { int mid = (left + right) / 2; - if (professions[mid].count == k) { + if (professions[mid].count == k) + { ans = &professions[mid]; break; } - right = (professions[mid].count < k) ? mid + 1: right; - left = (professions[mid].count > k) ? mid - 1: left; + right = (professions[mid].count < k) ? mid + 1 : right; + left = (professions[mid].count > k) ? mid - 1 : left; } return ans; } -void pst(People professions[], int s, int n) { - if (n >= s) { +void pst(People professions[], int s, int n) +{ + if (n >= s) + { printf("Out of range\n"); exit(2); } - + printf("count: %2d; name: %s\n", professions[n].count, professions[n].name); } - -int find_para(People t1[], int n1, People t2[], int n2) { +int find_para(People t1[], int n1, People t2[], int n2) +{ int ans = 0, i; - for (i = 0; i < n1; i++) { - People* tans = binSearch(t2, n2, t1[i].count); - if (tans == NULL) continue; + for (i = 0; i < n1; i++) + { + People *tans = binSearch(t2, n2, t1[i].count); + if (tans == NULL) + continue; ans++; } return ans; } - -void st_fprintf(People professions[], int n, char path[], char access[]) { +void st_fprintf(People professions[], int n, char path[], char access[]) +{ int i; - FILE* f = fopen(path, access); - if (f == NULL) { + FILE *f = fopen(path, access); + if (f == NULL) + { printf("Could not open file %s\n", path); exit(3); } - for (i = 0; i < n; i++) { + for (i = 0; i < n; i++) + { fprintf(f, "%d %s\n", professions[i].count, professions[i].name); } fclose(f); } -int lines_numf(char path[]) { +int lines_numf(char path[]) +{ int ans = 0; char c; - FILE* f = fopen(path, "r"); - if (f == NULL) { + FILE *f = fopen(path, "r"); + if (f == NULL) + { printf("Faila %s net...\n", path); exit(3); } - while ((c = fgetc(f)) != EOF) { - if (c == '\n') ans++; + while ((c = fgetc(f)) != EOF) + { + if (c == '\n') + ans++; } fclose(f); return ans; } -void st_fscanf(People t[], int n, char path[]) { +void st_fscanf(People t[], int n, char path[]) +{ int i; - FILE* f = fopen(path, "r"); - if (f == NULL) { + FILE *f = fopen(path, "r"); + if (f == NULL) + { printf("Faila %s net...\n", path); exit(3); } - for (i = 0; i < n; i++) { + for (i = 0; i < n; i++) + { fscanf(f, "%d %s\n", &t[i].count, t[i].name); } fclose(f); } -void swap(People* t1, People* t2) { - People* tmp = malloc(sizeof(People)); +void swap(People *t1, People *t2) +{ + People *tmp = malloc(sizeof(People)); - if (tmp == NULL) { + if (tmp == NULL) + { printf("could not malloc\n"); exit(1); } diff --git a/PL/Lessons/L28.03 - Dinamic Structure/header.h b/PL/Lessons/L28.03 - Dinamic Structure/header.h index 8b95ec0..47bfb3e 100644 --- a/PL/Lessons/L28.03 - Dinamic Structure/header.h +++ b/PL/Lessons/L28.03 - Dinamic Structure/header.h @@ -1,7 +1,7 @@ #define COUN 40 - -typedef struct Node { +typedef struct Node +{ long int data; struct Node *next; struct Node *prev; @@ -11,8 +11,8 @@ typedef struct Node { void pr(node *tmp); node *add_head(node *head, int hh); node *add_tail(node *head, int hh); -node *del_head(node *head); -node *del_tail(node *list); +node *del_head(node *head); +node *del_tail(node *list); // node *sort(node *head); // int lenghth(node *list); // void writefile(const char *path, node *head); diff --git a/PL/Lessons/L28.03 - Dinamic Structure/main.c b/PL/Lessons/L28.03 - Dinamic Structure/main.c index 4596d4b..baec28f 100644 --- a/PL/Lessons/L28.03 - Dinamic Structure/main.c +++ b/PL/Lessons/L28.03 - Dinamic Structure/main.c @@ -4,21 +4,23 @@ #include "header.h" #include -int main(int args, char *argv[]){ - node *head=NULL; +int main(int args, char *argv[]) +{ + node *head = NULL; int r; srand(time(NULL)); - while((r=(rand()%100))<90) head = add_head(head,r); + while ((r = (rand() % 100)) < 90) + head = add_head(head, r); pr(head); head = del_head(head); pr(head); - + head = add_tail(head, 5); pr(head); head = del_tail(head); - pr(head); + pr(head); } -//Домашка: сортировка списка и вычисление длины списка(по желанию запись в файл и чтение с файла) -//Ханойские башни игра написать \ No newline at end of file +// Домашка: сортировка списка и вычисление длины списка(по желанию запись в файл и чтение с файла) +// Ханойские башни игра написать \ No newline at end of file