sábado, 10 de maio de 2014

Lendo Matriz por um Arquivo e Ordenando

 O Programa começa lendo uma Matriz Quadrada e Embaralhada num Arquivo de texto de Nome "Matriz Embaralhada",logicamente precisei declarar outra Matriz, e dei o nome de mat_riz[10][10];Esta matriz recebe uma cópia de todos os elementos que foram lidos do Arquivo de texto pela primeira Matriz.
Para fazer uma ordenação completa, e não só por colunas ou linhas, declarei um vetor e dei o nome de vet [ 100 ];para 100 posições, este vetor tem seus elementos ordenados e são todos copiados em um Arquivo de texto criado para esta finalidade, e depois seus elementos são lidos na tela do DOS.

Para testar o programa basta copiar a Matriz de teste
incluída no código no bloco comentado, e salvar com o nome de: "Matriz Embaralhada.txt", Se o usuário salvar com um outro nome receberá a mensagem:

"Problemas na abertura do arquivo "

Programa criado no eclipse, e compilado pelo MinGW, que são minhas ferramentas padrão.

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





 Abaixo, se encontra este excelente código:

#include <stdio.h>
#include <conio.h>
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 );
}
// A matriz abaixo foi usada como teste
/*
36  48  34  89  25  56  1   59  2  81
15  22  65  17  91  6   11  93  26 55
85  8   35  24  39  27  76  14  21 99
66  95  87  69  100 51  20  18  71 46
33  45  57  67  80  50  64  82  83 92
31  13  94  16  47  10  61  63  74 60
54  88  37  77  23  38  86  43  72 78
70  98  73  52  42  28  68  3   41 75
58  79  84  97  4   5   9   19  7  32
44  62  90  53  40  30  96  29  12 49
 */
int main(){
    system ("title LENDO MATRIZ POR UM ARQUIVO E ORDENANDO");
    int matriz [ 9 ] [ 9 ] ;int mat_riz [ 10 ] [ 10 ] ;Janela5();
    int vet [ 100 ];
    int a = 0, i, j, aux, ord;
    FILE *arq = fopen("Matriz Embaralhada.txt", "r" );
    if ( arq == NULL ){
        textcolor(YELLOW);gotoxy(26,12);
        printf("Problemas na abertura do arquivo ");
        getche();
        return (0);
    }
    textcolor(LIGHTRED);gotoxy(25,3);printf("LENDO MATRIZ POR UM ARQUIVO E ORDENANDO");
    textcolor(LIGHTBLUE);gotoxy(26,5);printf("ABAIXO, A MATRIZ LIDA DO ARQUIVO TXT");
    textcolor(WHITE);
    for ( i = 0; i < 10; i++ ) {
        gotoxy(18, i  + 10);
        for ( j = 0; j < 10; j++ ) {
            fscanf( arq, "%3d", &matriz [ i ] [ j ] );
            printf(" %3d ", matriz [ i ] [ j ]  );
            mat_riz [ i ] [ j ] =  matriz [ i ] [ j ];
        }
    }
    fclose(arq);
    Sleep(1000);textcolor(LIGHTRED);gotoxy(32,22);printf("PRESSIONE QUALQUER TECLA");
    getche();system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(25,3);printf("LENDO MATRIZ POR UM ARQUIVO E ORDENANDO");
    textcolor(LIGHTBLUE);gotoxy(26,5);printf("ABAIXO, A MATRIZ COPIADA EM OUTRA");
    textcolor(LIGHTGREEN);
    for ( i = 0; i < 10; i++ ) {
        gotoxy(18, i  + 10);
        for ( j = 0; j < 10; j++ ) {
            printf(" %3d ", mat_riz [ i ] [ j ]  );
            vet [ a ] = mat_riz [ i ] [ j ];a++;
        }
    }
    Sleep(1000);textcolor(LIGHTRED);gotoxy(32,22);printf("PRESSIONE QUALQUER TECLA");
    getche();system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(25,3);printf("LENDO MATRIZ POR UM 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(25,3);printf("LENDO MATRIZ POR UM 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);
    char Linha [ 200 ];int result;
    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();
}

Nenhum comentário:

Postar um comentário

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