Pile / File

This commit is contained in:
HORVILLE 2022-01-18 15:45:21 +01:00
parent d1869af19f
commit c30ba3ccdf
4 changed files with 121 additions and 11 deletions

44
APL1.2/TP10/balises.c Normal file
View File

@ -0,0 +1,44 @@
#include <stdio.h>
#include <stdlib.h>
#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;
}

View File

@ -1,35 +1,45 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
typedef char* pile; struct pile_ {
char* tab;
int size;
};
typedef struct pile_ pile;
void push(pile* p, char c) { void push(pile* p, char c) {
p = (pile*)realloc(p, sizeof(*p) + sizeof(char)); (p->tab)[p->size] = c;
(*p)[(sizeof(*p)/sizeof(char))-1] = c; p->size++;
p->tab = realloc(p->tab, (p->size)+1);
} }
char pop(pile* p) { char pop(pile* p) {
char c = (*p)[(sizeof(*p)/sizeof(char))-1]; char c = (p->tab)[p->size-1];
p = realloc(p, sizeof(*p) - sizeof(char)); p->size--;
p->tab = realloc(p->tab, (p->size)+1);
return c; return c;
} }
int empty(pile p) { int empty(pile p) {
return p[0] == 0; return p.size == 0;
} }
char top(pile p) { char top(pile p) {
return p[(sizeof(p)/sizeof(char))-1]; return (p.tab)[(p.size) - 1];
} }
void clear(pile* p) { void clear(pile* p) {
p = realloc(p, sizeof(char)); free(p->tab);
(*p)[0] = 0; p->tab = calloc(1, sizeof(int));
p->size = 0;
} }
pile create() { pile create() {
pile p = (pile)calloc(1, sizeof(char)); pile* p = (calloc(1, sizeof(pile)));
return p; p->tab = calloc(1, sizeof(char));
p->size = 0;
return *p;
} }
pile destroy(pile* p) { pile destroy(pile* p) {

34
APL1.2/TP11/chainee.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
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;
}

View File

@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#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');
}