#include #include #include #include #include #include "Tabla.h" struct Tabla *tab; struct Tabla tabel; int prelefectuate; int arecinciunu(struct Tabla *tb,int p, int q){ int x,y,nr=0; if (((p==0)&&(q==0))||((p==0)&&(q==8))||((p==8)&&(q==0))||((p==8)&&(q==8))) { return(1); } else{ if ((p-1>=0)&&(p+1<=8)&&(q-1>=0)&&(q+1<=8)){//suntem in interiorul matricei for(x=p-1;x<=p+1;x++) for(y=q-1;y<=q+1;y++){ if ((x==p)&&(y!=q)||(x!=p)&&(y==q)||(x!=p)&&(y!=q)){ if (tab->t[x][y]==1) nr++; } } } else { if (p==0) { //suntem pe marginea de su if ((tab->t[p][q-1]==1)&&(tab->t[p+1][q-1]==1)&&(tab->t[p+1][q]==1)&&(tab->t[p+1][q+1]==1)&&(tab->t[p][q+1]==1)){ return (0); } } if (q==0) { //suntem pe marginea din stanga if ((tab->t[p-1][q]==1)&&(tab->t[p-1][q+1]==1)&&(tab->t[p][q+1]==1)&&(tab->t[p+1][q+1]==1)&&(tab->t[p+1][q]==1)){ return (0); } } if (p==8) { //suntem pe marginea de jos if ((tab->t[p-1][q]==1)&&(tab->t[p-1][q+1]==1)&&(tab->t[p][q+1]==1)&&(tab->t[p][q-1]==1)&&(tab->t[p-1][q-1]==1)){ return (0); } } if (q==8) { //suntem pe marginea din dreapta if ((tab->t[p][q-1]==1)&&(tab->t[p+1][q-1]==1)&&(tab->t[p+1][q]==1)&&(tab->t[p-1][q]==1)&&(tab->t[p-1][q-1]==1)){ return (0); } } }//else }//else if (nr>=5){ return (0); } else return(1); } int arecincizero(struct Tabla *tb,int p,int q){ int x,y,nr=0; if (((p==0)&&(q==0))||((p==0)&&(q==8))||((p==8)&&(q==0))||((p==8)&&(q==8))) { return(1); } else{ if ((p-1>=0)&&(p+1<=8)&&(q-1>=0)&&(q+1<=8)){//suntem in interiorul matricei for(x=p-1;x<=p+1;x++) for(y=q-1;y<=q+1;y++){ if ((x==p)&&(y!=q)||(x!=p)&&(y==q)||(x!=p)&&(y!=q)){ printf("%d ",tab->t[x][y]); if (tab->t[x][y]==0) nr++; } //printf("\n"); } } else { if (p==0) { //suntem pe marginea de su if ((tab->t[p][q-1]==0)&&(tab->t[p+1][q-1]==0)&&(tab->t[p+1][q]==0)&&(tab->t[p+1][q+1]==0)&&(tab->t[p][q+1]==0)){ return(0); } } if (q==0) { //suntem pe marginea din stanga if ((tab->t[p-1][q]==0)&&(tab->t[p-1][q+1]==0)&&(tab->t[p][q+1]==0)&&(tab->t[p+1][q+1]==0)&&(tab->t[p+1][q]==0)){ return (0); } } if (p==8) { //suntem pe marginea de jos if ((tab->t[p-1][q]==0)&&(tab->t[p-1][q+1]==0)&&(tab->t[p][q+1]==0)&&(tab->t[p][q-1]==0)&&(tab->t[p-1][q-1]==0)){ return (0); } } if (q==8) { //suntem pe marginea din dreapta if ((tab->t[p][q-1]==0)&&(tab->t[p+1][q-1]==0)&&(tab->t[p+1][q]==0)&&(tab->t[p-1][q]==0)&&(tab->t[p-1][q-1]==0)){ return (0); } } }//else }//else if (nr>=5) { return (0); } else return(1); } void prelucreaza(struct Tabla *tb,int nrc){ int p,q; for (p=0;p<9;p++){ printf("\nM[%d][%d]=%d ",p,nrc,tb->t[p][nrc]); //printf("apel 5 1\n"); if (arecinciunu(tb,p,nrc)==0){ printf("Are 5 de 1 \n"); if(tb->t[p][nrc]!=0){ tb->t[p][nrc]=0; prelefectuate=1; } } else{ //printf("apel 5 0\n"); if (arecincizero(tb,p,nrc)==0){ printf("ARE 5 DE ZERO \n"); if(tb->t[p][nrc]!=1){ tb->t[p][nrc]=1; prelefectuate=1; } } else { //tb->nrprel=0; // printf("\n"); } } } } int main(){ key_t key = 531982; key_t keysem=581948; int id,ids ; int x,y=0;//nr schimbarilor efectuate int nrc=0;//prel se face pe coloane, "nrc" retine nr coloanei la o prelucrare id=shmget(key,0,0600); if (id<0){ printf("Eroare la creare segment\n"); exit(1); } tab =(struct Tabla*)shmat(id,0,0); if (tab==NULL){ printf("Eroare la atasharea segmentului de memorie\n"); exit(2); } ids=semget(keysem,0,0);//atasare semafor unlock(ids); while (1){ sleep(1); lock(ids); prelefectuate=0; prelucreaza(tab,nrc); if (nrc<8) nrc++; else { printf("Gata o tura"); if ((prelefectuate==0)&&(nrc==0)) { //printf("Nu s-a mai efectuat nici o prelucrare ,ies"); //tab->nrprel=0; //exit(1); } nrc=0; } unlock(ids); } }