diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..3f19027cc3c8c4ae8005f632132486827d782a92 Binary files /dev/null and b/a.out differ diff --git a/affichage.h b/affichage.h new file mode 100644 index 0000000000000000000000000000000000000000..55d8de45a950324b0db51f1c90bf8b6cefbc3f52 --- /dev/null +++ b/affichage.h @@ -0,0 +1,6 @@ +#ifndef AFFICHAGE_H +#define AFFICHAGE_H + +void print_grid(char **tab); + +#endif \ No newline at end of file diff --git a/interpreteur.c b/interpreteur.c new file mode 100644 index 0000000000000000000000000000000000000000..2b3dc4ccd7621daa9f6b4e80f2c62092cd9efe79 --- /dev/null +++ b/interpreteur.c @@ -0,0 +1,56 @@ +#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 diff --git a/interpreteur.h b/interpreteur.h new file mode 100644 index 0000000000000000000000000000000000000000..57ee305a1b58cfd7943b57ea6287442772883779 --- /dev/null +++ b/interpreteur.h @@ -0,0 +1,19 @@ +#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 diff --git a/list.c b/list.c new file mode 100644 index 0000000000000000000000000000000000000000..cf6802c98cc7c2a551ab590a9321e3544332a80e --- /dev/null +++ b/list.c @@ -0,0 +1,70 @@ +#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 diff --git a/list.h b/list.h new file mode 100644 index 0000000000000000000000000000000000000000..5830bd4c16f8b845ecd4179ad8a613165d69768b --- /dev/null +++ b/list.h @@ -0,0 +1,40 @@ +#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 diff --git a/main.c b/main.c new file mode 100644 index 0000000000000000000000000000000000000000..3ed035e9a3b502644b7dbbadf110d7df898db628 --- /dev/null +++ b/main.c @@ -0,0 +1,61 @@ +#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; +} diff --git a/test.txt b/test.txt new file mode 100644 index 0000000000000000000000000000000000000000..64aa0929a6fd481957ce43b7798907a4113f5a1b --- /dev/null +++ b/test.txt @@ -0,0 +1,5 @@ +26 12 +a +bsf +czef qsf +d qsf qsf \ No newline at end of file