diff --git a/ex1/ex1.c b/ex1/ex1.c index fa0a47c033c4341fbd26b9ffd973c89ed9015e1b..0687cd1cf12e6f92e3cd2a928757125df8ca3e25 100644 --- a/ex1/ex1.c +++ b/ex1/ex1.c @@ -1,10 +1,11 @@ // lien du git : https://githepia.hesge.ch/leo.harb/examen_programmation_2023 #include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> typedef struct element_t { - struct element_t *prev; - float value; + int value; struct element_t *next; } element; @@ -14,14 +15,159 @@ typedef struct ll_t element *pos; }ll; +ll ll_create(){ + ll res; + res.head = NULL; + res.pos = NULL; + return res; +} + +void ll_destroy(ll *list){ + list->pos = list->head; + while (list->pos != NULL) + { + element *tmp = list->pos->next; + free(list->pos); + list->pos = tmp; + } + list->head = NULL; + list->pos = NULL; +} + +bool ll_is_empty(ll list){ + return list.head == NULL; +} + +// Est-ce que pos est le 1er élément? +bool ll_is_head(ll list){ + return list.pos == list.head; +} + +// Est-ce que pos est le dernier élément? +bool ll_is_tail(ll list){ + return list.pos->next == NULL; +} + +// data est-elle dans la liste? +bool ll_is_present(ll list, float data){ + while (list.pos->next != NULL) + { + if(list.pos->value == data){ + return true; + } + list.pos = list.pos->next; + } + return false; +} + +void ll_push(ll *list, int data){ + element *el = (element*)malloc(sizeof(element)); + el->value = data; + el->next = list->head; + list->head = el; + list->pos = el; +} + +void ll_insert(ll *list, int data){ + element *el = (element*)malloc(sizeof(element)); + if(list->pos == list->head){ + list->head = el; + } + el->next = list->pos; + element *tmp = list->head; + while (tmp->next!=list->pos); + { + tmp = tmp->next; + } + tmp->next = el; +} + +// affiche la liste +void ll_print(ll *list){ + while (list->pos != NULL) + { + printf("%f ", list->pos->value); + list->pos = list->pos->next; + } +} + +bool ll_contains(ll *list, int data){ + element* tmp = list->head; + while (tmp != NULL) + { + if(tmp->value == data){ + return true; + } + } + return false; +} + +void ll_sort_in(ll *list1, ll *list2, ll *res){ + list1->pos = list1->head; + res->pos = res->head; + while (list1->pos != NULL) + { + if(!ll_contains(res, list1->pos->value)){ + while (res->pos->value <= list1->pos->value) + { + res->pos = res->pos->next; + } + ll_insert(res, list1->pos->value); + } + list1->pos = list1->pos->next; + } + while (list2->pos != NULL) + { + if(!ll_contains(res, list2->pos->value)){ + while (res->pos->value <= list2->pos->value) + { + res->pos = res->pos->next; + } + ll_insert(res, list2->pos->value); + } + list2->pos = list2->pos->next; + } +} + int main(){ int len1 = 0; int len2 = 0; - int c = 0,r = 0; - char cmd = 0; - char entry[10]; - printf("Etrez la liste 1:\n"); - scanf("%[^\n]%*c", entry); - sscanf(entry, "%d %s", &cmd, &c, &r); + float values1[256]; + float values2[256]; + int left = 0; + char string1[256]; + char string2[256]; + + printf("Entrez la liste 1:"); + scanf("%[^\n]%*c", string1); + + float temp1; + char *p1 = string1; + int cpt1 = 0; + while(sscanf(p, "%f%n", &temp1, &left) == 1) { + values1[cpt1] = temp1; + p += left; + cpt1++; + } + + printf("Entrez la liste 2:"); + scanf("%[^\n]%*c", string1); + float temp2; + char *p2 = string2; + int cpt2 = 0; + while(sscanf(p, "%f%n", &temp2, &left) == 1) { + values1[cpt1] = temp2; + p += left; + cpt1++; + } + + + ll list1 = ll_create(); + ll list2 = ll_create(); + ll res = ll_create(); + + + dll_print(&list); + dll_destroy(&list); return 0; } \ No newline at end of file diff --git a/ex3/ex3.c b/ex3/ex3.c index c56c1c2306c07fd265718b1de249911f8c03f03c..77217319e0f52027aab09b7fd6de3aa45c07ffc7 100644 --- a/ex3/ex3.c +++ b/ex3/ex3.c @@ -90,7 +90,7 @@ int main() { int left = 0; char string[256]; - printf("Liste d'entiers: \n"); + printf("Liste d'entiers: "); scanf("%[^\n]%*c", string); float temp;