domingo, 22 de dezembro de 2013

Matriz Estática Para Matriz Dinâmica


Algumas vêzes precisamos passar uma Matriz Estática para Matriz dinâmica, e se você tem
dificuldade de fazer isto, mostro neste código como pode ser feito.
O programa imprime a Matriz Estática inicializada com limites de um a cem, e conta o total de elementos contido,
depois, imprime os mesmos números já na Matriz Dinâmica e conta o total de elementos contido.
No bloco seguinte, cria um arquivo .txt e escreve a Matriz Dinâmica que foi inicializado na Matriz Estática.
Usando um recurso do Dos, abre fisicamente o notepad.exe, contendo a Matriz Dinâmica imprimida e fecha.
Em seguida, mostra a leitura do arquivo .txt contendo imprimido nossa Matriz Dinâmica.
E então o programa se encerra.

Veja algumas imagens do programa em execução:






Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#define tam 10
void Janela5(){
    int lin, col;
    for ( lin = 0; lin <= 25; lin++ ){
        for ( col = 0; col <= 80; col++ ){
            gotoxy( col, lin );
            if ( lin == 2 ){textbackground(LIGHTBLUE);printf( " ");}
            if ( col == 1 ){textbackground(LIGHTBLUE);printf(" ");}
            if ( lin == 25 ){textbackground(LIGHTBLUE);}
            if ( col == 80 ){textbackground(LIGHTBLUE);printf(" ");}
        }
    }textbackground( BLACK );
}
void Titulo(){int i;Janela5();
char *titulo = {"MATRIZ ESTATICA PARA MATRIZ DINAMICA"
        "CRIADO POR SAMUEL LIMA"
        "sa_sp10@hotmail.com"
        "PRESSIONE QUALQUER TECLA"};
textcolor(YELLOW);gotoxy(18,7);for ( i = 0; i < 27; i++){
    printf ("%c ",titulo [ i ]  );Sleep(50);
}
textcolor(LIGHTRED);gotoxy(34,9);for ( i = 27; i < 36; i++){
    printf ("%c ",titulo [ i ]  );Sleep(100);
}
textcolor(LIGHTGREEN);gotoxy(33,11);for ( i = 36; i < 46; i++){
    printf ("%c ",titulo [ i ]  );Sleep(100);
}
textcolor(LIGHTBLUE);gotoxy(29,13);for ( i = 46; i < 58; i++){
    printf ("%c ",titulo [ i ]  );Sleep(100);
}
textcolor(WHITE);gotoxy(23,15);for ( i = 58; i < 77; i++){
    printf ("%c ",titulo [ i ]  );Sleep(100);
}
textcolor(LIGHTCYAN);gotoxy(20,19);for ( i = 77; i < 101; i++){
    printf ("%c ",titulo [ i ]  );Sleep(100);
}
getche();
}
int main(){
     system ("title MATRIZ ESTATICA PARA MATRIZ DINAMICA nnn");
     Titulo();
    char Linha [ 100 ];int result;
    char str [ 1 ] [ 50 ] = {"IMPRIMINDO A MATRIZ DINAMICA NUM ARQUIVO .TXT\n\n"};
    int Vet [ 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 **matriz;
    int i, j, v = 0, t = 0, f = 0;
    gotoxy(27,3);clrscr();Janela5();
    matriz = ( int** ) malloc ( tam * sizeof ( int** ) );
    textcolor(LIGHTRED);gotoxy(26,3);printf("MATRIZ ESTATICA PARA MATRIZ DINAMICA  ");
    textcolor(LIGHTBLUE);gotoxy(29,5);printf("IMPRIMINDO A MATRIZ ORIGINAL");
    textcolor(YELLOW);
    FILE *arq = fopen ("Matriz.txt", "w");
    result = fprintf( arq, str [ 0 ] );
    for( i = 0; i < Vet [ 0 ] [ 9 ]; i++ ){  // Faz a leitura das linhas
        gotoxy(23, i + 8);
        for( j = 0; j < Vet [ 0 ] [ 9 ]; j++ ){  // Faz a leitura das colunas
            matriz [ t ] [ v ] = Vet [ i ] [ j ];
            printf(" %d ",  Vet [ i ] [ j ] ); // Imprime linhas e colunas
            f = Vet [ i ] [ j ];
        }
    }
    textcolor(LIGHTBLUE);gotoxy(24,19);
    printf ("Quantidade de elementos na Matriz:  %d", f );
    getche();clrscr();Janela5();
    textcolor(LIGHTRED);gotoxy(26,3);printf("MATRIZ ESTATICA PARA MATRIZ DINAMICA  ");
    textcolor(LIGHTBLUE);gotoxy(29,5);printf("IMPRIMINDO A MATRIZ DINAMICA");
    textcolor(LIGHTGREEN);printf("\n\n");
    for( i = 0; i < Vet [ 0 ] [ 9 ]; i++ ){  // Faz a leitura das linhas
        gotoxy(23, i + 8);
        for( j = 0; j < Vet [ 0 ] [ 9 ]; j++ ){  // Faz a leitura das colunas
            matriz [ t ] [ v ] = Vet [ i ] [ j ];
            f = matriz [ t ] [ v ];
            printf( " %d ", matriz [ t ] [ v ] );// Imprime linhas e colunas
            fprintf(arq," %d ", matriz [ t ] [ v ] );
        }
    }
    fclose( arq );
    textcolor(LIGHTBLUE);gotoxy(24,19);
    printf ("Quantidade de elementos na Matriz:  %d", f );
    getche();clrscr();
    textcolor(LIGHTRED);gotoxy(26,3);printf("MATRIZ ESTATICA PARA MATRIZ DINAMICA  ");
    textcolor(LIGHTBLUE);gotoxy(22,5);printf("Abrindo o arquivo .txt contendo a Matriz");getche();
    system("start Matriz.txt");getche();
    textcolor(LIGHTBLUE);gotoxy(20,7);printf("Fechando agora o arquivo .txt contendo a Matriz");getche();
    system("taskkill.exe /IM notepad.exe");gotoxy(1,7);clreol (); // Usando a funcao clreol (); que junto com gotoxy, apaga uma linha
    textcolor(LIGHTBLUE);gotoxy(23,7);printf("Abaixo, a leitura da Matriz num arquivo .txt");
    gotoxy(22,7);
    arq = fopen("Matriz.txt","rb");
    while (!feof(arq)){
        fgets(Linha, 100, arq);
        if (result) {
            textcolor(WHITE);printf("%s",Linha );
        }
    }
    getche();textcolor(LIGHTCYAN);gotoxy(33,23);printf("O ROCCO AGRADECE!");getche();
    free ( matriz );
}

Nenhum comentário:

Postar um comentário

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