diff --git a/Labgen/a.out b/Labgen/a.out index 1776c76cb4eaf2afa28a505bbbb6bd0ca61d0195..6dc060296f6a65e13977c12e48cc264760244e59 100755 Binary files a/Labgen/a.out and b/Labgen/a.out differ diff --git a/Labgen/labgen.c b/Labgen/labgen.c index a952bdd1828ed907498a47245a6bb8a7e9c37626..4520ab817799466cd68d62f9630b7f03d5275182 100644 --- a/Labgen/labgen.c +++ b/Labgen/labgen.c @@ -1,5 +1,5 @@ #include "labgen.h" -point size; +point size; /*Attention c'est la taille réelle de la matrice*/ char *** matrice = NULL ; int in,out; variables listevars; @@ -15,21 +15,23 @@ point new_point(int x, int y) } -/* Crée une matrice de taille (s1, s2) */ +/* Crée une matrice de taille (s1, s2) + * /!\ Attention : toujours créer la matrice avant d'utiliser + * les autres fonctions */ void create_matrice(int s1, int s2) { int i,j; - matrice = malloc(s1*sizeof(char **)); - - for (i= 0; i<s1 ;i++){ - matrice[i] = malloc(s2 * sizeof(char *)); - for (j=0; j<s2; j++) - { - matrice[i][j] = malloc(sizeof(char)); - strcpy(matrice[i][j],""); - } + matrice = malloc((s1+1)*sizeof(char **)); + + for (i= 0; i<s1+1; i++){ + matrice[i] = malloc((s2+1) * sizeof(char *)); + for (j=0; j<s2+1; j++) { + matrice[i][j] = malloc(sizeof(char)); + strcpy(matrice[i][j],""); + } } - size = new_point(s1,s2); + size = new_point(s1+1,s2+1); + /*Attention c'est la taille réelle de la matrice*/ in = 0; out = 0; } @@ -38,10 +40,14 @@ void create_matrice(int s1, int s2) /* Renvoie la valeur de la matrice de coordonnées p */ char* lecture(point p) { - if (p.x > 0 && p.y > 0 && p.x < size.x && p.y < size.y) - { - return matrice[p.x][p.y]; /* Définir la matrice avant ? */ + if (matrice == NULL){ + printf("Matrice non initialisée.\n"); } + else if (p.x >= 0 && p.y >= 0 && p.x < size.x && p.y < size.y){ + return matrice[p.x][p.y]; + } + else + printf("Impossible de lire la valeur au point (%d,%d).\n", p.x,p.y); return NULL; } @@ -50,18 +56,27 @@ char* lecture(point p) /* Change la valeur de la matrice de coordonnées p par la valeur msg */ void change_val_matrice(point p, char * msg) { - char * m1; - if (p.x > 0 && p.y > 0 && p.x < size.x && p.y < size.y) - { + if (matrice == NULL){ + printf("Matrice non initialisée.\n"); + } + + else if (p.x >= 0 && p.y >= 0 && p.x < size.x && p.y < size.y) { + char * m1; m1 = malloc(strlen(msg)*sizeof(char)); strcpy(m1,msg); if (matrice[p.x][p.y] != NULL){ free(matrice[p.x][p.y]) ; } matrice[p.x][p.y] = m1; - } + } + else { + printf("Impossible de changer de valeur la valeur au point (%d,%d).\n", + p.x,p.y); + } } + +/* Initialise listevars */ void init_listevars(){ listevars.last = 0; } @@ -70,10 +85,10 @@ void init_listevars(){ /* Vérifie si var est dans listevars : * - si oui, renvoie l'adresse correspondante - sinon, renvoie NULL */ -int find_var (char* var) -{ +int find_var(char* var) { int i; int ret; + for (i=0; i<listevars.last; i++) { ret = strcmp(listevars.ident[i], var); if (ret==0) { @@ -89,51 +104,77 @@ int find_var (char* var) * directement modifiée * - Sinon rajoute le nom de la variable et sa valeur dans listevars si la taille de listevars le permet */ -void create_modif_var(char* nom, int val) -{ -int index = find_var(nom); +void create_modif_var(char* nom, int val) { + int index = find_var(nom); -if (index != -1) -{ - listevars.val[index] = val; + if (index != -1) + { + listevars.val[index] = val; + } + + else{ + if (listevars.last > TAILLE) { + printf("Il n'y a plus de place pour tous ces variables.\n"); + exit(1); + } + else + listevars.ident[listevars.last] = malloc(strlen(nom)*sizeof(char)); + + strcpy(listevars.ident[listevars.last],nom); + listevars.val[listevars.last] = val; + listevars.last++; + } } -else{ - if (listevars.last > TAILLE) - { - printf(" il n'y a plus de place pour tous ces variables\n"); - exit(1); - } - else - listevars.ident[listevars.last] = malloc(strlen(nom)*sizeof(char)); - strcpy(listevars.ident[listevars.last],nom); - listevars.val[listevars.last] = val; - listevars.last++; -}} +/*void ligne(){ + int j; + for (j=0;j<size.y;j++) + printf(" -------"); + printf("\n"); +}*/ +void affichmat() { + int i,j; + + /*ligne();*/ + for(i=0;i<size.x;i++){ + for(j=0;j<size.y;j++){ + printf("|%s \t",matrice[i][j]); + } + printf("|\n"); + /*ligne();*/ + } + /*ligne();*/ +} int main () { create_matrice(5,4); - point p1 = new_point(1,2); - change_val_matrice(p1, "ABC"); - printf("%s\n", lecture(p1)); + point p1 = new_point(5,4); + printf("Point p1 (%d,%d)\n",p1.x,p1.y); + printf("\nAppel de la fonction change_val_matrice(p1,ABC)\n"); + change_val_matrice(p1,"ABC"); + printf("Point p1 :%s\n", lecture(p1)); + printf("\nAppel de la fonction change_val_matrice(p1,DEF)\n"); change_val_matrice(p1, "DEF"); - printf("%s\n", lecture(p1)); + printf("Point p1 :%s\n", lecture(p1)); + + printf("\nTaille de la matrice : (%d,%d)\n",size.x-1,size.y-1); + printf("Matrice correspondante :\n"); + affichmat(matrice); init_listevars(); create_modif_var("IDENT",1); create_modif_var("IDENT",2); create_modif_var("IDENT",1); create_modif_var("IDENT2",5); - printf("Taille : %d\n",listevars.last); + printf("\n\nTaille listevars : %d\n",listevars.last); printf("Premier nom : %s\n",listevars.ident[0]); printf("Première valeur : %d\n",listevars.val[0]); printf("Deuxième nom : %s\n",listevars.ident[1]); printf("Deuxième valeur : %d\n",listevars.val[1]); - return 0; } diff --git a/Labgen/labgen.h b/Labgen/labgen.h index 12788aef412cdc66effbddbb4a0b06e95393f124..271ef447224d1fefe8e4e8c6d49cb1b0647e21ba 100644 --- a/Labgen/labgen.h +++ b/Labgen/labgen.h @@ -1,20 +1,26 @@ #include<stdio.h> #include<stdlib.h> #include<string.h> -#define TAILLE 255 +#define TAILLE 255 + typedef struct { int x; int y; } point; -typedef struct -{ + +typedef struct { char* ident[TAILLE]; int val[TAILLE]; int last; -}variables; +} variables; -char*** create_matrice(int,int); point new_point(int ,int); -char* lecture(point, char***); -void change_val_matrice(point,char*,char***); -void affichmat(char *** matrice); + +void create_matrice(int,int); +char* lecture(point); +void change_val_matrice(point,char*); + +void init_listevars(); +int find_var(char*); +void create_modif_var(char*,int); +void affichmat();