restruct 1

This commit is contained in:
ada-dmitry
2023-09-11 17:15:34 +03:00
parent 096d8dea1a
commit 6809e2ef5f
67 changed files with 0 additions and 0 deletions
Binary file not shown.
+6
View File
@@ -0,0 +1,6 @@
1 fdxyqwb3
1 icotltwo
2 ysharmos
2 xhlbvpd3
3 owvischE
3 bmmuvuhM
+166
View File
@@ -0,0 +1,166 @@
#include "head.h"
void st_rand(Book books[], int n) {
int i, j;
for (i = 0; i < n; i++) {
// books[i].len = rand() % 99 + 1;
books[i].len = rand() % 3 + 1;
for (j = 0; j < STR_LEN - 1; j++)
books[i].name[j] = 'a' + rand() % 25;
books[i].name[j + 1] = '\0';
}
}
void st_print(Book books[], int n) {
int i;
for (i = 0; i < n; i++)
printf("len: %3d; name: %s\n", books[i].len, books[i].name);
}
void swap(Book* b1, Book* b2) {
Book* tmp = malloc(sizeof(Book));
if (tmp == NULL) {
printf("could not malloc\n");
exit(1);
}
memcpy(tmp, b1, sizeof(Book));
memcpy(b1, b2, sizeof(Book));
memcpy(b2, tmp, sizeof(Book));
free(tmp);
}
void selection_sort(Book books[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (books[j].len < books[i].len)
swap(books + i, books + j);
}
}
}
Book* bin_search(Book books[], int n, int key) {
Book* res = NULL;
int low = 0, high = n - 1;
while (low <= high) {
int mid = (high + low) / 2;
if (books[mid].len == key) {
res = &books[mid];
break;
}
low = (books[mid].len < key) ? mid + 1: low;
high = (books[mid].len > key) ? mid - 1: high;
}
return res;
}
void pst(Book books[], int size, int n) {
if (n >= size) {
printf("index bigger then length of the array\n");
exit(2);
}
printf("len: %3d; name: %s\n", books[n].len, books[n].name);
}
int matches_number(Book b[], int n, int match) {
int res = 0, i;
for (i = 0; i < n; i++) {
if (b[i].len == match) res++;
else if (b[i].len > match) break;
}
return res;
}
int find_pairs_easy(Book b1[], int n1, Book b2[], int n2) {
int res = 0, i;
for (i = 0; i < n1; i++) {
Book* b_res = bin_search(b2, n2, b1[i].len);
if (b_res == NULL) continue;
res++;
}
return res;
}
int find_pairs_advanced(Book b1[], int n1, Book b2[], int n2) {
int res = 0, i;
for (i = 0; i < n1; i++) {
Book* b_res = bin_search(b2, n2, b1[i].len);
if (b_res == NULL) continue;
res += matches_number(b2, n2, b1[i].len);
}
return res;
}
void st_fprintf(Book books[], int n, char path[], char access[]) {
int i;
FILE* f = fopen(path, access);
if (f == NULL) {
printf("Could not open file %s\n", path);
exit(3);
}
for (i = 0; i < n; i++) {
fprintf(f, "%d %s\n", books[i].len, books[i].name);
}
fclose(f);
}
int lines_numf(char path[]) {
int res = 0;
char c;
FILE* f = fopen(path, "r");
if (f == NULL) {
printf("Could not open file %s\n", path);
exit(3);
}
while ((c = fgetc(f)) != EOF) {
if (c == '\n') res++;
}
fclose(f);
return res;
}
int lines_num(char path[]) {
int res = 0, flag = 0;
char c;
FILE* f = fopen(path, "r");
if (f == NULL) {
printf("Could not open file %s\n", path);
exit(3);
}
while ((c = fgetc(f)) != EOF) {
if (c != '\n') flag = 1;
if (c == '\n' && flag) {
res++;
flag = 0;
}
}
fclose(f);
return res + flag;
}
void st_fscanf(Book b[], int n, char path[]) {
int i;
FILE* f = fopen(path, "r");
if (f == NULL) {
printf("Could not open file %s\n", path);
exit(3);
}
for (i = 0; i < n; i++) {
fscanf(f, "%d %s\n", &b[i].len, b[i].name);
}
fclose(f);
}
+71
View File
@@ -0,0 +1,71 @@
#include "head.h"
int main(int argc, char* argv[]) {
srand(time(0));
Book* books1 = malloc(sizeof(Book) * N);
Book* books2 = malloc(sizeof(Book) * N);
if (books1 == NULL || books2 == NULL) {
printf("could not malloc\n");
exit(1);
}
st_rand(books1, N);
st_rand(books2, N);
printf("First array:\n");
st_print(books1, N);
printf("\nSecond array:\n");
st_print(books2, N);
selection_sort(books1, N);
selection_sort(books2, N);
printf("\nFirst array after sort:\n");
st_print(books1, N);
printf("\nSecond array after sort:\n");
st_print(books2, N);
Book* res = bin_search(books1, N, 15);
if (res == NULL)
printf("\nNo such book\n");
else
printf("\nLength: %2d; name: %s\n", res->len, res->name);
printf("\nElement in first array: ");
pst(books1, N, (int)(N * 0.7));
printf("Element in second array: ");
pst(books2, N, (int)(N * 0.3));
printf("\nNumber of pairs (easy realization): %d\n", find_pairs_easy(books1, N, books2, N));
printf("Number of pairs (advanced realization): %d\n", find_pairs_advanced(books1, N, books2, N));
st_fprintf(books1, N, "books.txt", "w");
st_fprintf(books2, N, "books.txt", "a");
int line_number = lines_numf("books.txt");
printf("\nNumber of lines in books.txt is %d\n", line_number);
Book* books3 = malloc(sizeof(Book) * line_number);
if (books3 == NULL) {
printf("could not malloc\n");
free(books1);
free(books2);
exit(1);
}
st_fscanf(books3, line_number, "books.txt");
selection_sort(books3, line_number);
printf("\nThird array is:\n");
st_print(books3, line_number);
free(books1);
free(books2);
free(books3);
return 0;
}
+29
View File
@@ -0,0 +1,29 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 3
#define STR_LEN 8
typedef struct Book {
int len;
char name[STR_LEN];
} Book;
void st_rand(Book books[], int n);
void st_print(Book books[], int n);
void swap(Book* b1, Book* b2);
void selection_sort(Book books[], int n);
Book* bin_search(Book books[], int n, int key);
void pst(Book books[], int size, int n);
int matches_number(Book b[], int n, int match);
int find_pairs_easy(Book b1[], int n1, Book b2[], int n2);
int find_pairs_advanced(Book b1[], int n1, Book b2[], int n2);
void st_fprintf(Book books[], int n, char path[], char access[]);
int lines_numf(char path[]);
int lines_num(char path[]);
void st_fscanf(Book b[], int n, char path[]);