1
0
mirror of https://gitlab.com/klmp200/LO27.git synced 2024-11-22 16:43:20 +00:00

Génération aléatoire de matrices et import depuis une matrice de bool

This commit is contained in:
Antoine Bartuccio 2016-12-27 01:24:27 +01:00
parent f327f1024b
commit 4aeedd44c6
3 changed files with 118 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <matrix.h>
#include <time.h>
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

View File

@ -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
*

9
main.c
View File

@ -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);