quinta-feira, 25 de setembro de 2014

Pilha - Invertendo uma Matriz de Inteiros

Em linguagem C temos várias estruturas de dados, e pilha é uma estrutura
de fácil uso, e que permite remover e inserir elementos.
A regra básica da pilha, nos diz que o primeiro elemento inserido será o último
a ser removido.
Neste código a pilha opera de uma maneira especial, más não fugindo as regras,
Uma Matriz de inteiro foi declarada, e seus elementos são inseridos, ou melhor empilhados pela
função void Empi_lha ();, más é estática, apenas a estrutura da pilha foi alocado.
Na função void Desem_pilha  (); são desempilhados, suas posições são todas trocadas
num arranjo em posições sequênciais.

Veja abaixo algumas imagens do programa em execução:

 

Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
#define tam 10
void Janela5(){
    int lin, col;
    for ( lin = 0; lin <= 36; lin++ ){
        for ( col = 0; col <= 80; col++ ){
            gotoxy( col, lin );
            if ( lin == 2 ){textbackground(LIGHTRED);printf( " ");}
            if ( col == 1 ){textbackground(LIGHTRED);printf(" ");}
            if ( lin == 36 ){textbackground(LIGHTRED);}
            if ( col == 80 ){textbackground(LIGHTRED);printf(" ");}
        }
    }textbackground(BLACK);
}
void inicio( ){    system ("title PILHA - INVERTENDO UMA MATRIZ DE INTEIROS");
int i;do{Janela5();
textcolor(LIGHTRED);gotoxy(23,7);printf("PILHA - INVERTENDO UMA MATRIZR DE INTEIROS");
textcolor(YELLOW);gotoxy(25,10);printf("Programa desenvolvido por:");
textcolor(LIGHTCYAN);gotoxy(52,10);printf("Samuel Lima");
textcolor(LIGHTGREEN);gotoxy(34,12);printf("sa_sp10@hotmail.com");
textcolor(LIGHTBLUE);gotoxy(24,14);printf("DIGITE    PARA SAIR OU   PARA CONTINUAR  ");
textcolor(LIGHTRED);gotoxy(32,14);printf("1");textcolor(LIGHTRED);gotoxy(47,14);printf("2");
gotoxy(41,16);
scanf("%d",&i);fflush(stdin);
if( i == 1 ){textcolor(LIGHTRED);gotoxy(35,20);printf ("MUITO OBRIGADO");Sleep(1800);exit(0);}
if( i == 2 ){system("cls"); return;}
else {textcolor(LIGHTRED);gotoxy(37,20);printf ("\aOPÇÃO ERRADA");Sleep(1800);system("cls");}
}
while ( i );
}
typedef struct Pilha {
    int vol;
    struct Pilha *prox;
} Pil_ha;
void Empi_lha ( int c, Pil_ha *to_po ) {
    Pilha *item = ( Pilha * )  malloc ( sizeof ( Pilha ) );
    item -> vol = c;
    item -> prox = to_po -> prox;
    to_po -> prox = item;
}
int Desem_pilha ( Pil_ha *to_po ) {
    int c;
    Pil_ha *pt;
    pt = to_po -> prox;
    c = pt -> vol;
    to_po -> prox = pt -> prox;
    free ( pt );
    return c;
}
int main(){
    int Mat [ tam ] [ tam ] = {
            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};
    int i, j;
    Pil_ha cab;
    Pil_ha *top;
    top = &cab;
    top -> prox = NULL;
    inicio( );Janela5();
    textcolor(LIGHTRED);gotoxy(23,3);printf("PILHA - INVERTENDO UMA MATRIZ DE INTEIROS");
    textcolor(LIGHTBLUE);gotoxy(23,5);printf("Veja abaixo a impressão normal da Matriz ");
    textcolor(LIGHTGRAY);gotoxy(2,7);
    for( i = 0; i < tam; i++ ){
        gotoxy ( 18, i + 8 );
        for( j = 0; j < tam; j++ ){
            printf(" %3d ",  Mat [ i ] [ j ] );
        }
    }Sleep(1800);
    textcolor(LIGHTBLUE);gotoxy(28,20);printf("A MATRIZ FOI EMPILHADA COM SUCESSO");
        textcolor(LIGHTRED);gotoxy(32,22);printf("PRESSIONE QUALQUER TECLA");getche();
    textcolor(LIGHTGRAY);gotoxy(2,7);
    for( i = 0; i < tam; i++ ){
        for( j = 0; j < tam; j++ ){
            Empi_lha ( Mat  [ i ] [ j ], top );
        }
    }system("cls");
    textcolor(LIGHTRED);gotoxy(23,3);printf("PILHA - INVERTENDO UMA MATRIZ DE INTEIROS");
    textcolor(LIGHTBLUE);gotoxy(23,5);printf("Veja abaixo a Matriz totalmente invertida ");
    textcolor(LIGHTGRAY);gotoxy ( 1,9 );
    while ( top -> prox != NULL)
        printf(" %d ", Desem_pilha ( top ) );
    Sleep(1800);
    textcolor(LIGHTRED);gotoxy(36,23);printf ("MUITO OBRIGADO");
    getche();exit(0);
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.