mirror of
https://gitlab.com/klmp200/LO27.git
synced 2024-11-05 10:18:03 +00:00
193 lines
3.7 KiB
C
193 lines
3.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <matrix.h>
|
|
|
|
Matrix applyRules (Matrix matrix,int Rules, int N){
|
|
int power = 2;
|
|
int i = 0;
|
|
if (Rules <= 0){
|
|
return matrix;
|
|
} else {
|
|
while (Rules%power == 0 ){
|
|
power*=2;
|
|
}
|
|
for (i=0;i<N;i++){
|
|
printf("Apply rule %d \n",Rules%power);
|
|
/*Replace it by the implementation of the rules*/
|
|
}
|
|
|
|
applyRules(matrix,Rules - Rules%power,N);
|
|
}
|
|
return matrix;
|
|
}
|
|
|
|
Matrix CreateMatrix(){
|
|
Matrix matrix;
|
|
matrix.colCount = 0;
|
|
matrix.rowCount = 0;
|
|
matrix.cols = CreateList();
|
|
matrix.rows = CreateList();
|
|
return matrix;
|
|
}
|
|
|
|
void CreateMatrixElem(Matrix matrix, int ColPos, int RowPos){
|
|
ListElement * Row = NULL;
|
|
ListElement * Col = NULL;
|
|
int error = 0;
|
|
cellElement * elem = NULL;
|
|
cellElement * tmp = NULL;
|
|
|
|
elem = CreateCellElem();
|
|
SetPositionIndex(elem,ColPos,RowPos);
|
|
|
|
Row = GetElementPos(matrix.rows,RowPos);
|
|
if (Row != NULL && Row->data != NULL){
|
|
|
|
if (Row->data->colIndex == ColPos){
|
|
error ++;
|
|
} else if (Row->data->colIndex > ColPos){
|
|
elem->nextCol = Row->data;
|
|
Row->data = elem;
|
|
} else {
|
|
tmp = Row->data;
|
|
while (tmp->nextCol != NULL && tmp->nextCol->colIndex < ColPos){
|
|
tmp=tmp->nextCol;
|
|
}
|
|
if (tmp->nextCol->colIndex > ColPos){
|
|
elem->nextCol = tmp->nextCol;
|
|
tmp->nextCol = elem;
|
|
}else if (tmp->colIndex == ColPos){
|
|
error ++;
|
|
}
|
|
}
|
|
|
|
}else {
|
|
push(matrix.rows,elem);
|
|
matrix.rows->tail->pos = RowPos;
|
|
}
|
|
|
|
Col = GetElementPos(matrix.cols,ColPos);
|
|
if (Col != NULL && Col->data != NULL){
|
|
|
|
if (Col->data->rowIndex == RowPos){
|
|
error ++;
|
|
} else if (Col->data->rowIndex > RowPos){
|
|
elem->nextRow = Col->data;
|
|
Col->data = elem;
|
|
} else {
|
|
tmp = Col->data;
|
|
while (tmp->nextRow != NULL && tmp->nextRow->rowIndex < RowPos){
|
|
tmp=tmp->nextRow;
|
|
}
|
|
if (tmp->nextRow->rowIndex > RowPos){
|
|
elem->nextRow = tmp->nextRow;
|
|
tmp->nextRow = elem;
|
|
}else if (tmp->rowIndex == RowPos){
|
|
error ++;
|
|
}
|
|
}
|
|
|
|
}else {
|
|
push(matrix.cols,elem);
|
|
matrix.cols->tail->pos = ColPos;
|
|
}
|
|
|
|
|
|
if (error != 0){
|
|
free(elem);
|
|
}
|
|
}
|
|
|
|
cellElement * FindMatrixElem(Matrix matrix, int ColPos, int RowPos){
|
|
ListElement * Row = NULL;
|
|
cellElement * elem = NULL;
|
|
|
|
Row = GetElementPos(matrix.rows,RowPos);
|
|
if (Row == NULL){
|
|
return NULL;
|
|
}
|
|
elem = Row->data;
|
|
|
|
while (elem->colIndex != ColPos && elem != NULL){
|
|
elem = elem->nextCol;
|
|
}
|
|
|
|
return elem;
|
|
}
|
|
|
|
int SupprMatrixElem(Matrix matrix, int ColPos, int RowPos){
|
|
cellElement * elem = NULL;
|
|
cellElement * tmp = NULL;
|
|
|
|
ListElement * Row = NULL;
|
|
ListElement * Col = NULL;
|
|
|
|
elem = FindMatrixElem(matrix,ColPos,RowPos);
|
|
if (elem == NULL){
|
|
return 0;
|
|
}
|
|
|
|
Row = GetElementPos(matrix.rows,RowPos);
|
|
if (Row == NULL || Row->data == NULL){
|
|
return -1;
|
|
}
|
|
|
|
if (Row->data->colIndex == ColPos){
|
|
Row->data = elem->nextCol;
|
|
} else {
|
|
tmp = Row->data;
|
|
while (tmp->nextCol != NULL && tmp->nextCol != elem){
|
|
tmp = tmp->nextCol;
|
|
}
|
|
if (tmp->nextCol == NULL){
|
|
return -3; /* WTF ?? */
|
|
} else {
|
|
tmp->nextCol = elem->nextCol;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
Col = GetElementPos(matrix.cols,ColPos);
|
|
if (Col == NULL|| Col->data == NULL){
|
|
return -2;
|
|
}
|
|
if (Col->data->rowIndex == RowPos){
|
|
Col->data = elem->nextRow;
|
|
} else {
|
|
tmp = Col->data;
|
|
while (tmp->nextRow != NULL && tmp->nextRow != elem){
|
|
tmp = tmp->nextRow;
|
|
}
|
|
if (tmp->nextRow == NULL){
|
|
return -4; /* WTF ?? */
|
|
} else {
|
|
tmp->nextRow = elem->nextRow;
|
|
}
|
|
|
|
}
|
|
|
|
FreeCellElement(elem);
|
|
return 1;
|
|
|
|
}
|
|
|
|
bool GetCellValue(Matrix matrix, int ColPos, int RowPos){
|
|
if (FindMatrixElem(matrix,ColPos,RowPos) == NULL){
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void SetCellValue(Matrix matrix, int ColPos, int RowPos,bool value){
|
|
if (value == true){
|
|
CreateMatrixElem(matrix,ColPos,RowPos);
|
|
}else{
|
|
SupprMatrixElem(matrix,ColPos,RowPos);
|
|
}
|
|
|
|
}
|
|
/* todos:
|
|
*gerer bornes matrice
|
|
*print matrice
|
|
*/ |