add calkBT

This commit is contained in:
ada-dmitry
2023-05-23 16:20:15 +03:00
parent 68f659165b
commit d8e322f93c
8 changed files with 495 additions and 139 deletions
Binary file not shown.
+105 -109
View File
@@ -5,7 +5,9 @@
#include <time.h>
#include <string.h>
void pr(tower *tmp)
#define max(x, y) (((x) > (y)) ? (x) : (y))
void pr(node *tmp)
{
if (tmp == NULL)
{
@@ -14,63 +16,52 @@ void pr(tower *tmp)
}
while (tmp)
{
printf("[%14p]<-[%14p]->[%14p] ", tmp->prev, tmp, tmp->next);
printf("data: [%4ld] s[%s]\n", tmp->data, tmp->s);
printf("[%14p]->[%14p] ", tmp, tmp->next);
printf("size: [%4d]\n", tmp->size);
tmp = tmp->next;
}
printf("-------------\n");
}
tower *add_head(tower *crown, int hh)
node *add_head(node *head, int hh)
{
tower *tmp = NULL;
int i;
if ((tmp = malloc(sizeof(tower))) == NULL)
node *tmp = NULL;
if ((tmp = malloc(sizeof(node))) == NULL)
{
perror("malloc: NULL");
exit(2);
}
tmp->prev = NULL;
tmp->data = hh;
for (i = 0; i < N - 1; i++)
tmp->s[i] = ((char)(65 + rand() % 25));
tmp->s[N - 1] = '\0';
if (crown == NULL)
tmp->size = hh;
if (head == NULL)
tmp->next = NULL;
else
{
tmp->next = crown;
crown->prev = tmp;
tmp->next = head;
}
return tmp;
}
tower *del_head(tower *crown)
node *del_head(node *head)
{
tower *tmp = crown;
if (crown == NULL)
node *tmp = head;
if (head == NULL)
return NULL;
if (crown->next == NULL)
if (head->next == NULL)
{
free(crown);
free(head);
return NULL;
}
crown = crown->next;
crown->prev = NULL;
head = head->next;
free(tmp);
return crown;
return head;
}
int length(tower *list)
int length(node *list)
{
tower *tmp = list;
node *tmp = list;
int res = 0;
if (list == NULL)
return 0;
if (list->data == 0 && !strcmp(list->s, "\0"))
if (list == NULL || list->size == 0)
return 0;
else
{
while (tmp->prev != NULL)
tmp = tmp->prev;
while (tmp != NULL)
{
res++;
@@ -80,41 +71,18 @@ int length(tower *list)
return res;
}
tower *adv_add_head(tower *crown, int hh, char string[])
void swap(node *a, node *b)
{
tower *tmp = NULL;
if ((tmp = malloc(sizeof(tower))) == NULL)
{
perror("malloc: NULL");
exit(2);
}
tmp->prev = NULL;
tmp->data = hh;
memcpy(tmp->s, string, N);
if (crown == NULL)
tmp->next = NULL;
else
{
tmp->next = crown;
crown->prev = tmp;
}
return tmp;
}
void swap(tower *a, tower *b)
{
tower *tmp = NULL;
if ((tmp = malloc(sizeof(tower))) == NULL)
node *tmp = NULL;
if ((tmp = malloc(sizeof(node))) == NULL)
exit(1);
tmp->data = a->data;
a->data = b->data;
b->data = tmp->data;
memcpy(tmp->s, a->s, N);
memcpy(a->s, b->s, N);
memcpy(b->s, tmp->s, N);
tmp->size = a->size;
a->size = b->size;
b->size = tmp->size;
free(tmp);
}
tower *get(tower *list, int n)
node *get(node *list, int n)
{
if (n >= length(list))
exit(3);
@@ -124,73 +92,101 @@ tower *get(tower *list, int n)
return list;
}
tower *sort(tower *crown)
node *sort(node *head)
{
if (crown == NULL || crown->next == NULL)
return crown;
int i, j, n = length(crown);
if (head == NULL || head->next == NULL)
return head;
int i, j, n = length(head);
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (get(crown, j)->data < get(crown, i)->data)
swap(get(crown, i), get(crown, j));
if (get(head, j)->size < get(head, i)->size)
swap(get(head, i), get(head, j));
}
}
return crown;
return head;
}
tower *printextra(tower *crown, int len, int i)
pointer *add_head_address(pointer *head, node **address)
{
if (len < i || len == 0)
pointer *tmp = NULL;
if ((tmp = malloc(sizeof(pointer))) == NULL)
{
printf("*********************************|");
return crown;
perror("malloc: NULL");
exit(2);
}
tmp->address = address;
if (head == NULL)
tmp->next = NULL;
else
{
printf("[%14p] data: [%4ld] s[%s]|", crown, crown->data, crown->s);
return crown->prev;
tmp->next = head;
}
}
tower *end(tower *crown)
{
tower *tmp = NULL;
tmp = crown;
if (crown == NULL)
return crown;
while (tmp->next != NULL)
tmp = tmp->next;
return tmp;
}
void printftof(tower *head1, tower *head2, tower *head3)
node *printextra(node *head, int len, int i)
{
int a = length(head1), b = length(head2), c = length(head3), maximum, i;
maximum = max(a, b);
maximum = max(maximum, c);
head1 = end(head1);
head2 = end(head2);
head3 = end(head3);
for (i = maximum; i > 0; i--)
if (len < i || len == 0)
{
for (i = 0; i < T - 1; i++)
printf(" ");
printf("|");
return head;
}
else
{
disk(head->size);
printf("|");
return head->next;
}
}
void prcount(char s, int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%c", s);
}
}
void disk(int size)
{
int i;
prcount(' ', T / 2 - size);
for (i = 0; i < size - 1; i++)
{
printf("* ");
}
printf("*");
prcount(' ', T / 2 - size);
}
void printftof(pointer *adresses)
{
node *head1 = *(adresses->address), *head2 = *(adresses->next->address), *head3 = *(adresses->next->next->address);
int a = length(head1), b = length(head2), c = length(head3), i; // maximum
// maximum = max(max(a, b), c);
for (i = T / 2; i > 0; i--)
{
head1 = printextra(head1, a, i);
head2 = printextra(head2, b, i);
head3 = printextra(head3, c, i);
printf("\n");
}
printf("----------------------------------------------------------------------------------------------------------\n");
printf("Tower 1 |Tower 2 |Tower 3 |\n");
printf("----------------------------------------------------------------------------------------------------------\n\n");
}
int max(int a, int b)
{
if (a < b)
return b;
else
return a;
prcount('-', 3 * T);
printf("\n");
printf("Tower 1");
prcount(' ', T - 8);
printf("|Tower 2");
prcount(' ', T - 8);
printf("|Tower 3");
prcount(' ', T - 8);
printf("|\n");
prcount('-', 3 * T);
printf("\n");
}
void hanoi_tower(int n, tower **head_from, tower **head_to, tower **head_aux, tower **const tower1, tower **const tower2, tower **const tower3)
void hanoi_tower(int n, node **head_from, node **head_to, node **head_aux, pointer *adresses)
{
if (n == 0)
{
@@ -200,17 +196,17 @@ void hanoi_tower(int n, tower **head_from, tower **head_to, tower **head_aux, to
else if (n == 1)
{
if (length(*head_to) == 0)
printftof(*tower1, *tower2, *tower3);
*head_to = adv_add_head(*head_to, (*head_from)->data, (*head_from)->s);
printftof(adresses);
*head_to = add_head(*head_to, (*head_from)->size);
*head_from = del_head(*head_from);
if (length(*head_from) == 0)
printftof(*tower1, *tower2, *tower3);
printftof(adresses);
return;
}
hanoi_tower(n - 1, head_from, head_aux, head_to, tower1, tower2, tower3);
printftof(*tower1, *tower2, *tower3);
*head_to = adv_add_head(*head_to, (*head_from)->data, (*head_from)->s);
hanoi_tower(n - 1, head_from, head_aux, head_to, adresses);
printftof(adresses);
*head_to = add_head(*head_to, (*head_from)->size);
*head_from = del_head(*head_from);
printftof(*tower1, *tower2, *tower3);
hanoi_tower(n - 1, head_aux, head_to, head_from, tower1, tower2, tower3);
printftof(adresses);
hanoi_tower(n - 1, head_aux, head_to, head_from, adresses);
}
+20 -13
View File
@@ -1,28 +1,35 @@
#include "prot.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
tower *crown1 = NULL, *crown2 = NULL, *crown3 = NULL;
node *head = NULL, *head2 = NULL, *head3 = NULL;
pointer *addresses = NULL;
int r;
srand(time(NULL));
printf("Generated linked list (method of crown1)\n");
while ((r = (rand() % 100)) < 75)
crown1 = add_head(crown1, r);
pr(crown1);
printf("Length of linked list is: %d\n", length(crown1));
printf("Generated linked list (method of head)\n");
/*while ((r=(rand()%T)) < T/2){
if (r==0) r+=1;
head = add_head(head, r);
}*/
for (r = 1; r <= T / 2; r++)
{
head = add_head(head, r);
}
pr(head);
printf("Length of linked list is: %d\n", length(head));
printf("Sorted linked list\n");
sort(crown1);
pr(crown1);
hanoi_tower(length(crown1), &crown1, &crown3, &crown2, &crown1, &crown2, &crown3);
sort(head);
pr(head);
addresses = add_head_address(addresses, &head3);
addresses = add_head_address(addresses, &head2);
addresses = add_head_address(addresses, &head);
hanoi_tower(length(head), &head, &head3, &head2, addresses);
return 0;
}
+21 -17
View File
@@ -1,23 +1,27 @@
#define N 10
#define T 20
typedef struct Node
{
long int data;
int size;
struct Node *next;
struct Node *prev;
char s[N];
} tower;
} node;
typedef struct nodepointer
{
struct nodepointer *next;
struct Node **address;
} pointer;
void pr(tower *tmp);
tower *add_head(tower *crown, int hh);
tower *del_head(tower *crown);
int length(tower *list);
tower *sort(tower *crown);
tower *get(tower *list, int n);
void swap(tower *a, tower *b);
tower *adv_add_head(tower *crown, int hh, char string[]);
void hanoi_tower(int n, tower **head_from, tower **head_to, tower **head_aux, tower **const tower1, tower **const tower2, tower **const tower3);
void printftof(tower *head1, tower *head2, tower *head3);
tower *end(tower *crown);
tower *printextra(tower *crown, int len, int i);
int max(int a, int b);
void pr(node *tmp);
node *add_head(node *head, int hh);
node *del_head(node *head);
pointer *add_head_address(pointer *head, node **address);
int length(node *list);
node *sort(node *head);
node *get(node *list, int n);
void swap(node *a, node *b);
void hanoi_tower(int n, node **head_from, node **head_to, node **head_aux, pointer *adresses);
void printftof(pointer *adresses);
node *printextra(node *head, int len, int i);
void disk(int size);
void prcount(char s, int n);
Binary file not shown.
+312
View File
@@ -0,0 +1,312 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <error.h>
#include "head.h"
Tree *tnew(void)
{
Tree *tmp;
if ((tmp = malloc(sizeof(Tree))) == NULL)
{
perror("malloc:");
exit(1);
}
tmp->key = 0;
tmp->left = NULL;
tmp->right = NULL;
return tmp;
}
Tree *leaf(char c)
{
Tree *tmp;
tmp = tnew();
tmp->key = c;
return tmp;
}
Tree *maketree(char expr[], int first, int last)
{
Tree *root;
int k;
if (first == last)
{
return leaf(expr[first]);
}
k = lastop(expr, first, last);
root = tnew();
root->key = expr[k];
root->left = maketree(expr, first, k - 1);
root->right = maketree(expr, k + 1, last);
return root;
}
unsigned char priority(char c)
{
if ((c - '0' >= 0) && (c - '0' <= 9))
{
return MPRT;
}
switch (c)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return c;
}
int lastop(char expr[], int first, int last)
{
unsigned char min, prt;
int i, k;
min = MPRT;
for (i = first; i <= last; i++)
{
prt = priority(expr[i]);
if (prt <= min)
{
min = prt;
k = i;
}
}
return k;
}
int calculate(Tree *root)
{
int num1, num2;
if (!(root->left))
{
return root->key - 0x30;
}
num1 = calculate(root->left);
num2 = calculate(root->right);
switch (root->key)
{
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
case '^':
return pow(num1, num2);
}
return 0xffffffff;
}
void showtree(Tree *root, int level)
{
if ((root->key > 31) && (root->key < 127))
{
printf("%c ", root->key);
}
else
{
printf("%X ", root->key);
}
if (root->right)
{
printf("------> ");
level++;
showtree(root->right, level);
level--;
}
if (root->left)
{
int i = 0;
printf("\n");
for (i = 0; i < level; i++)
{
printf(" ");
}
printf("| \n");
for (i = 0; i < level; i++)
{
printf(" ");
}
showtree(root->left, level);
}
}
Tree *create(unsigned char key)
{
Tree *tmp;
if ((tmp = malloc(sizeof(Tree))) == NULL)
{
perror("malloc:");
exit(1);
}
tmp->key = key;
tmp->parent = NULL;
tmp->left = NULL;
tmp->right = NULL;
return tmp;
}
Tree *add(Tree *root, unsigned char key)
{
Tree *rtmp = root, *rsave = NULL, *tmp;
if ((tmp = malloc(sizeof(Tree))) == NULL)
{
perror("malloc:");
exit(1);
}
while (rtmp)
{
rsave = rtmp;
if (key < rtmp->key)
{
rtmp = rtmp->left;
}
else if (key > rtmp->key)
{
rtmp = rtmp->right;
}
else
{
printf("\n There is element %c \n", key);
return root;
}
}
tmp->parent = rsave;
tmp->left = NULL;
tmp->right = NULL;
tmp->key = key;
if (key < rsave->key)
{
rsave->left = tmp;
}
else
{
rsave->right = tmp;
}
return root;
}
Tree *search(Tree *root, unsigned char key)
{
if ((root == NULL) || (root->key == key))
{
return root;
}
if (key < root->key)
{
return search(root->left, key);
}
else if (key > root->key)
{
return search(root->right, key);
}
}
Tree *min(Tree *root)
{
while (root->left != NULL)
{
root = root->left;
}
return root;
}
Tree *max(Tree *root)
{
while (root->right != NULL)
{
root = root->right;
}
return root;
}
Tree *nextdel(Tree *root)
{
Tree *l = NULL;
if (root->right != NULL)
{
return min(root->right);
}
l = root->parent;
while ((l != NULL) && (root == l->right))
{
root = l;
l = l->parent;
}
return l;
}
void delete(Tree *del)
{
Tree *tmp;
if (del == NULL)
{
printf("\n no such elem \n");
return;
}
if ((del->left == NULL) && (del->right == NULL))
{
if (del == del->parent->right)
{
del->parent->right = NULL;
}
else
{
del->parent->right = NULL;
}
free(del);
return;
}
if ((del->left == NULL) && (del->right != NULL))
{
if (del == del->parent->right)
{
del->parent->right = del->right;
del->parent->right->parent = del->parent;
}
else
{
del->parent->left = del->right;
del->parent->left->parent = del->parent;
}
free(del);
return;
}
if ((del->left != NULL) && (del->right == NULL))
{
if (del == del->parent->right)
{
del->parent->right = del->left;
del->parent->right->parent = del->parent;
}
else
{
del->parent->left = del->left;
del->parent->left->parent = del->parent;
}
free(del);
return;
}
if ((del->left != NULL) && (del->right != NULL))
{
tmp = min(del->right);
del->key = tmp->key;
delete (tmp);
}
}
+10
View File
@@ -0,0 +1,10 @@
#define MPRT 255
#define SIZE 80
typedef struct tree
{
unsigned char key;
struct tree *parent;
struct tree *left;
struct tree *right;
} Tree;
+27
View File
@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <error.h>
#include "head.h"
int main(int argc, char *argv[])
{
char s[SIZE];
Tree *tree = NULL;
if (argc < 2)
{
printf("no expression\n");
return 2;
}
strcpy(s, argv[1]);
printf("processing %s\n", s);
tree = maketree(s, 0, strlen(s) - 1);
printf("\n tree: \n");
showtree(tree, 0);
printf("= %d \n", calculate(tree));
return 0;
}