This commit is contained in:
ada-dmitry
2023-05-09 17:52:40 +03:00
parent 875630a713
commit 8fb559981e
57 changed files with 0 additions and 794 deletions
Binary file not shown.
+70
View File
@@ -0,0 +1,70 @@
#include "funcHeader.h"
int main()
{
srand(time(0));
People *professions1 = malloc(sizeof(People) * COUN);
People *professions2 = malloc(sizeof(People) * COUN);
if (professions1 == NULL || professions2 == NULL)
{
printf("Mesta net\n");
exit(1);
}
st_rand(professions1, COUN);
st_rand(professions2, COUN);
printf("Perviy massiv:\n");
print_struc(professions1, COUN);
printf("\nVtoroi massiv:\n");
print_struc(professions2, COUN);
sort_sel(professions1, COUN);
sort_sel(professions2, COUN);
printf("\nPerviy massiv posle sort:\n");
print_struc(professions1, COUN);
printf("\nVtoroi massiv posle sort:\n");
print_struc(professions2, COUN);
People *ans = binSearch(professions1, COUN, 15);
if (ans == NULL)
printf("\nPeoples net\n");
else
printf("\nLength: %2d; name: %s\n", ans->count, ans->name);
printf("\nElement in perviy massiv: ");
pst(professions1, COUN, (int)(COUN * 0.7));
printf("Element in vtoroi massiv: ");
pst(professions2, COUN, (int)(COUN * 0.3));
printf("\nNumber of para : %d\n", find_para(professions1, COUN, professions2, COUN));
st_fprintf(professions1, COUN, "professions.txt", "w");
st_fprintf(professions2, COUN, "professions.txt", "a");
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);
if (professions3 == NULL)
{
printf("Mesta net\n");
exit(1);
}
st_fscanf(professions3, line_number, "professions.txt");
sort_sel(professions3, line_number);
Sleep(50000);
printf("\nTTretiy massiv is:\n");
print_struc(professions3, line_number);
free(professions1);
free(professions2);
free(professions3);
return 0;
}
+25
View File
@@ -0,0 +1,25 @@
#define COUN 4
#define LEN 8
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <windows.h>
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);
+149
View File
@@ -0,0 +1,149 @@
#include "funcHeader.h"
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)
{
int i, 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)
{
int i, j;
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;
professions[i].name[j + 1] = '\0';
}
}
People *binSearch(People professions[], int n, int k)
{
People *ans = NULL;
int right = 0, left = n - 1;
while (right <= left)
{
int mid = (left + right) / 2;
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;
}
return ans;
}
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 ans = 0, i;
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[])
{
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", professions[i].count, professions[i].name);
}
fclose(f);
}
int lines_numf(char path[])
{
int ans = 0;
char c;
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++;
}
fclose(f);
return ans;
}
void st_fscanf(People t[], int n, char path[])
{
int i;
FILE *f = fopen(path, "r");
if (f == NULL)
{
printf("Faila %s net...\n", path);
exit(3);
}
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));
if (tmp == NULL)
{
printf("could not malloc\n");
exit(1);
}
memcpy(tmp, t1, sizeof(People));
memcpy(t1, t2, sizeof(People));
memcpy(t2, tmp, sizeof(People));
free(tmp);
}
+8
View File
@@ -0,0 +1,8 @@
1 pkaeoew
1 ujqrkpqP
2 nllvevc=
2 wrqqkwf(
1 krxtlgme
1 srgyisre
3 tiflqbd(
3 opukfbci
Binary file not shown.
BIN
View File
Binary file not shown.
+140
View File
@@ -0,0 +1,140 @@
#include "head.h"
void linked_print(LinkList* l) {
while (l != NULL) {
printf("%d %s\n", l->x, l->name);
l = l->next;
}
}
LinkList* add_tail(LinkList* l, LinkList node) {
if (l->x == 0 && !strcmp(l->name, "\0")) {
l->x = node.x;
memcpy(l->name, node.name, LEN);
l->next = NULL;
l->prev = NULL;
return l;
}
LinkList* res = l;
while (l->next != NULL) l = l->next;
LinkList* tmp = malloc(sizeof(LinkList));
tmp->x = node.x;
memcpy(tmp->name, node.name, LEN);
tmp->prev = l;
l->next = tmp;
return res;
}
LinkList* add_head(LinkList* l, LinkList node) {
if (l->x == 0 && !strcmp(l->name, "\0")) {
l->x = node.x;
memcpy(l->name, node.name, LEN);
l->next = NULL;
l->prev = NULL;
return l;
}
LinkList* tmp = malloc(sizeof(LinkList));
if (tmp == NULL)
exit(1);
tmp->next = l;
l->prev = tmp;
tmp->prev = NULL;
tmp->x = node.x;
memcpy(tmp->name, node.name, LEN);
return tmp;
}
LinkList* del_head(LinkList* l) {
if (l == NULL) return NULL;
if (l->next == NULL) {
free(l);
return NULL;
}
LinkList* tmp = l->next;
tmp->prev = NULL;
free(l);
return tmp;
}
void del_tail(LinkList* l) {
if (l == NULL) return;
else if (l->next == NULL) {
free(l);
l = NULL;
return;
}
while (l->next != NULL) l = l->next;
LinkList* tmp = l->prev;
l->prev = NULL;
free(l);
tmp->next = NULL;
}
int len(LinkList* l) {
int res = 0;
if (l->x == 0 && !strcmp(l->name, "\0"))
return 0;
while (l != NULL) {
res++;
l = l->next;
}
return res;
}
LinkList get(LinkList* l, int n) {
if (n > len(l)) exit(3);
int i;
for (i = 0; i < n; i++) {
l = l->next;
}
return *l;
}
void tower_of_hanoi(int n, LinkList* start, LinkList* finish, LinkList* tmp) {
if (n == 1) {
finish = add_head(finish, get(start, 0));
start = del_head(start);
return;
}
tower_of_hanoi(n - 1, start, tmp, finish);
finish = add_head(finish, get(start, 0));
start = del_head(start);
tower_of_hanoi(n - 1, tmp, finish, start);
printf("\nPosition:\nStart:\n");
linked_print(start);
printf("Finish:\n");
linked_print(finish);
printf("Tmp:\n");
linked_print(tmp);
}
void linked_free(LinkList* l) {
while (l->next != NULL) l = l->next;
while (l->prev != NULL) {
LinkList* tmp = l;
l = l->prev;
free(tmp);
}
}
+38
View File
@@ -0,0 +1,38 @@
#include "head.h"
int main(int argc, char* argv[]) {
srand(time(0));
LinkList* l1 = malloc(sizeof(LinkList));
LinkList* l2 = malloc(sizeof(LinkList));
LinkList* l3 = malloc(sizeof(LinkList));
if (l1 == NULL || l2 == NULL || l3 == NULL)
exit(1);
int i, j;
char s[LEN];
for (i = 0; i < N; i++) {
for (j = 0; j < LEN - 1; j++)
s[j] = 'a' + rand() % 25;
s[j + 1] = '\0';
LinkList l;
l.x = i;
memcpy(l.name, s, LEN);
l.next = NULL;
l.prev = NULL;
l1 = add_tail(l1, l);
}
tower_of_hanoi(N, l1, l3, l2);
// printf("Done:\n");
linked_free(l1);
linked_free(l2);
linked_free(l3);
return 0;
}
+24
View File
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 3
#define LEN 8
typedef struct LinkList {
int x;
char name[LEN];
struct LinkList* next;
struct LinkList* prev;
} LinkList;
void linked_print(LinkList* l);
LinkList* add_tail(LinkList* l, LinkList node);
LinkList* add_head(LinkList* l, LinkList node);
LinkList* del_head(LinkList* l);
void del_tail(LinkList* l);
int len(LinkList* l);
LinkList get(LinkList* l, int n);
void tower_of_hanoi(int n, LinkList* start, LinkList* finish, LinkList* tmp);
void linked_free(LinkList* l);
BIN
View File
Binary file not shown.
+65
View File
@@ -0,0 +1,65 @@
#include "head.h"
Stack get(Stack *l, int n) {
if (n > len(l))
exit(3);
int i;
for (i = 0; i < n; i++) {
l = l->next;
}
return *l;
}
Stack *add_head(Stack *l, double x) {
if (l == NULL) {
l = malloc(sizeof(Stack));
if (l == NULL)
exit(1);
l->x = x;
l->next = NULL;
}
if (l->x == 0.0) {
l->x = x;
l->next = NULL;
return l;
}
Stack *tmp = malloc(sizeof(Stack));
if (tmp == NULL)
exit(1);
tmp->next = l;
tmp->x = x;
return tmp;
}
void stack_print(Stack *l) {
while (l != NULL) {
printf("%lF\n", l->x);
l = l->next;
}
}
int len(Stack *l) {
int ans = 0;
while (l != NULL) {
ans++;
l = l->next;
}
return ans;
}
Stack *del_head(Stack *l) {
if (l == NULL)
return NULL;
else if (l->next == NULL) {
free(l);
l = NULL;
return NULL;
}
Stack *tmp = l->next;
free(l);
l = NULL;
return tmp;
}
+17
View File
@@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 6
typedef struct Stack {
double x;
struct Stack *next;
} Stack;
Stack get(Stack *l, int n);
Stack *add_head(Stack *l, double x);
void stack_print(Stack *l);
int len(Stack *l);
Stack *del_head(Stack *l);
+61
View File
@@ -0,0 +1,61 @@
#include "head.h"
int main(int argc, char *argv[]) {
srand(time(0));
Stack *numbers = malloc(sizeof(Stack));
if (numbers == NULL)
exit(1);
char c;
char flag = 'f';
while ((c = getchar()) != EOF) {
if (c == '\n') {
break;
} else if (c == ' ') {
flag = 't';
continue;
}
switch (c) {
case '0' ... '9':
if (flag == 't') {
numbers = add_head(numbers, (double)(c - '0'));
flag = 'f';
} else {
double a = get(numbers, 0).x;
numbers = del_head(numbers);
a = a * 10 + c - '0';
numbers = add_head(numbers, a);
}
break;
case '+':
case '-':
case '*':
case '/':
if (len(numbers) < 2) {
printf("Invalid expression\n");
exit(2);
}
double a = get(numbers, 0).x;
numbers = del_head(numbers);
double b = get(numbers, 0).x;
numbers = del_head(numbers);
b = (c == '+') ? b + a : b;
b = (c == '-') ? b - a : b;
b = (c == '*') ? b * a : b;
b = (c == '/') ? b / a : b;
numbers = add_head(numbers, b);
break;
}
}
printf("result: %lF\n", get(numbers, 0).x);
free(numbers);
return 0;
}
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+127
View File
@@ -0,0 +1,127 @@
#include <stdio.h>
#include <stdlib.h>
#include "head.h"
tree *init_root(tree *root, int field) // Выделение памяти под корень дереваnode
{
tree *tmp = malloc(sizeof(tree)); // Присваивание значения ключу
tmp -> field = field;// Присваивание указателю на родителя значения NULL
tmp -> parent = NULL; // Присваивание указателю на левое и правое поддерево значения NULL
tmp -> left = tmp -> right = NULL;
root = tmp;
return root;
}
tree *add_node(tree *root, int field)
{
tree *root2 = root, *root3 = NULL; // Выделение памяти под узел дерева
tree *tmp = malloc(sizeof(tree)); // Присваивание значения ключу
tmp -> field = field;
while (root2 != NULL)
{
root3 = root2;
if (field < root2 -> field)
root2 = root2 -> left;
else
root2 = root2 -> right;
}
tmp -> parent = root3; // Присваивание указателю на левое и правое поддерево значения NULL
tmp -> left = NULL;
tmp -> right = NULL;
if (field < root3 -> field) root3 -> left = tmp;
else root3 -> right = tmp;
return root;
}
tree *find_node(tree * root, int field) // Если дерево пусто или ключ корня равен искомому ключу, то возвращается указатель на корень
{
if ((root == NULL) || (root -> field == field))
return root; // Поиск нужного узла
if (field < root -> field)
return find_node(root -> left, field);
else return find_node(root -> right, field);
}
// Минимальный элемент дерева
tree *minim(tree *root)
{
tree *lef = root;
while (lef -> left != NULL)
lef = lef -> left;
return lef;
}
// Максимальный элемент дерева
tree *maxim(tree *root)
{
tree *rig = root;
while (rig -> right != NULL)
rig = rig -> right;
return rig;
}
tree *findForDel(tree *root)
{
tree *rig = root, *lef = NULL; // Если есть правое поддерево, то ищем минимальный элемент в этом поддереве
if (rig -> right != NULL)
return minim(rig -> right);
lef = rig -> parent;
while ((lef != NULL) && (rig == lef -> right))
{
rig = lef;
lef = lef -> parent;
}
return lef;
}
tree *del_node(tree *root, int field)
{
// Поиск удаляемого узла по ключу
tree *rig = root, *lef = NULL, *tmp = NULL;
lef = find_node(root, field);
// 1 случай: у узла нет потомков
if ((lef -> left == NULL) && (lef -> right == NULL))
{
tmp = lef -> parent;
if (lef == tmp -> right) tmp -> right = NULL;
else tmp -> left = NULL;
free(lef);
}
// 2 случай: потомок один - поддерево справа
if ((lef -> left == NULL) && (lef -> right != NULL))
{
tmp = lef -> parent;
if (lef == tmp -> right) tmp -> right = lef -> right;
else tmp -> left = lef -> right;
free(lef);
}
// 2 случай: потомок один - поддерево слева
if ((lef -> left != NULL) && (lef -> right == NULL))
{
tmp = lef -> parent;
if (lef == tmp -> right) tmp -> right = lef -> left;
else tmp -> left = lef -> left;
free(lef);
}
// 3 случай: оба потомка
if ((lef -> left != NULL) && (lef -> right != NULL))
{
tmp = findForDel(lef);
lef -> field = tmp -> field;
if (tmp -> right == NULL)
tmp -> parent -> left = NULL;
else tmp -> parent -> left = tmp -> right;
free(tmp);
}
return root;
}
void print_tree(tree *root)
{
if (root == NULL)
return;
if (root -> field)
printf("%d ", root -> field);
print_tree(root -> left);
print_tree(root -> right);
}
+15
View File
@@ -0,0 +1,15 @@
typedef struct tnode {
int field;
struct tnode *left;
struct tnode *right;
struct tnode *parent;
} tree;
tree *init_root(tree *root, int field);
tree *add_node(tree *root, int field);
tree *find_node(tree * root, int field);
tree *minim(tree *root);
tree *maxim(tree *root);
tree *findForDel(tree *root);
tree *del_node(tree *root, int field);
void print_tree(tree *root);
+25
View File
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <stdio.h>
#include "head.h"
int main(int argc, char *argv[]){
tree *root=NULL;
root = init_root(root, 65);
print_tree(root);
printf("\n");
root=del_node(root, 65);
print_tree(root);
printf("\n");
root = add_node(root, 70);
root = add_node(root, 66);
root = add_node(root, 68);
root = add_node(root, 71);
root = add_node(root, 67);
root = add_node(root, 72);
print_tree(root);
printf("\n");
root = del_node(root, 66);
print_tree(root);
printf("\n");
return 0;
}
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+115
View File
@@ -0,0 +1,115 @@
#include "head.h"
void node_print(Node *n)
{
while (n != NULL)
{
printf("%s\n", n->name);
n = n->next;
}
}
Node get(Node *node, int n)
{
int i;
for (i = 0; i < n; i++)
{
node = node->next;
}
return *node;
}
int len(Node *n)
{
int res = 0;
while (n != NULL)
{
res++;
n = n->next;
}
return res;
}
Node *xor_find(Node *n1, Node *n2)
{
int i, j, f = 0;
Node *res = NULL;
if ((res = malloc(sizeof(Node))) == NULL)
{
printf("could not malloc\n");
exit(3);
}
for (i = 0; i < len(n1); i++)
{
Node tmp1 = get(n1, i);
for (j = 0; j < len(n2); j++)
{
Node tmp2 = get(n2, j);
if (strcmp(tmp1.name, tmp2.name) == 0)
{
f = 1;
}
}
if (f == 0)
{
Node tmp = get(n1, i);
res = add_tail(res, tmp.name);
}
f = 0;
}
for (i = 0; i < len(n2); i++)
{
Node tmp2 = get(n2, i);
for (j = 0; j < len(n1); j++)
{
Node tmp1 = get(n1, j);
if (strcmp(tmp2.name, tmp1.name) == 0)
{
f = 1;
}
}
if (f == 0)
{
Node tmp = get(n2, i);
res = add_tail(res, tmp.name);
}
f = 0;
}
return res;
}
Node *add_tail(Node *n, char s[])
{
if (n == NULL)
{
if ((n = malloc(sizeof(Node))) == NULL)
{
printf("could not malloc\n");
exit(1);
}
n->next = NULL;
n->prev = NULL;
memcpy(n->name, s, LEN);
return n;
}
if (!strcmp(n->name, "\0"))
{
memcpy(n->name, s, LEN);
n->next = NULL;
n->prev = NULL;
return n;
}
Node *res = n;
while (n->next != NULL)
n = n->next;
Node *tmp = malloc(sizeof(Node));
memcpy(tmp->name, s, LEN);
tmp->prev = n;
n->next = tmp;
return res;
}
+18
View File
@@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 3
#define LEN 3
typedef struct Node {
struct Node* next;
struct Node* prev;
char name[LEN];
} Node;
void node_print(Node* n);
Node get(Node* node, int n);
Node* xor_find(Node* n1, Node* n2);
Node* add_tail(Node* n, char s[]);
+50
View File
@@ -0,0 +1,50 @@
#include "head.h"
int main(int argc, char *argv[])
{
srand(time(0));
Node *n1 = NULL;
Node *n2 = NULL;
Node *n3 = NULL;
if ((n1 = malloc(sizeof(Node))) == NULL ||
(n2 = malloc(sizeof(Node))) == NULL ||
(n3 = malloc(sizeof(Node))) == NULL)
{
printf("Could not malloc\n");
exit(2);
}
int i;
for (i = 0; i < N; i++)
{
char s[LEN];
int j;
for (j = 0; j < LEN - 1; j++)
s[j] = 'a' + rand() % 2;
s[LEN - 1] = '\0';
n1 = add_tail(n1, s);
for (j = 0; j < LEN - 1; j++)
s[j] = 'a' + rand() % 2;
s[LEN - 1] = '\0';
n2 = add_tail(n2, s);
}
printf("Begin arrays:\n");
node_print(n1);
printf("\n");
node_print(n2);
n3 = xor_find(n1, n2);
printf("\nFinal array:\n");
node_print(n3);
free(n1);
free(n2);
free(n3);
return 0;
}
+56
View File
@@ -0,0 +1,56 @@
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* top = NULL; // инициализация вершины стека как пустой
/* функция для добавления элемента в стек */
void push(int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = top;
top = new_node;
}
/* функция для удаления элемента из стека */
int pop() {
if (top == NULL) { // проверка на пустой стек
printf("Stack Underflow!");
return -1;
}
int data = top->data;
struct Node* temp = top;
top = top->next; // Спросить по поводу отсутствия эл-та
free(temp);
return data;
}
/* функция для просмотра последнего элемента стека */
int peek() {
if (top == NULL) { // проверка на пустой стек
printf("Stack is Empty!");
return -1;
}
return top->data;
}
int main() {
printf("%d\n", peek());
push(1);
printf("%d\n", peek());
push(2);
printf("%d\n", peek());
push(3);
printf("%d\n", pop()); // выведет 3
printf("%d\n", peek()); // выведет 2
printf("%d", pop()); // выведет 2
printf("\n");
return 0;
}
+172
View File
@@ -0,0 +1,172 @@
#include "header.h"
void linked_print(Node* l) {
while (l != NULL) {
printf("%d %s\n", l->x, l->name);
l = l->next;
}
}
Node* add_tail(Node* l, int x, char name[]) {
if (l->x == 0 && !strcmp(l->name, "\0")) {
l->x = x;
memcpy(l->name, name, STR_LEN);
l->next = NULL;
l->prev = NULL;
return l;
}
Node* res = l;
while (l->next != NULL) l = l->next;
Node* tmp = malloc(sizeof(Node));
tmp->x = x;
memcpy(tmp->name, name, STR_LEN);
tmp->prev = l;
l->next = tmp;
return res;
}
Node* add_head(Node* l, int x, char name[]) {
if (l->x == 0 && !strcmp(l->name, "\0")) {
l->x = x;
memcpy(l->name, name, STR_LEN);
l->next = NULL;
l->prev = NULL;
return l;
}
Node* tmp = malloc(sizeof(Node));
if (tmp == NULL)
exit(1);
tmp->next = l;
l->prev = tmp;
tmp->prev = NULL;
tmp->x = x;
memcpy(tmp->name, name, STR_LEN);
return tmp;
}
Node* del_head(Node* l) {
if (l == NULL) return NULL;
else if (l->next == NULL) {
free(l);
l = NULL;
return NULL;
}
Node* tmp = l->next;
tmp->prev = NULL;
free(l);
l = NULL;
return tmp;
}
void del_tail(Node* l) {
if (l == NULL) return;
else if (l->next == NULL) {
free(l);
l = NULL;
return;
}
while (l->next != NULL) l = l->next;
Node* tmp = l->prev;
l->prev = NULL;
free(l);
tmp->next = NULL;
}
int len(Node* l) {
int res = 0;
while (l != NULL) {
res++;
l = l->next;
}
return res;
}
void swap(Node* l, int first, int second) {
if (first >= len(l) || second >= len(l)) exit(3);
int i, min, max;
min = (first <= second) ? first : second;
max = first + second - min;
for (i = 0; i < min; i++) {
if (l != NULL) l = l->next;
else exit(2);
}
Node* tmp = l;
for (i = 0; i < max - min; i++) {
if (l != NULL) l = l->next;
else exit(2);
}
Node temp = *l;
l->x = tmp->x;
tmp->x = temp.x;
memcpy(l->name, tmp->name, STR_LEN);
memcpy(tmp->name, temp.name, STR_LEN);
}
Node get(Node* l, int n) {
if (n > len(l)) exit(3);
int i;
for (i = 0; i < n; i++) {
l = l->next;
}
return *l;
}
void select_sort(Node* l) {
if (l == NULL || l->next == NULL) return;
int n = len(l), i, j;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (get(l, j).x < get(l, i).x)
swap(l, i, j);
}
}
}
void list_fprintf(Node* l, char path[]) {
FILE* f = fopen(path, "w");
if (f == NULL)
exit(4);
int i, n = len(l);
for (i = 0; i < n; i++) {
fprintf(f, "%d %s\n", l->x, l->name);
l = l->next;
}
fclose(f);
}
Node* list_fscanf(char path[]) {
int tmp_x, i;
char s[STR_LEN] = {'\0'};
FILE* f = fopen(path, "r");
if (f == NULL)
exit(5);
Node* tmp = malloc(sizeof(Node)); //Дописать проверку на NULL
tmp->prev = NULL;
tmp->next = NULL;
for (i = 0; fscanf(f, "%d %s\n", &tmp_x, s) != EOF; i++) {
if (i == 0) {
tmp->x = tmp_x;
memcpy(tmp->name, s, STR_LEN);
} // Перенести в a_t
else add_tail(tmp, tmp_x, s);
}
fclose(f);
return tmp;
}
+27
View File
@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 5
#define STR_LEN 8
typedef struct Node {
int x;
struct Node* next;
struct Node* prev;
char name[STR_LEN];
} Node;
void linked_print(Node* l);
Node* add_tail(Node* l, int x, char name[]);
Node* add_head(Node* l, int x, char name[]);
Node* del_head(Node* l);
void del_tail(Node* l);
int len(Node* l);
Node get(Node* l, int n);
void swap(Node* l1, int i, int j);
void select_sort(Node* l);
void list_fprintf(Node* l, char path[]);
Node* list_fscanf(char path[]);
+50
View File
@@ -0,0 +1,50 @@
#include "function.c"
int main() {
srand(time(0));
Node* l = malloc(sizeof(Node));
if (l == NULL)
exit(1);
int i, j;
for (i = 0; i < N; i++) {
char s[STR_LEN];
for (j = 0; j < STR_LEN - 1; j++)
s[j] = 'a' + rand() % 25;
s[j + 1] = '\0';
add_tail(l, rand() % 100, s);
}
printf("\nAfter add_tail(%d):\n", len(l));
linked_print(l);
l = add_head(l, 5, "head");
printf("\nAfter add_head(%d):\n", len(l));
linked_print(l);
l = del_head(l);
printf("\nAfter del_head(%d):\n", len(l));
linked_print(l);
swap(l, 0, 1);
printf("\nAfter swap first and second el:\n");
linked_print(l);
del_tail(l);
printf("\nAfter del_tail(%d):\n", len(l));
linked_print(l);
select_sort(l);
printf("\nAfter sort:\n");
linked_print(l);
printf("\nWriting to file test.txt\n");
list_fprintf(l, "test.txt");
printf("\nRead from file test.txt:\n");
Node* l2 = list_fscanf("test.txt");
linked_print(l2);
free(l);
return 0;
}
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,4 @@
2 qjrjqtg
7 upivrdd
49 bokdbfv
74 hlchgxy
+32
View File
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* function to solve the Tower of Hanoi puzzle */
void tower_of_hanoi(int n, char from, char to, char aux)
{
/* base case - when there's only one disk present */
if (n == 1)
{
printf("Move disk 1 from rod %c to rod %c\n", from, to);
return;
}
/* move n-1 disks from the source to auxiliary rod */
tower_of_hanoi(n - 1, from, aux, to);
/* move the remaining one disk from the source to destination rod */
printf("Move disk %d from rod %c to rod %c\n", n, from, to);
/* move n-1 disks from the auxiliary to destination rod */
tower_of_hanoi(n - 1, aux, to, from);
}
int main()
{
int n;
/* ask user for the number of disks */
printf("Enter the number of disks: ");
scanf("%d", &n);
/* call the tower_of_hanoi function to solve the puzzle */
tower_of_hanoi(n, 'A', 'C', 'B');
return 0;
}
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
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[]);
+41
View File
@@ -0,0 +1,41 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
double func(double x)
{
return exp(x*5.8) + sin(x+3.5);
}
int main(int argc, char *argv[])
{
srand(time(0));
double start = -5.6, finish = -1.0, e = 1e-6, mid = (finish + start) / 2.0;
if (func(start) * func(finish) > 0)
{
printf("Couldn`t find\n");
exit(0);
}
while (fabs(func(mid)) >= e)
{
mid = (finish + start) / 2.0;
if (func(mid) > 0)
{
start = (func(start) > func(finish)) ? mid : start;
finish = (func(start) < func(finish)) ? mid : finish;
}
else
{
start = (func(start) < func(finish)) ? mid : start;
finish = (func(start) > func(finish)) ? mid : finish;
}
}
printf("Zero: %lF\n", start);
return 0;
}
Binary file not shown.