Skip to content
Extraits de code Groupes Projets
Valider ed3c2fc0 rédigé par Anteunis Charles's avatar Anteunis Charles
Parcourir les fichiers

add files

parent e0b014ee
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Fichier ajouté
#ifndef AFFICHAGE_H
#define AFFICHAGE_H
void print_grid(char **tab);
#endif
\ No newline at end of file
#include "interpreteur.h"
void interpreteur(char **matrix, int height, int width) {
curseur cur;
int i, j;
list pile = empty();
char tmp;
cur.x = 0;
cur.y = 0;
cur.current_char = matrix[0][0];
while (cur.current_char != '@') {
/* Action en fonction du caractère lu */
switch (cur.current_char)
{
case 48 ... 57:
/* si on lit un entier */
push(cur.current_char - 48, &pile);
break;
case '>':
cur.current_dir = W;
break;
case ',':
if (is_empty(pile))
tmp = '0';
else
tmp = pop(&pile) + 48;
break;
default:
break;
}
/* Déplacement du curseur et lecture du prochain caractère */
switch (cur.current_dir)
{
case N:
cur.x = cur.x - 1;
/* Gérer les cas si on est au bord de la matrice */
cur.current_char = matrix[cur.y][cur.x];
break;
case NW:
cur.x = cur.x - 1;
cur.y = cur.y + 1;
break;
default:
break;
}
}
}
\ No newline at end of file
#ifndef INTERPRETEUR_H
#define INTERPRETEUR_H
#include "list.h"
typedef enum direction {
N, S, E, W, NE, NW, SE, SW
} direction;
typedef struct curseur {
int x;
int y;
char current_char;
direction current_dir;
} curseur;
void interpreteur(char **matrix, int height, int width);
#endif
\ No newline at end of file
#include "list.h"
/**
* Return the empty list
*/
list empty() {
return NULL;
}
/**
* Return true if l is empty, else false
* \param l the list to test
*/
int is_empty(list l) {
return l==NULL;
}
/**
* Add an element to the head of a list
* \param e the element to add
* \param l a pointer to the list
* \attention modify the list, l must be allocated
*/
void push(int e, list *l) {
list tmp = (elem *)malloc(sizeof(elem));
tmp->val = e;
tmp->next = *l;
*l = tmp;
}
/**
* Return the head of the list and pop it from the list
* \param l a pointer to the list
* \attention modify the list, return 0 if l is empty
*/
int pop(list *l) {
if (l==NULL)
return 0;
int res = (*l)->val;
list tmp = *l;
*l = (*l)->next;
free(tmp);
return res;
}
/**
* Return the length of a list
* \param l the list we want the length
* \attention recursive function
*/
int len(list l) {
if (is_empty(l))
return 0;
else
return 1 + len(l->next);
}
/**
* Print a list like this : head->...->...->NULL
* \param l the list we want to print
*/
void print_list(list l) {
while (l != NULL) {
printf("%i->",l->val);
l = l->next;
}
printf("NULL\n");
}
\ No newline at end of file
#ifndef LIST_H
#define LIST_H
#include<stdio.h>
#include<stdlib.h>
/**
* \file list.h
* \author Charles Anteunis
*
* Definiton of the int list structure in C
*/
/**
* Definition of list type
*
*/
typedef struct elem * list;
/**
* Definition of a list element
*/
typedef struct elem {
int val;
list next;
} elem;
list empty();
int is_empty(list l);
void push(int e, list *l);
int pop(list *l);
int len(list l);
void print_list(list l);
#endif
\ No newline at end of file
#include "list.h"
#include "affichage.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
FILE *f;
char c = '\0';
int height = 0, width = 0;
int i, j;
char **matrix;
if (argc == 1) {
printf("Usage: ./prog2d file\n");
return 0;
}
else {
/* ouverture du fichier à lire */
if ((f = fopen(argv[1], "r")) == NULL) {
printf("Error while reading file\n");
exit(1);
}
}
/* on récupère le nombre de colonnes du fichier */
fread(&c, sizeof(char), 1, f);
while ( c != ' ') {
width = 10*width + strtol(&c, NULL, 10);
fread(&c, sizeof(char), 1, f);
}
width = width/10;
/* on récupère le nombre de lignes du fichier */
fread(&c, sizeof(char), 1, f);
while (c != '\n') {
height = 10*height + strtol(&c, NULL, 10);
fread(&c, sizeof(char), 1, f);
}
height = height/10;
matrix = (char **) malloc(height*sizeof(char *));
for (i=0 ; i<height ; i++) {
matrix[i] = (char *) malloc(width*sizeof(char));
}
i = 0;
j = 0;
/* On remplit la matrice avec les données du fichier */
while (fread(&c, sizeof(char), 1, f)) {
if (c == '\n') {
i++;
j = 0;
continue;
}
matrix[i][j] = c;
j++;
}
return 0;
}
26 12
a
bsf
czef qsf
d qsf qsf
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter