1
0
mirror of https://gitlab.com/klmp200/LO27.git synced 2024-11-29 16:04:14 +00:00

ajouté print et corrigé plein de fautes, reste une fuite mémoire : on peut créer deux fois la meme case

This commit is contained in:
Naej 2016-12-24 15:18:19 +01:00
parent 8e72e57179
commit db8322f504
4 changed files with 51 additions and 40 deletions

View File

@ -127,6 +127,9 @@ void recursivePrint(cellElement * tree){
} }
void FreeCellElement(cellElement* element) { void FreeCellElement(cellElement* element) {
printf("---FreeCellElement---\n");
if (element != NULL){ if (element != NULL){
free(element); free(element);
} }

View File

@ -30,9 +30,10 @@ Matrix CreateMatrix(){
return matrix; return matrix;
} }
void SetMatrixDim(Matrix matrix,int nbCols,int nbRows){ Matrix SetMatrixDim(Matrix matrix,int nbCols,int nbRows){
matrix.colCount = nbCols; matrix.colCount = nbCols;
matrix.rowCount = nbRows; matrix.rowCount = nbRows;
return matrix;
} }
bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){ bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){
@ -42,7 +43,7 @@ bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){
cellElement * elem = NULL; cellElement * elem = NULL;
cellElement * tmp = NULL; cellElement * tmp = NULL;
if (matrix.colCount < ColPos || matrix.rowCount < RowPos){ if (matrix.colCount <= ColPos || matrix.rowCount <= RowPos ){
return ERROR; return ERROR;
} }
elem = CreateCellElem(); elem = CreateCellElem();
@ -61,10 +62,10 @@ bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){
while (tmp->nextCol != NULL && tmp->nextCol->colIndex < ColPos){ while (tmp->nextCol != NULL && tmp->nextCol->colIndex < ColPos){
tmp=tmp->nextCol; tmp=tmp->nextCol;
} }
if (tmp->nextCol->colIndex > ColPos){ if (tmp->nextCol == NULL || tmp->nextCol->colIndex > ColPos){
elem->nextCol = tmp->nextCol; elem->nextCol = tmp->nextCol;
tmp->nextCol = elem; tmp->nextCol = elem;
}else if (tmp->colIndex == ColPos){ }else {
error ++; error ++;
} }
} }
@ -87,10 +88,10 @@ bool CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){
while (tmp->nextRow != NULL && tmp->nextRow->rowIndex < RowPos){ while (tmp->nextRow != NULL && tmp->nextRow->rowIndex < RowPos){
tmp=tmp->nextRow; tmp=tmp->nextRow;
} }
if (tmp->nextRow->rowIndex > RowPos){ if (tmp->nextRow == NULL || tmp->nextRow->rowIndex > RowPos){
elem->nextRow = tmp->nextRow; elem->nextRow = tmp->nextRow;
tmp->nextRow = elem; tmp->nextRow = elem;
}else if (tmp->rowIndex == RowPos){ }else {
error ++; error ++;
} }
} }
@ -120,7 +121,7 @@ cellElement * FindMatrixElem(Matrix matrix, int ColPos, int RowPos){
} }
elem = Row->data; elem = Row->data;
while (elem->colIndex != ColPos && elem != NULL){ while (elem != NULL && elem->colIndex != ColPos){
elem = elem->nextCol; elem = elem->nextCol;
} }
@ -198,7 +199,7 @@ int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){
} }
bool GetCellValue(Matrix matrix, int ColPos, int RowPos){ bool GetCellValue(Matrix matrix, int ColPos, int RowPos){
if (matrix.colCount < ColPos || matrix.rowCount < RowPos){ if (matrix.colCount <= ColPos || matrix.rowCount <= RowPos){
return ERROR; return ERROR;
} }
@ -218,8 +219,31 @@ bool SetCellValue(Matrix matrix, int ColPos, int RowPos,bool value){
return false; return false;
} }
} }
} }
/* todos:
*print matrice void BasicPrintMatrix(Matrix matrix){
*/ /* Non optimisé : debug fx */
int i = 0;
int j = 0;
printf("\n---PRINT MATRIX---\n");
for (i=0;i<matrix.colCount;i++){
printf("| ");
for (j=0;j<matrix.rowCount;j++){
if (GetCellValue(matrix,i,j) == true){
printf("1 ");
}else if (GetCellValue(matrix,i,j) == false){
printf("0 ");
}else{
printf("X "); /* error out of bounds, should never happend*/
}
}
printf("|\n");
}
printf("---END OF MATRIX---\n\n");
}

View File

@ -75,7 +75,9 @@ bool SetCellValue(Matrix matrix, int ColPos, int RowPos, bool value);
bool GetCellValue(Matrix matrix, int ColPos, int RowPos); bool GetCellValue(Matrix matrix, int ColPos, int RowPos);
void SetMatrixDim(Matrix matrix,int nbCols,int nbRows); Matrix SetMatrixDim(Matrix matrix,int nbCols,int nbRows);
void BasicPrintMatrix(Matrix matrix);
#endif #endif

36
main.c
View File

@ -12,38 +12,20 @@
int main(int argc, char **argv){ int main(int argc, char **argv){
Matrix matrix = CreateMatrix(); Matrix matrix = CreateMatrix();
int Rule = 170; int Rule = 256;
int N = 1; int N = 1;
applyRules(matrix,Rule,N); applyRules(matrix,Rule,N);
/* matrix = SetMatrixDim(matrix,1,2);
Matrix matrix;
cellElement * tree = NULL;
List * cols = NULL;
List * rows = NULL;
cols = CreateList(); BasicPrintMatrix(matrix);
rows = CreateList();
SetCellValue(matrix,0,0,true);
SetCellValue(matrix,0,0,true);
BasicPrintMatrix(matrix);
tree = CreateCellElem(); SetCellValue(matrix,0,0,false);
SetPositionIndex(tree,1,1); SetCellValue(matrix,0,0,false);
BasicPrintMatrix(matrix);
AddNextRow(tree);
SetPositionIndex(tree->nextRow,1,2);
AddNextCol(tree);
SetPositionIndex(tree->nextCol,1,3);
SetNextRow(tree->nextCol,tree->nextRow);
recursivePrint(tree);
removeNextRow(tree);
removeNextCol(tree);
recursivePrint(tree);
*/
return 0; return 0;
} }