sexta-feira, 9 de maio de 2014

Matriz - Salvando num Arquivo E Ordenando

 
Esta Matriz Dinâmica, é carregada através de um contador, onde deteminei
previamente seus limites em 10 x 10, através do macro #tam.
Ela é imprimida primeiramente ordenada,más é enviada a um bloco onde se embaralha seus elementos, e assim é copiada em
um arquivo de texto, com o nome de "Matriz Embaralhada".
E em seguida o arquivo é aberto, sendo sua leitura na tela
do prompt de comando.
No próximo bloco, a Matriz é repassada num Vetor estático que foi definido em 100 posições, e passa por um processo de ordenação, vindo à ser imprimido ordenado, e salvo em um outro arquivo de texto, com o nome de "Matriz Ordenada", e posteriormente lido na tela do DOS também.

A função escolhida para leitura do arquivo, foi a "fgets",
Esta versátil função, pode ler linhas inteiras por completo,
ou aonde não for encontrado um "\n", uma operação bem sucedida faz com quer a função retorne o endereço da string, ou "NULL", se algo der errado.

Fique a vontade de editar este código, para que  fique claro
o seu funcionamento.

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

 




 

Veja abaixo o Código  do Programa:

 
#include <stdio.h>
#include <conio.h>
#include <stdlib.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 );
}
int main(){
    int a = 0, i, j, r, y, aux, cont = 0, ord, temp;
    int **matrix;int vet [ 100 ];
    system ("title MATRIZ - SALVANDO NUM ARQUIVO E ORDENANDO");
    clrscr();Janela5();
    textcolor(LIGHTRED);gotoxy(23,3);printf("MATRIZ - SALVANDO NUM ARQUIVO E ORDENANDO");
    textcolor(LIGHTBLUE);gotoxy(30,5);printf("IMPRIMINDO A MATRIZ ORIGINAL");
    textcolor(YELLOW);printf("\n\n");
    matrix = (int**) malloc ( tam * sizeof ( int** ) );
    for ( i = 0; i < tam; i++ ) {
        gotoxy(18, i + 8);
        matrix [ i ] = ( int* ) malloc( tam * sizeof(int* ) );
        for ( j = 0; j < tam; j++ ) {
            cont++;
            matrix [ i ] [ j ]  = cont;
            printf(" %3d ",matrix [ i ] [ j ] );
        }
        printf("\n");
    }
    textcolor(LIGHTBLUE);gotoxy(28,20);printf("A MATRIZ FOI GERADA COM SUCESSO");
    textcolor(LIGHTRED);gotoxy(32,22);printf("PRESSIONE QUALQUER TECLA");
    getche();system("cls");Janela5();
    for ( i = 0; i < tam; i++){
        for ( j = 0; j < tam; j++){
            r = rand() % tam;
            y = rand() % tam;
            temp = matrix [ i ] [ j ];
            matrix [ i ] [ j ] = matrix [ r ] [ y ];
            matrix [ r ] [ y ] = temp;
        }
    }
    textcolor(LIGHTRED);gotoxy(23,3);printf("MATRIZ - SALVANDO NUM ARQUIVO E ORDENANDO");
    textcolor(LIGHTBLUE);gotoxy(29,5);printf("Embaralhando Agora a Matriz ...");
    textcolor(LIGHTGREEN);int  f = 0, h = 0;
    FILE *arq = fopen ("Matriz Embaralhada.txt", "w");
    for( r = 0; r  < tam ; r++ ){
        gotoxy(18, r + 9);
        for( y = 0; y  < tam; y++ ){
            matrix [ f ] [ h ] = matrix [ r  ] [ y ];
            Sleep(100);
            printf(" %3d ",  matrix [ r  ] [ y ] );
            vet [ a ] = matrix [ r ] [ y ];a++;
            fprintf(arq," %d ",  matrix [ r ] [ y ] );
        }
    }
    textcolor(LIGHTBLUE);gotoxy(26,5);printf("A Matriz foi Embaralhada com sucesso");
    Sleep(1000);textcolor(LIGHTRED);gotoxy(32,22);printf("PRESSIONE QUALQUER TECLA");
    getche();fclose( arq );system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(23,3);printf("MATRIZ - SALVANDO NUM ARQUIVO E ORDENANDO");
    textcolor(LIGHTBLUE);gotoxy(23,5);printf("Abaixo, a leitura do Vetor num arquivo .txt");
    arq = fopen("Matriz Embaralhada.txt", "rb" );
    if ( arq == NULL ){
        textcolor(YELLOW);gotoxy(26,7);
        printf("Problemas na abertura do arquivo ");
        getche();
        return (0);
    }fclose( arq );
    char Linha [ 200 ];int result;
    gotoxy(1,7);textcolor(YELLOW);
    arq = fopen("Matriz Embaralhada.txt","rb");
    while ( !feof ( arq ) ){
        fgets( Linha, 200, arq );
        if (result) {
            printf("%s",Linha );
        }
    }
    Sleep(1000);textcolor(LIGHTRED);gotoxy(32,22);printf("PRESSIONE QUALQUER TECLA");
    getche();system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(23,3);printf("MATRIZ - SALVANDO NUM ARQUIVO E ORDENANDO");
    textcolor(LIGHTBLUE);gotoxy(25,5);printf("Imprimindo Agora a Matriz Ordenada");
    ord = 0;
    while(ord == 0){
        ord = 1;
        for(a = 0; a < 100 ; a++){
            if( vet [a]  > vet [(a + 1)]){
                aux = vet [a];
                vet [a] = vet [(a + 1)];
                vet [(a + 1 )] = aux;
                ord = 0;
            }
        }
    }
    textcolor(LIGHTGREEN);gotoxy(2,7);
    arq = fopen ("Matriz Ordenada.txt", "w");
    for(a = 0; a < 100; a++){
        Sleep(100);
        printf(" %2d", vet [ a ] );
        fprintf(arq," %d ",  vet [ a ] );
    }
    fclose( arq );
    Sleep(1000);textcolor(LIGHTRED);gotoxy(32,22);printf("PRESSIONE QUALQUER TECLA");
    getche();system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(23,3);printf("MATRIZ - SALVANDO NUM ARQUIVO E ORDENANDO");
    textcolor(LIGHTBLUE);gotoxy(23,5);printf("Abaixo, a leitura do Vetor num arquivo .txt");
    arq = fopen("Matriz Ordenada.txt", "rb" );
    if ( arq == NULL ){
        textcolor(YELLOW);gotoxy(26,7);
        printf("Problemas na abertura do arquivo ");
        getche();
        return (0);
    }fclose( arq );
    gotoxy(2,7);textcolor(LIGHTRED);
    arq = fopen("Matriz Ordenada.txt","rb");
    while ( !feof ( arq ) ){
        fgets( Linha, 200, arq );
        if (result) {
            printf("%s",Linha );
        }
    }
    Sleep(1000);textcolor(LIGHTCYAN);gotoxy(35,23);printf("MUITO OBRIGADO");
    getche();free(matrix);
}

Nenhum comentário:

Postar um comentário

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