From 4aeedd44c6ad74e66b265ed480d373582053bba2 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Tue, 27 Dec 2016 01:24:27 +0100 Subject: [PATCH] =?UTF-8?q?G=C3=A9n=C3=A9ration=20al=C3=A9atoire=20de=20ma?= =?UTF-8?q?trices=20et=20import=20depuis=20une=20matrice=20de=20bool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LibAutomaton/matrix.c | 65 +++++++++++++++++++++++++++++++++++++++++++ LibAutomaton/matrix.h | 44 +++++++++++++++++++++++++++++ main.c | 9 ++++++ 3 files changed, 118 insertions(+) diff --git a/LibAutomaton/matrix.c b/LibAutomaton/matrix.c index 40893f1..30ca076 100644 --- a/LibAutomaton/matrix.c +++ b/LibAutomaton/matrix.c @@ -1,6 +1,7 @@ #include #include #include +#include Matrix applyRules (Matrix matrix,int Rules, int N){ int power = 2; @@ -382,6 +383,70 @@ Matrix orRowSequenceOnMatrix(Matrix m){ return rowSequenceOnMatrix(m, OR); } +Matrix newMatrix(BooleanMatrix bmatrix){ + Matrix m = CreateMatrix(); + int i; + int j; + + m.colCount = bmatrix.cols; + m.rowCount = bmatrix.rows; + + for (i=0; i < m.rowCount ; i++){ + for (j=0; j < m.colCount; j++){ + if (bmatrix.data[i][j]){ + SetCellValue(m, j, i, true); + } + } + } + + return m; +} + +BooleanMatrix CreateBooleanMatrix(int cols, int rows){ + BooleanMatrix matrix; + int i; + + matrix.rows = rows; + matrix.cols = cols; + + matrix.data = (bool**)malloc(matrix.rows * sizeof(bool*)); + if (matrix.data != NULL){ + for (i=0; i < matrix.rows; i++){ + matrix.data[i] = (bool*)malloc(matrix.cols * sizeof(bool)); + } + } + return matrix; +} + +BooleanMatrix RandomizeBooleanMatrix(BooleanMatrix matrix){ + int i; + int j; + int r; + + srand(time(NULL)); + + for (i=0; i < matrix.rows; i++){ + for (j=0; j < matrix.cols; j++){ + r = rand() % 2; + if (r == 1){ + matrix.data[i][j] = true; + } else { + matrix.data[i][j] = false; + } + } + } + + return matrix; +} + +void FreeBooleanMatrix(BooleanMatrix matrix){ + int i; + for (i=0; i < matrix.rows; i++){ + free(matrix.data[i]); + } + free(matrix.data); +} + /* todos : *finir le freeMatrix *chasser les bugs diff --git a/LibAutomaton/matrix.h b/LibAutomaton/matrix.h index 289fa70..8216a95 100644 --- a/LibAutomaton/matrix.h +++ b/LibAutomaton/matrix.h @@ -25,6 +25,50 @@ typedef struct Matrix { }Matrix; +/** +* Abstract type for a matrix of booleans +* @param rows number of rows of the matrix +* @param cols numbers of columns of the matrix +* @param data the matrix +* @see CreateBooleanMatrix to create one +*/ +typedef struct { + int rows; + int cols; + + bool **data; + +} BooleanMatrix; + +/** +* Function creating an empty boolean matrix +* @param cols number of cols +* @param rows number of rows +* @return booleanmatrix a new matrix +*/ +BooleanMatrix CreateBooleanMatrix(int cols, int rows); + +/** +* Randomize a BooleanMatrix +* @param matrix a BooleanMatrix +* @return booleanmatrix the processed matrix +*/ +BooleanMatrix RandomizeBooleanMatrix(BooleanMatrix matrix); + +/** +* Free a BooleanMatrix +* @param matrix a BooleanMatrix +* @return +*/ +void FreeBooleanMatrix(BooleanMatrix matrix); + +/** +* Create a Matrix from its array-based representation +* @param bmatrix BooleanMatrix +* @return m Matrix +*/ +Matrix newMatrix(BooleanMatrix bmatrix); + /*---applyRules--- *A function tha allows you to apply some rules n times on the matrix and returns it * diff --git a/main.c b/main.c index 32c6445..12d7ac3 100644 --- a/main.c +++ b/main.c @@ -13,6 +13,15 @@ int main(int argc, char **argv){ Matrix matrix = CreateMatrix(); Matrix m2; + Matrix m3; + BooleanMatrix bmatrix = CreateBooleanMatrix(5, 5); + + bmatrix = RandomizeBooleanMatrix(bmatrix); + m3 = newMatrix(bmatrix); + FreeBooleanMatrix(bmatrix); + BasicPrintMatrix(m3); + freeMatrix(m3); + int Rule = 256; int N = 1; applyRules(matrix,Rule,N);