mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 01:00:15 +00:00
work in progress...1
This commit is contained in:
Binary file not shown.
@@ -1,6 +1,6 @@
|
||||
#include "head.h"
|
||||
|
||||
void node_print(Node *n)
|
||||
void node_print(Unit *n)
|
||||
{
|
||||
while (n != NULL)
|
||||
{
|
||||
@@ -9,7 +9,7 @@ void node_print(Node *n)
|
||||
}
|
||||
}
|
||||
|
||||
Node get(Node *node, int n)
|
||||
Unit get(Unit *node, int n)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
@@ -19,7 +19,7 @@ Node get(Node *node, int n)
|
||||
return *node;
|
||||
}
|
||||
|
||||
int len(Node *n)
|
||||
int len(Unit *n)
|
||||
{
|
||||
int res = 0;
|
||||
while (n != NULL)
|
||||
@@ -30,11 +30,11 @@ int len(Node *n)
|
||||
return res;
|
||||
}
|
||||
|
||||
Node *xor_find(Node *n1, Node *n2)
|
||||
Unit *xor_find(Unit *n1, Unit *n2)
|
||||
{
|
||||
int i, j, f = 0;
|
||||
Node *res = NULL;
|
||||
if ((res = malloc(sizeof(Node))) == NULL)
|
||||
Unit *res = NULL;
|
||||
if ((res = malloc(sizeof(Unit))) == NULL)
|
||||
{
|
||||
printf("could not malloc\n");
|
||||
exit(3);
|
||||
@@ -42,10 +42,10 @@ Node *xor_find(Node *n1, Node *n2)
|
||||
|
||||
for (i = 0; i < len(n1); i++)
|
||||
{
|
||||
Node tmp1 = get(n1, i);
|
||||
Unit tmp1 = get(n1, i);
|
||||
for (j = 0; j < len(n2); j++)
|
||||
{
|
||||
Node tmp2 = get(n2, j);
|
||||
Unit tmp2 = get(n2, j);
|
||||
if (strcmp(tmp1.name, tmp2.name) == 0)
|
||||
{
|
||||
f = 1;
|
||||
@@ -53,7 +53,7 @@ Node *xor_find(Node *n1, Node *n2)
|
||||
}
|
||||
if (f == 0)
|
||||
{
|
||||
Node tmp = get(n1, i);
|
||||
Unit tmp = get(n1, i);
|
||||
res = add_tail(res, tmp.name);
|
||||
}
|
||||
f = 0;
|
||||
@@ -61,10 +61,10 @@ Node *xor_find(Node *n1, Node *n2)
|
||||
|
||||
for (i = 0; i < len(n2); i++)
|
||||
{
|
||||
Node tmp2 = get(n2, i);
|
||||
Unit tmp2 = get(n2, i);
|
||||
for (j = 0; j < len(n1); j++)
|
||||
{
|
||||
Node tmp1 = get(n1, j);
|
||||
Unit tmp1 = get(n1, j);
|
||||
if (strcmp(tmp2.name, tmp1.name) == 0)
|
||||
{
|
||||
f = 1;
|
||||
@@ -72,7 +72,7 @@ Node *xor_find(Node *n1, Node *n2)
|
||||
}
|
||||
if (f == 0)
|
||||
{
|
||||
Node tmp = get(n2, i);
|
||||
Unit tmp = get(n2, i);
|
||||
res = add_tail(res, tmp.name);
|
||||
}
|
||||
f = 0;
|
||||
@@ -81,11 +81,11 @@ Node *xor_find(Node *n1, Node *n2)
|
||||
return res;
|
||||
}
|
||||
|
||||
Node *add_tail(Node *n, char s[])
|
||||
Unit *add_tail(Unit *n, char s[])
|
||||
{
|
||||
if (n == NULL)
|
||||
{
|
||||
if ((n = malloc(sizeof(Node))) == NULL)
|
||||
if ((n = malloc(sizeof(Unit))) == NULL)
|
||||
{
|
||||
printf("could not malloc\n");
|
||||
exit(1);
|
||||
@@ -103,11 +103,11 @@ Node *add_tail(Node *n, char s[])
|
||||
return n;
|
||||
}
|
||||
|
||||
Node *res = n;
|
||||
Unit *res = n;
|
||||
while (n->next != NULL)
|
||||
n = n->next;
|
||||
|
||||
Node *tmp = malloc(sizeof(Node));
|
||||
Unit *tmp = malloc(sizeof(Unit));
|
||||
memcpy(tmp->name, s, LEN);
|
||||
tmp->prev = n;
|
||||
n->next = tmp;
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define N 5
|
||||
#define LEN 5
|
||||
|
||||
typedef struct Unit
|
||||
{
|
||||
struct Unit *next;
|
||||
struct Unit *prev;
|
||||
char name[LEN];
|
||||
} Unit;
|
||||
|
||||
void node_print(Unit *n);
|
||||
Unit get(Unit *node, int n);
|
||||
Unit *xor_find(Unit *n1, Unit *n2);
|
||||
Unit *add_tail(Unit *n, char s[]);
|
||||
@@ -3,12 +3,12 @@
|
||||
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)
|
||||
Unit *n1 = NULL;
|
||||
Unit *n2 = NULL;
|
||||
Unit *n3 = NULL;
|
||||
if ((n1 = malloc(sizeof(Unit))) == NULL ||
|
||||
(n2 = malloc(sizeof(Unit))) == NULL ||
|
||||
(n3 = malloc(sizeof(Unit))) == NULL)
|
||||
{
|
||||
printf("Could not malloc\n");
|
||||
exit(2);
|
||||
@@ -32,14 +32,15 @@ int main(int argc, char *argv[])
|
||||
n2 = add_tail(n2, s);
|
||||
}
|
||||
|
||||
printf("Begin arrays:\n");
|
||||
printf("First array:\n");
|
||||
node_print(n1);
|
||||
printf("\n");
|
||||
printf("Second array:\n");
|
||||
node_print(n2);
|
||||
|
||||
n3 = xor_find(n1, n2);
|
||||
|
||||
printf("\nFinal array:\n");
|
||||
printf("\nThird array:\n");
|
||||
node_print(n3);
|
||||
|
||||
free(n1);
|
||||
Binary file not shown.
+38
-29
@@ -1,21 +1,20 @@
|
||||
#include "head.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
Stack get(Stack *l, int n) {
|
||||
if (n > len(l))
|
||||
exit(3);
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
void linked_print(Stack *l)
|
||||
{
|
||||
while (l != NULL)
|
||||
{
|
||||
printf("%lF\n", l->x);
|
||||
l = l->next;
|
||||
}
|
||||
return *l;
|
||||
}
|
||||
|
||||
Stack *add_head(Stack *l, double x) {
|
||||
if (l == NULL) {
|
||||
Stack *add_head(Stack *l, double x)
|
||||
{
|
||||
if (l == NULL)
|
||||
{
|
||||
l = malloc(sizeof(Stack));
|
||||
if (l == NULL)
|
||||
exit(1);
|
||||
@@ -23,7 +22,8 @@ Stack *add_head(Stack *l, double x) {
|
||||
l->next = NULL;
|
||||
}
|
||||
|
||||
if (l->x == 0.0) {
|
||||
if (l->x == 0.0)
|
||||
{
|
||||
l->x = x;
|
||||
l->next = NULL;
|
||||
return l;
|
||||
@@ -38,26 +38,12 @@ Stack *add_head(Stack *l, double 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) {
|
||||
Stack *del_head(Stack *l)
|
||||
{
|
||||
if (l == NULL)
|
||||
return NULL;
|
||||
else if (l->next == NULL) {
|
||||
else if (l->next == NULL)
|
||||
{
|
||||
free(l);
|
||||
l = NULL;
|
||||
return NULL;
|
||||
@@ -67,3 +53,26 @@ Stack *del_head(Stack *l) {
|
||||
l = NULL;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
int len(Stack *l)
|
||||
{
|
||||
int res = 0;
|
||||
while (l != NULL)
|
||||
{
|
||||
res++;
|
||||
l = l->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
+6
-6
@@ -1,13 +1,13 @@
|
||||
#define N 5
|
||||
|
||||
#define N 6
|
||||
|
||||
typedef struct Stack {
|
||||
typedef struct Stack
|
||||
{
|
||||
double x;
|
||||
struct Stack *next;
|
||||
} Stack;
|
||||
|
||||
Stack get(Stack *l, int n);
|
||||
void linked_print(Stack *l);
|
||||
Stack *add_head(Stack *l, double x);
|
||||
void stack_print(Stack *l);
|
||||
int len(Stack *l);
|
||||
Stack *del_head(Stack *l);
|
||||
int len(Stack *l);
|
||||
Stack get(Stack *l, int n);
|
||||
+23
-14
@@ -1,32 +1,40 @@
|
||||
#include "head.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Выдает неадекватные значения
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
srand(time(0));
|
||||
Stack *numbers = malloc(sizeof(Stack));
|
||||
if (numbers == NULL)
|
||||
exit(1);
|
||||
|
||||
char c;
|
||||
char flag = 'f';
|
||||
char flag = 'n';
|
||||
|
||||
while ((c = getchar()) != EOF) {
|
||||
if (c == '\n') {
|
||||
while ((c = getchar()) != EOF)
|
||||
{
|
||||
if (c == '\n')
|
||||
{
|
||||
break;
|
||||
} else if (c == ' ') {
|
||||
flag = 't';
|
||||
}
|
||||
else if (c == ' ')
|
||||
{
|
||||
flag = 's';
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
switch (c)
|
||||
{
|
||||
case '0' ... '9':
|
||||
if (flag == 't') {
|
||||
if (flag == 's')
|
||||
{
|
||||
numbers = add_head(numbers, (double)(c - '0'));
|
||||
flag = 'f';
|
||||
} else {
|
||||
flag = 'n';
|
||||
}
|
||||
else
|
||||
{
|
||||
double a = get(numbers, 0).x;
|
||||
numbers = del_head(numbers);
|
||||
a = a * 10 + c - '0';
|
||||
@@ -38,7 +46,8 @@ int main(int argc, char *argv[]) {
|
||||
case '-':
|
||||
case '*':
|
||||
case '/':
|
||||
if (len(numbers) < 2) {
|
||||
if (len(numbers) < 2)
|
||||
{
|
||||
printf("Invalid expression\n");
|
||||
exit(2);
|
||||
}
|
||||
@@ -58,7 +67,7 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
printf("result: %lF\n", get(numbers, -1).x);
|
||||
printf("res: %lF\n", get(numbers, 0).x);
|
||||
|
||||
free(numbers);
|
||||
return 0;
|
||||
|
||||
Executable
BIN
Binary file not shown.
+44
-14
@@ -2,7 +2,7 @@
|
||||
#include <stdlib.h>
|
||||
#include "head.h"
|
||||
|
||||
tree *init_root(tree *root, int field) // Выделение памяти под корень дереваnode
|
||||
tree *init_root(tree *root, int field) // Выделение памяти под корень дерева
|
||||
{
|
||||
tree *tmp = malloc(sizeof(tree)); // Присваивание значения ключу
|
||||
tmp->field = field; // Присваивание указателю на родителя значения NULL
|
||||
@@ -28,8 +28,10 @@ tree *add_node(tree *root, int field)
|
||||
tmp->parent = root3; // Присваивание указателю на левое и правое поддерево значения NULL
|
||||
tmp->left = NULL;
|
||||
tmp->right = NULL;
|
||||
if (field < root3 -> field) root3 -> left = tmp;
|
||||
else root3 -> right = tmp;
|
||||
if (field < root3->field)
|
||||
root3->left = tmp;
|
||||
else
|
||||
root3->right = tmp;
|
||||
return root;
|
||||
}
|
||||
|
||||
@@ -39,7 +41,8 @@ tree *find_node(tree * root, int field) // Если дерево пусто ил
|
||||
return root; // Поиск нужного узла
|
||||
if (field < root->field)
|
||||
return find_node(root->left, field);
|
||||
else return find_node(root -> right, field);
|
||||
else
|
||||
return find_node(root->right, field);
|
||||
}
|
||||
|
||||
// Минимальный элемент дерева
|
||||
@@ -83,24 +86,30 @@ tree *del_node(tree *root, int field)
|
||||
if ((lef->left == NULL) && (lef->right == NULL))
|
||||
{
|
||||
tmp = lef->parent;
|
||||
if (lef == tmp -> right) tmp -> right = NULL;
|
||||
else tmp -> left = NULL;
|
||||
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;
|
||||
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;
|
||||
if (lef == tmp->right)
|
||||
tmp->right = lef->left;
|
||||
else
|
||||
tmp->left = lef->left;
|
||||
free(lef);
|
||||
}
|
||||
// 3 случай: оба потомка
|
||||
@@ -110,18 +119,39 @@ tree *del_node(tree *root, int field)
|
||||
lef->field = tmp->field;
|
||||
if (tmp->right == NULL)
|
||||
tmp->parent->left = NULL;
|
||||
else tmp -> parent -> left = tmp -> right;
|
||||
else
|
||||
tmp->parent->left = tmp->right;
|
||||
free(tmp);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
void print_tree(tree *root)
|
||||
void preorder(tree *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
return;
|
||||
if (root -> field)
|
||||
printf("%d ", root -> field);
|
||||
print_tree(root -> left);
|
||||
print_tree(root -> right);
|
||||
preorder(root -> left);
|
||||
preorder(root -> right);
|
||||
}
|
||||
|
||||
void postorder(tree *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
return;
|
||||
postorder(root -> left);
|
||||
postorder(root -> right);
|
||||
if (root -> field)
|
||||
printf("%d ", root -> field);
|
||||
}
|
||||
|
||||
void inorder(tree *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
return;
|
||||
inorder(root -> left);
|
||||
if (root -> field)
|
||||
printf("%d ", root -> field);
|
||||
inorder(root -> right);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
typedef struct tnode {
|
||||
typedef struct tnode
|
||||
{
|
||||
int field;
|
||||
struct tnode *left;
|
||||
struct tnode *right;
|
||||
@@ -12,4 +13,6 @@ tree *minim(tree *root);
|
||||
tree *maxim(tree *root);
|
||||
tree *findForDel(tree *root);
|
||||
tree *del_node(tree *root, int field);
|
||||
void print_tree(tree *root);
|
||||
void preorder(tree *root);
|
||||
void inorder(tree *root);
|
||||
void postorder(tree *root);
|
||||
+23
-17
@@ -1,25 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include <stdio.h>
|
||||
#include "head.h"
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
// Дерево улетает в SF
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
tree *root = NULL;
|
||||
root = init_root(root, 65);
|
||||
print_tree(root);
|
||||
root = init_root(root, 'A');
|
||||
preorder(root);
|
||||
printf("\n");
|
||||
root=del_node(root, 65);
|
||||
print_tree(root);
|
||||
root = del_node(root, 'A');
|
||||
preorder(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);
|
||||
root = add_node(root, 'F');
|
||||
root = add_node(root, 'B');
|
||||
root = add_node(root, 'D');
|
||||
root = add_node(root, 'G');
|
||||
root = add_node(root, 'C');
|
||||
root = add_node(root, 'H');
|
||||
preorder(root);
|
||||
printf("\n");
|
||||
root = del_node(root, 66);
|
||||
print_tree(root);
|
||||
postorder(root);
|
||||
printf("\n");
|
||||
inorder(root);
|
||||
printf("\n");
|
||||
root = del_node(root, 'B');
|
||||
preorder(root);
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -1,18 +0,0 @@
|
||||
#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[]);
|
||||
Reference in New Issue
Block a user