diff --git a/APL1.2/TP10/balises.c b/APL1.2/TP10/balises.c new file mode 100644 index 0000000..8fb0d89 --- /dev/null +++ b/APL1.2/TP10/balises.c @@ -0,0 +1,44 @@ +#include +#include +#include "chainee.c" + +int isBal(char c) { + return (c == '<') || (c == '>'); +} + +int opening(char c) { + return (c == '<'); +} + +int isMatching(char a, char b) { + return (a == '<' && b == '>'); +} + +int main(int argc, char* argv[]) { + FILE* f = fopen(argv[1], "r"); + pile p; + + if (f) { + int valid = 1; + while (!feof(f)) { + char c = fgetc(f); + if (isBal(c)) { + if (opening(c)) push(&p, c); + else { + char par = pop(&p); + if (!isMatching(par, c)) { + printf("Le fichier %s est mal balisé !\n", argv[1]); + valid = 0; + break; + } + } + } + } + + if (valid) printf("Le fichier %s est bien balisé !\n", argv[1]); + + fclose(f); + } else puts("Error opening file."); + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/APL1.2/TP10/chainee_tables.c b/APL1.2/TP10/chainee_tables.c index 2cdb9a8..d4fbccd 100644 --- a/APL1.2/TP10/chainee_tables.c +++ b/APL1.2/TP10/chainee_tables.c @@ -1,35 +1,45 @@ #include #include -typedef char* pile; +struct pile_ { + char* tab; + int size; +}; + +typedef struct pile_ pile; void push(pile* p, char c) { - p = (pile*)realloc(p, sizeof(*p) + sizeof(char)); - (*p)[(sizeof(*p)/sizeof(char))-1] = c; + (p->tab)[p->size] = c; + p->size++; + p->tab = realloc(p->tab, (p->size)+1); } char pop(pile* p) { - char c = (*p)[(sizeof(*p)/sizeof(char))-1]; - p = realloc(p, sizeof(*p) - sizeof(char)); + char c = (p->tab)[p->size-1]; + p->size--; + p->tab = realloc(p->tab, (p->size)+1); return c; } int empty(pile p) { - return p[0] == 0; + return p.size == 0; } char top(pile p) { - return p[(sizeof(p)/sizeof(char))-1]; + return (p.tab)[(p.size) - 1]; } void clear(pile* p) { - p = realloc(p, sizeof(char)); - (*p)[0] = 0; + free(p->tab); + p->tab = calloc(1, sizeof(int)); + p->size = 0; } pile create() { - pile p = (pile)calloc(1, sizeof(char)); - return p; + pile* p = (calloc(1, sizeof(pile))); + p->tab = calloc(1, sizeof(char)); + p->size = 0; + return *p; } pile destroy(pile* p) { diff --git a/APL1.2/TP11/chainee.c b/APL1.2/TP11/chainee.c new file mode 100644 index 0000000..bb4f23a --- /dev/null +++ b/APL1.2/TP11/chainee.c @@ -0,0 +1,34 @@ +#include +#include + +struct maillon_ { + char c; + struct maillon_* next; +}; + +typedef struct maillon_ maillon; +typedef maillon* file; + +void push(file* f, char c) { + maillon* m = calloc(1, sizeof(maillon)); + m->c = c; + + if (*f == NULL) { + *f = m; + } else { + file* start = f; + file ff = *f; + while (ff->next != NULL) ff = ff->next; + ff->next = m; + f = start; + } +} + +char pop(file* f) { + char c = (*f)->c; + *f = (*f)->next; +} + +int empty(file f) { + return f == NULL; +} \ No newline at end of file diff --git a/APL1.2/TP11/test_chainee.c b/APL1.2/TP11/test_chainee.c new file mode 100644 index 0000000..8954d5a --- /dev/null +++ b/APL1.2/TP11/test_chainee.c @@ -0,0 +1,22 @@ +#include +#include +#include "chainee.c" + +int main(int argc, char* argv[]) { + file p; + puts("La file attend vos ordres."); + + char input[10]; + do { + fgets(input, 10, stdin); + if (input[0] == '+') { + push(&p, input[1]); + printf("Le caractère %c a été ajouté.\n", input[1]); + } else if (input[0] == '-') { + if (!empty(p)) { + char c = pop(&p); + printf("Le caractère %c à été supprimé\n", c); + } else puts("La file est vide !"); + } + } while (input[0] != 'q' && input[0] != 'Q'); +} \ No newline at end of file