Untitled
user_9243973
plain_text
3 years ago
14 kB
7
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <dos.h>
#include <dir.h>
// déclaration de la Pile
typedef int Telm;
typedef struct EPile *Pile;
typedef struct EPile
{
Telm Val;
Pile Suiv
} CelluleP;
typedef struct Liste *Pliste;
typedef struct Liste
{
int x;
int y;
int nature;
Pliste Suiv;
} Cellule;
int n, m, k1, k2;
int T[100][100];
Pliste Tete;
// déclaration de la File
typedef struct Element *EFile;
typedef struct Element
{
Pliste T;
EFile Suiv
} CelluleF;
typedef struct
{
EFile Tete, Queue;
} File;
// primitives des Piles
// procedure Initpile
void Initpile(Pile *P)
{
(*P) = NULL;
}
// procedure Empiler
void Empiler(Pile *P, Telm x)
{
Pile V;
V = malloc(sizeof(CelluleP));
V->Val = x;
V->Suiv = *P;
*P = V;
}
// procedure Depiler
void Depiler(Pile *P, Telm *x)
{
Pile V;
*x = (*P)->Val;
V = *P;
*P = (*P)->Suiv;
free(V);
}
// Fonction Pilevide
int Pilevide(Pile P)
{
if (P == NULL)
return 1;
else
return 0;
}
// Fonction SommetPile
Telm SommetPile(Pile P)
{
return P->Val;
}
////primitives des Files
// procedure Initfile
void Initfile(File *F)
{
(*F).Tete = NULL;
(*F).Queue = NULL;
}
// procedure Enfiler
void Enfiler(File *F, Pliste x)
{
EFile V;
V = malloc(sizeof(CelluleF));
V->T = x;
V->Suiv = NULL;
if ((*F).Tete == NULL)
(*F).Tete = V; // la file est vide
else
(*F).Queue->Suiv = V;
(*F).Queue = V;
}
// procedure Defiler
void Defiler(File *F, Pliste *x)
{
EFile V;
V = (*F).Tete;
*x = V->T; // ou *x=(*F).Tete->Val;
if ((*F).Tete == (*F).Queue)
{
(*F).Tete = NULL;
(*F).Queue = NULL;
}
else
(*F).Tete = (*F).Tete->Suiv; // ou (*F).Tete=V->Suiv;
free(V);
}
// Fonction Filevide
int Filevide(File F)
{
if (F.Tete == NULL)
return 1;
else
return 0;
}
// Fonction Tetefile
Pliste Tetefile(File F)
{
return F.Tete->T;
}
// Creation
void creercarte(int T[][100], int n, int m)
{
int i, j, r;
srand(time(NULL));
for (i = 0; i < n; i = i + 2) // on avance de deux pas pour travailler que par paire
{
for (j = 0; j < m; j = j + 2)
{
r = (rand() % 4) + 1; // rand%4 veut dire qu'on remplie avec des 0 1 2 3 on rajoute
T[i][j] = r; //+1 pour remplir avec 1 2 3 4 seulement
T[i][j + 1] = r;
T[i + 1][j] = r; // on remplie aleatoirement 4 pixels voisin en meme temps et vu que cest
T[i + 1][j + 1] = r; // aleatoire on a au minimum des objets de 4 sinon plus que 4
}
}
}
// Affichage
void affichecarte(int T[][100])
{
int x, y;
printf("\n\n La carte : \n");
printf(" ");
printf("%c", 201);
for (x = 0; x < n; x++) // pour delimiter la carte : la premiere ligne de_
{
printf("%c", 205);
}
printf("%c", 187);
for (x = 0; x < n; x++)
{
printf("\n");
printf(" ");
printf("%c", 186);
for (y = 0; y < m; y++)
{
if (T[x][y] == 1)
{
SetColor(1);
}
if (T[x][y] == 2)
{
SetColor(4);
} // pour que chaque nature ait sa propre couleur
if (T[x][y] == 3)
{
SetColor(6);
} // on appelle la fonction des couleurs
if (T[x][y] == 4)
{
SetColor(2);
}
printf("%d", T[x][y]);
}
SetColor(15); //(le 15 la couleur du blanc) pour quil n'y ait que les chiffres en couleurs et non les |
printf("%c", 186);
}
printf("\n");
printf(" ");
printf("%c", 200);
for (x = 0; x < n; x++) // la derniere ligne de _
{
printf("%c", 205);
}
printf("%c", 188);
}
// la fonction pour les couleurs
void SetColor(int ForgC)
{
WORD wColor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
// We use csbi for the wAttributes word.
if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
// Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
void afficheliste(Pliste Tete)
{
Pliste P;
P = Tete;
while (P != NULL)
{
printf("(%d %d)", P->x, P->y);
P = P->Suiv;
}
}
void reintialiser(int T[][100], int n, int m)
{
for (int x = 0; x < n; x++)
for (int y = 0; y < m; y++)
{
if (T[x][y] == 5)
{
T[x][y] = 1;
}
if (T[x][y] == 6)
{
T[x][y] = 2;
}
if (T[x][y] == 7)
{
T[x][y] = 3;
}
if (T[x][y] == 8)
{
T[x][y] = 4;
}
}
}
void triliste(Pliste *Tete)
{
Pliste Q, P;
int A, B;
P = *Tete;
while (P->Suiv != NULL)
{
Q = P->Suiv;
while (Q != NULL)
{
if (P->x >= Q->x)
{
A = P->x;
B = P->y;
P->x = Q->x;
P->y = Q->y;
Q->x = A;
Q->y = B;
};
if (P->x == Q->x && P->y >= Q->y)
{
A = P->x;
B = P->y;
P->x = Q->x;
P->y = Q->y;
Q->x = A;
Q->y = B;
};
Q = Q->Suiv;
}
P = P->Suiv;
}
}
Pliste ExtraireObjet(int T[][100], int x, int y, int n, int m, int nature)
{
Pliste P, Q;
int A, B;
;
if (T[x][y] == nature)
{
if (nature == 1)
{
T[x][y] = 5;
}
if (nature == 2)
{
T[x][y] = 6;
}
if (nature == 3)
{
T[x][y] = 7;
}
if (nature == 4)
{
T[x][y] = 8;
}
k1 = k1 + 1;
if (y > 0)
{
ExtraireObjet(T, x, y - 1, n, m, nature);
} // case gauche
if (x > 0)
{
ExtraireObjet(T, x - 1, y, n, m, nature);
} // case haut
if (x < n - 1)
{
ExtraireObjet(T, x + 1, y, n, m, nature);
} // bas
if (y < m - 1)
{
ExtraireObjet(T, x, y + 1, n, m, nature);
} // droite
P = malloc(sizeof(Cellule));
P->x = x;
P->y = y;
P->nature = nature;
P->Suiv = Tete;
Tete = P;
k2 = k2 + 1;
k1 = k1 - 1;
}
if (k1 == 0 && k2 > 0)
{
triliste(&Tete);
}
return (Tete);
}
void AfficheObjet(int T[][100], int a, int b, int n, int m, int nature)
{
Pliste P;
P = ExtraireObjet(T, a, b, n, m, nature);
int x, y;
printf(" ");
printf("%c", 201);
for (x = 0; x < n; x++) // pour delimiter la carte : la premiere ligne de_
{
printf("%c", 205);
}
printf("%c", 187);
for (x = 0; x < n; x++)
{
printf("\n");
printf(" ");
printf("%c", 186);
for (int y = 0; y < m; y++)
{
if (P != NULL)
{
if (P->nature == 1)
{
SetColor(1);
}
if (P->nature == 2)
{
SetColor(4);
} // pour que chaque nature ait sa propre couleur
if (P->nature == 3)
{
SetColor(6);
} // on appelle la fonction des couleurs
if (P->nature == 4)
{
SetColor(2);
}
if (x == P->x && y == P->y)
{
printf("%c", 219);
P = P->Suiv;
}
else
{
SetColor(15);
printf(" ");
}
}
else
{
SetColor(15);
printf(" ");
}
}
SetColor(15); //(le 15 la couleur du blanc) pour quil n'y ait que les chiffres en couleurs et non les |
printf("%c", 186);
}
printf("\n");
printf(" ");
printf("%c", 200);
for (x = 0; x < n; x++) // la derniere ligne de _
{
printf("%c", 205);
}
printf("%c", 188);
}
void ExtraireTheme(int T[][100], int n, int m, int nature, File *F)
{
int x, y;
Initfile(&(*F));
Pliste P;
for (x = 0; x < n; x++)
for (y = 0; y < m; y++)
{
if (T[x][y] == nature)
{
P = ExtraireObjet(T, x, y, n, m, nature);
Enfiler(&(*F), P);
}
}
}
void AfficheTheme(File F)
{
int Th[n][m];
for (int x = 0; x < n; x++)
for (int y = 0; y < m; y++)
Th[x][y] = 0;
while (!Filevide(F))
{
Pliste Tth;
Defiler(&F, &Tth);
while (Tth != NULL)
{
Th[Tth->x][Tth->y] = Tth->nature;
Tth = Tth->Suiv;
}
}
int x, y;
printf("\n ");
printf("%c", 201);
for (x = 0; x < n; x++) // pour delimiter la carte : la premiere ligne de_
{
printf("%c", 205);
}
printf("%c", 187);
for (x = 0; x < n; x++)
{
printf("\n");
printf(" ");
printf("%c", 186);
for (y = 0; y < m; y++)
{
if (Th[x][y] == 0)
printf(" ");
else
{
if (Th[x][y] == 1)
{
SetColor(1);
}
if (Th[x][y] == 2)
{
SetColor(4);
} // pour que chaque nature ait sa propre couleur
if (Th[x][y] == 3)
{
SetColor(6);
} // on appelle la fonction des couleurs
if (Th[x][y] == 4)
{
SetColor(2);
}
printf("%c", 219);
}
}
SetColor(15); //(le 15 la couleur du blanc) pour quil n'y ait que les chiffres en couleurs et non les |
printf("%c", 186);
}
printf("\n");
printf(" ");
printf("%c", 200);
for (x = 0; x < n; x++) // la derniere ligne de _
{
printf("%c", 205);
}
printf("%c", 188);
}
void Stat(int T[][100], int n, int m)
{
int x, y, surface1, surface2, surface3, surface4;
surface1 = 0;
surface2 = 0;
surface3 = 0;
surface4 = 0;
for (x = 0; x < n; x++)
{
for (y = 0; y < m; y++)
{
if (T[x][y] == 1)
{
surface1 = surface1 + 1;
}
if (T[x][y] == 2)
{
surface2 = surface2 + 1;
}
if (T[x][y] == 3)
{
surface3 = surface3 + 1;
}
if (T[x][y] == 4)
{
surface4 = surface4 + 1;
}
}
}
printf("\n Sur la carte il y a :");
printf("\n -");
printf("%d", surface1);
printf(" zones agricoles.");
printf("\n -");
printf("%d", surface2);
printf(" zones d'habitations.");
printf("\n -");
printf("%d", surface3);
printf(" forets.");
printf("\n -");
printf("%d", surface4);
printf(" zones industrielles.");
}
int main()
{
int x, y, nature;
Pliste P;
k1 = 0;
k2 = 0;
printf("\n Veuillez entrer les dimensions de votre carte \n");
printf(" La longueur: ");
scanf("%d", &n);
printf(" La largeur: ");
scanf("%d", &m);
creercarte(T, n, m);
for (int e = 0; e < 100; e++)
{
affichecarte(T);
printf("\n Que voulez-vous faire? : \n");
printf("\n 1.Extraire un objet. ");
printf("\n 2.Extraire un theme. ");
printf("\n 3.Afficher la statistique d'un type de parcelle. ");
printf("\n Autre pour quitter le programme. \n");
int c;
printf("\n Entrez un chiffre : ");
scanf("%d", &c);
if (c == 1)
{
printf("\n Veuillez entrer les coordonnees : \n");
printf(" x :");
scanf("%d", &x);
printf(" y :");
scanf("%d", &y);
printf("\n Veuillez entrer la nature de la parcelle : ");
scanf("%d", &nature);
AfficheObjet(T, x, y, n, m, nature);
reintialiser(T, n, m);
printf("\n Appuyez sur n'importe quelle touche pour continuer.");
char cc;
scanf("%s", &cc);
printf("\n\n\n\n\n\n\n\n\n");
}
if (c == 2)
{
printf("\n Veuillez entrer la nature du theme que vous voulez afficher : ");
int t;
scanf("%d", &t);
File F;
Tete = NULL;
ExtraireTheme(T, n, m, t, &F);
AfficheTheme(F);
reintialiser(T, n, m);
printf("\n Appuyez sur n'importe quelle touche pour continuer.");
char cc;
scanf("%s", &cc);
printf("\n\n\n\n\n\n\n\n\n");
}
if (c == 3)
{
affichecarte(T);
Stat(T, n, m);
printf("\n Appuyez sur n'importe quelle touche pour continuer.");
char cc;
scanf("%s", &cc);
printf("\n\n\n\n\n\n\n\n\n");
}
if (c <= 0 && c >= 4)
{
printf(" Merci !");
return 0;
}
}
}
Editor is loading...