Skip to content
Extraits de code Groupes Projets
Valider 1b85ed51 rédigé par Hélène TRAN's avatar Hélène TRAN
Parcourir les fichiers

Correction labgen.c ok

parent 23616dc2
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Aucun aperçu pour ce type de fichier
#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;
}
#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();
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter