1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| // JDV3.cpp*: définit le point d'entrée pour l'application console.
//
#include "stdafx.h"
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
#define MATRICE0 7
#define MATRICE1 9
/****************************************/
/******* Fonctions ********/
/****************************************/
//---------------------Fonction iniatliser Matrice--------------//
void intialisation(int matrice [][MATRICE1 ])
{
int i,j ;
for(i=0;i<MATRICE1;i++)
{
for(j=0; j<MATRICE1; j++)
{
if(i<=j &&i>0 &&j<=7)
matrice[i][j]=1;
else matrice[i][j]=0;
}
}
}
//-----------------------Fonction calcul de nombre de voisin-------------------------//
int nombre_voisins (int matrice[][MATRICE1 ], int ligne, int colonne)
{
int compteur=0; //compteur de cellule
int i =0;
int j=0;
for (i=ligne-1;i<=ligne+1;i++) // parcourir ligne
for(j=colonne-1;j<=colonne+1;j++) //parcourir colones
compteur-=matrice[ligne][colonne]; // retirer cellule du milieu
return compteur;
}
//--------------------------Fonction de Mise a jour //-----------------------------------//
void mise_a_jour(int matrice[ ][MATRICE1 ])
{
int i,j;
int nbr_voisins;
int matrice_densite[MATRICE0][MATRICE0]; // matrice qui compte le nombre de voisins case par case
for(i=0; i< MATRICE0; i++)
for(j=0; j< MATRICE0; j++)
matrice_densite[i][j]=nombre_voisins(matrice,i+1,j+1); // i+1 et j+1 car on passe de la SOUS_MATRICE(matrice0) a la MATRICE(matrice1)
for(i=0; i< MATRICE0; i++)
for(j=0; j< MATRICE0; j++)
{
nbr_voisins=matrice_densite[i][j];
if(nbr_voisins==2)
matrice[i+1][j+1]=1;
else if (nbr_voisins==0 || nbr_voisins==4)
matrice[i+1][j+1]=0;
}
}
//--------------- Fonction trace Ligne---------------------------------------------------//
void ligne(int largeur)
{
int i;
for(i=0; i<largeur; i++)
printf("+-");
printf("+\n");
}
//-------------------------------Fonction afficahge-----------------------------------------//
void affiche_matrice(int matrice[ ][MATRICE1 ])
{
int i,j;
for(i=1; i<=MATRICE0; i++)
{
ligne(7);
for(j=1; j<= MATRICE0; j++)
if (matrice[i][j]==1)
printf("|%c",'*');
else
printf("|%c",'|');
printf("|\n");
}
ligne(MATRICE0);
}
/****************************************/
/******* Main Principale ********/
/****************************************/
int _tmain(int argc, _TCHAR* argv[])
{
int i;
int nbr_cycles;
int matrice[MATRICE1] [MATRICE1 ];
char s[2];
printf("Nombre cycles : ");
scanf_s("%i",&nbr_cycles);
intialisation(matrice);
printf("Céllule au depart : \n");
affiche_matrice(matrice);
printf("Appuyer sur ENTER pour continuer...\n");
gets(s);
for(i=0; i<nbr_cycles; i++)
{
mise_a_jour (matrice);
printf("Céllule apres %d cycles: \n", i+1);
affiche_matrice (matrice);
printf("Pressez sur ENTER pour continuer...\n");
gets(s);
}
//system("pause");
return 0;
} |
Partager