Untitled

 avatar
user_9243973
plain_text
2 years ago
14 kB
8
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 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 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 i, j;
    printf("\n\n  La carte : \n");
    printf("  ");
    printf("%c", 201);

    for (i = 0; i< m; i++) // pour delimiter la carte : la premiere ligne de_
    {
        printf("%c", 205);
    }
    printf("%c", 187);

    for (i = 0; i < n; i++)
    {
        printf("\n");
        printf("  ");
        printf("%c", 186);

        for (j = 0; j < m; j++)
        {
            if (T[i][j] == 1)
            {
                SetColor(1);
            }
            if (T[i][j] == 2)
            {
                SetColor(4);
            } // pour que chaque nature ait sa propre couleur
            if (T[i][j] == 3)
            {
                SetColor(6);
            } // on appelle la fonction des couleurs
            if (T[i][j] == 4)
            {
                SetColor(2);
            }

            printf("%d", T[i][j]);
        }
        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 (i = 0; i < m; i++) // 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 x, int y, int n, int m, int nature)
{
    Pliste P;
    P = ExtraireObjet(T, x, y, n, m, nature);

    int i, j;
    printf("  ");
    printf("%c", 201);

    for (i = 0; i < m; i++) // pour delimiter la carte : la premiere ligne de_
    {
        printf("%c", 205);
    }
    printf("%c", 187);

    for (i = 0; i < n; i++)
    {
        printf("\n");
        printf("  ");
        printf("%c", 186);

        for ( j = 0; j < m;j++)
        {
            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 (i == P->x && j== 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 (i = 0; i < m; i++) // 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 i, j;

    printf("\n  ");
    printf("%c", 201);

    for (i = 0; i < m; i++) // pour delimiter la carte : la premiere ligne de_
    {
        printf("%c", 205);
    }
    printf("%c", 187);

    for (i = 0; i < n; i++)
    {
        printf("\n");
        printf("  ");
        printf("%c", 186);
        for (j = 0; j < m; j++)
        {
            if (Th[i][j] == 0)
                printf(" ");
            else
            {
                if (Th[i][j] == 1)
                {
                    SetColor(1);
                }
                if (Th[i][j] == 2)
                {
                    SetColor(4);
                } // pour que chaque nature ait sa propre couleur
                if (Th[i][j] == 3)
                {
                    SetColor(6);
                } // on appelle la fonction des couleurs
                if (Th[i][j] == 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 (i = 0; i < m; i++) // la derniere ligne de _
    {
        printf("%c", 205);
    }
    printf("%c", 188);
}
void Stat(int T[][100], int n, int m)
{
    int i, j, surface1, surface2, surface3, surface4;
    surface1 = 0;
    surface2 = 0;
    surface3 = 0;
    surface4 = 0;

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < m; j++)
        {
            if (T[i][j] == 1)
            {
                surface1 = surface1 + 1;
            }
            if (T[i][j] == 2)
            {
                surface2 = surface2 + 1;
            }
            if (T[i][j] == 3)
            {
                surface3 = surface3 + 1;
            }
            if (T[i][j] == 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      4.Pour quitter le programme. \n");
        int d;
        printf("\n  Entrez un chiffre : ");
        scanf("%d", &d);
        if (d == 1)
        {
            Tete=NULL;
            printf("\n  Veuillez entrer les coordonnees : \n");
            printf("  x :");
            scanf("%d", &x);
            while(x>=n)
            {
                printf("  Veuillez entrer une coordonnee valable. \n");
                printf("  x :");
                scanf("%d", &x);
            }
            printf("  y :");
            scanf("%d", &y);
            while(y>=m)
            {
                printf("  Veuillez entrer une coordonnee valable. \n");
                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");
        }
        if (d == 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);
            Initfile(&F);
            reintialiser(T, n, m);
            printf("\n  Appuyez sur n'importe quelle touche pour continuer.");
            char cc;
            scanf("%s", &cc);
            printf("\n");
        }

        if (d== 3)
        {
            affichecarte(T);
            Stat(T, n, m);
            printf("\n  Appuyez sur n'importe quelle touche pour continuer.");
            char cc;
            scanf("%s", &cc);
            printf("\n");
        }
        if (d == 4)
        {
            printf("  Merci !");
            return 0;
        }
    }
}
Editor is loading...