diff --git a/classification/Makefile b/classification/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..1bf6f7e91490a9ff8887c7e347f4f13e2ac57644 --- /dev/null +++ b/classification/Makefile @@ -0,0 +1,14 @@ +cc=gcc +CFLAGS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak +LFLAGS= -lm + + +main: class.x + ./class.x + +class.x: class.o main.o + $(cc) -o $@ $^ $(CFLAGS) $(LFLAGS) + +clean: + rm -f *.o *.x + diff --git a/classification/class.c b/classification/class.c new file mode 100644 index 0000000000000000000000000000000000000000..e57a9fd43ce0bbbc5e54e8af3913b88236443e50 --- /dev/null +++ b/classification/class.c @@ -0,0 +1,133 @@ +#include "class.h" +#include <stdio.h> +#include <stdlib.h> + +node init_node(double value) { + node n; + n.value = value; + n.child[0] = NULL; + n.child[1] = NULL; + return n; +} + +element *createElement(double value) { + element *tmp = calloc(1, sizeof(element)); + tmp->next = NULL; + tmp->nd = init_node(value); + return tmp; +} + +void swap(double *a, double *b) { + double tmp = *a; + *a = *b; + *b = tmp; +} + + +// bubble sort +void sort(double *arr, int size) { + + for (int j = size; j > 1; j--) { + + for (int i = 0; i < j - 1; i++) { + if (arr[i] < arr[i + 1]) { + swap(&arr[i], &arr[i + 1]); + } + } + } +} + +// add at first place +element *push(element *head, double value) { + element *tmp = createElement(value); + tmp->next = head; + return tmp; +} + +void list_print(element *head) { + element *tmp = head; + + while (tmp != NULL) { + printf("%.2f ", tmp->nd.value); + tmp = tmp->next; + } + printf("\n"); +} + +element *createSortedList(double *values, int len) { + sort(values, len); + element *head = NULL; // empty + for (int i = 0; i < len; i++) { + head = push(head, values[i]); + } + list_print(head); + return head; +} + +int list_length(element *head) { + + if (head == NULL) { + return -1; + } + + int size = 0; + element *current = head; + while (current != NULL) { + size++; + current = current->next; + } + return size; +} + +void print_tree(node* tree, int depth) { // affiche l'arbre ordre croissant + + if (tree == NULL) { + return; + } + print_tree(tree->child[0], depth + 1); + for (int i = 0; i < depth; i++) { + printf("-------"); + } + printf(" %.2f \n\n", tree->value); + print_tree(tree->child[1], depth + 1); +} + +element *step(element *head, element *current) { + if (current->next != NULL) { + node nd1 = current->nd; + node nd2 = current->next->nd; + double ave = (nd1.value + nd2.value) / 2; + + current->nd = init_node(ave); + current->nd.child[0] = &nd1; + current->nd.child[1] = &nd2; + // remove next + current->next = current->next->next; + return current->next; + } else { + element* tmp=head; + while(tmp->next!=current){ + tmp=tmp->next; + } + node nd1 = tmp->nd; + node nd2 = current->nd; + double ave = (nd1.value + nd2.value) / 2; + + tmp->nd = init_node(ave); + tmp->nd.child[0] = &nd1; + tmp->nd.child[1] = &nd2; + // remove next + tmp->next=NULL; + return tmp; + } +} + +tree classify(element *head) { + element*current=head; + while (list_length(head) > 1) { + current=step(head,current); + list_print(head); + } + // print_tree(&head->nd, 0); + return &head->nd; +} \ No newline at end of file diff --git a/classification/class.h b/classification/class.h new file mode 100644 index 0000000000000000000000000000000000000000..d62d1e8fa82bf5286698808d446f6f19ad63a02a --- /dev/null +++ b/classification/class.h @@ -0,0 +1,28 @@ +#ifndef _CLASS_H_ +#define _CLASS_H_ + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + + +typedef struct _node { + double value; + struct _node *child[2]; +} node; + +typedef struct element { + node nd ; + struct element *next; +} element; + +typedef node* tree; + + +tree classify(element *head); + +element* createSortedList(double* values, int len); + + + +#endif diff --git a/classification/main.c b/classification/main.c new file mode 100644 index 0000000000000000000000000000000000000000..a75753a6e4715f3409d2f54bebe91475327ed217 --- /dev/null +++ b/classification/main.c @@ -0,0 +1,14 @@ +#include "class.h" +#include <stdlib.h> + + + + + +int main() { + + double arr[]={1.8,3.0,7.5,8.1,9.8}; + element* head=createSortedList(arr, 5); + classify(head); + return EXIT_SUCCESS; +} \ No newline at end of file