sábado, 17 de maio de 2014

Lendo Matriz de String por um Arquivo e Ordenando


Mais uma vez usando a função quicksort(); para ordenação de string,
apresento este ótimo código didático.
Primeiro o programa imprime todos os nomes contidos no arquivo de texto desordenados do lado esquerdo da tela do DOS, más a função quicksort(); Já ordena todos os nomes do arquivo à partir dele mesmo. "Muito bom".
E imprime do lado direito da tela totalmente em ordem.

Veja abaixo imagens do programa em execução:



Veja abaixo o código do programa:

#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 );
}
void quickSort( char items [ ] [ 17 ], int left, int right );
void quickSortMain( char items [ ] [ 17 ], int count ){
quickSort( items, 0, count -1 );
}
void quickSort( char items [ ] [ 17 ], int left, int right ){
int i, j;char *x, temp [ 17 ];
i = left;
j = right;
x = items [ ( left + right ) / 2 ];
do{
while( ( strcmp ( items [ i ], x ) < 0 ) && ( i < right ) ){
i++;
}
while( ( strcmp ( items [ j ], x ) > 0 ) && ( j > left ) ){
j--;
}
if( i <= j ){
strcpy( temp, items [ i ] );
strcpy( items [ i ], items [ j ] );
strcpy( items [ j ], temp );
i++;
j--;
}
} while( i <= j );
if( left < j ) {
quickSort( items, left, j );
}
if( i < right ){
quickSort( items, i, right );
}
}
int continuando( ){
int nr; do{system("cls");Janela5();
textcolor(LIGHTRED);gotoxy(17,3);printf("LENDO MATRIZ DE STRING POR UM ARQUIVO E ORDENANDO ");
textcolor(YELLOW);gotoxy(24,5);printf("Programa desenvolvido por:");
textcolor(LIGHTGRAY);gotoxy(51,5);printf("Samuel Lima");
textcolor(LIGHTGREEN);gotoxy(34,7);printf("sa_sp10@hotmail.com");
textcolor(LIGHTBLUE);gotoxy(24,9);printf ("Digite 0 para sair ou 1 para continuar ");
textcolor(LIGHTRED);scanf ( "%d", &nr );fflush(stdin);fflush(stdin);
if( nr == 0 ){
textcolor(LIGHTRED);gotoxy(35,21);printf("MUITO OBRIGADO");
getche();exit(0);
}
else if( nr == 1 ){
return 1;
}
textcolor(YELLOW);gotoxy(36,16);printf ("\aopcao errada!");
getche();
}while(1);
return 1;
}
int main(){
//Salve os nomes abaixos num arquivo de texto, com o nome de: Nomes.txt
/*
 Eder Costa
Humberto Gomes
Dijalma Lacerda
Caroline Silva
Igor Goncalves
Bruna Carla
Fabio Quadros
Geany Barros
Jaqueline Vega
Ana Celia
*/

system ("title LENDO MATRIZ DE STRING POR UM ARQUIVO E ORDENANDO");
continuando();
char matriz [ 10 ] [ 17 ];
int i;char *resul;
FILE *arq = fopen("Nomes.txt", "r" );
if ( arq == NULL ){
textcolor(YELLOW);gotoxy(26,12);
printf("Problemas na abertura do arquivo ");
getche();
return (0);
}system("cls");Janela5();
textcolor(LIGHTRED);gotoxy(17,3);printf("LENDO MATRIZ DE STRING POR UM ARQUIVO E ORDENANDO ");
textcolor(LIGHTBLUE);gotoxy(6,5);printf("MATRIZ TXT DESORDENADA");
textcolor(WHITE);
for ( i = 0; i < 10; i++ ) {
gotoxy(5, i  + 7);
resul = fgets(*matriz, 17, arq);
printf(" %s ", *matriz );
quickSortMain( matriz, 17 );
}
fclose(arq);
getche();
textcolor(LIGHTBLUE);gotoxy(46,5);printf("MATRIZ TXT ORDENADA ");
textcolor(WHITE);
for(i = 0; i < 10; i++ ){
gotoxy(45, i  + 7);
printf(" %s ", matriz [ i ] );
}
getche();
textcolor(LIGHTRED);gotoxy(35,21);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.