quarta-feira, 14 de maio de 2014

Alfabeto: Abrindo - Arquivo - Lendo - E Ordenando


O coração deste código é a função Quicksort, criada por
C.A.R. Hoare em 1960.Dizem ser um método rápido e muito eficiente, eu por exemplo, tem me dado bem com esta função, pode ver neste código que criei, como funciona bem.
O programa começa Imprimindo o nosso Alfabeto em Maiúsculo, que foi carregado no vetor de char str[];
usando a função strlnw(); da biblioteca string.h, o Alfabeto é convertido em Minúsculo, e é imprimido também assim.
Usando a função strcat(); também da biblioteca string.h, são concatenados e imprimidos também assim por printf.Em seguida, são enviados a um bloco, onde são embaralhados, e logo também são imprimidos, e são copiados num Arquivo de texto de nome: Alfabeto Desordenado,e são consequentemente lidos na tela do DOS.Então vem a famosa função quicksort(); que faz a Ordenação pela leitura do Arquivo "Alfabeto Desordenado".
Salva em um novo Arquivo, agora com o nome de: Afabeto Ordenado, e abre em modo leitura na tela do Dos.
E o programa termina agradecendo os usuários.

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




Veja abaixo o código do programa:

#include <stdio.h>
#include <string.h>
#include <conio.h>
//#include "gottextback.DLL"
void qs(char *items, int left, int right);
void quicksort(char *items, int len){
qs(items, 0, len-1);
}
void qs(char *items, int left, int right){
int i, j;
char x, y;
i = left; j = right;
x = items[( left+right) / 2 ];
do {
while((items[i] < x) && (i < right))
i++;
while((x < items[j]) && (j > left))
j--;
if(i <= j) {
y = items[i];
items[i] = items[j];
items[j] = y;
i++; j--;
}
} while(i <= j);

if(left < j)
qs(items, left, j);
if(i < right)
qs(items, i, right);
}
int main(){
system ("title ALFABETO : ABRINDO - ARQUIVO - LENDO E ORDENANDO");
char str [ 27 ] = {"ABCDEFGHIJKLMNOPQRSTUVXYWZ"};
FILE *arq;char *resul;
char str1 [ 53 ], Linha [ 100 ], mat_riz [ 10 ] [ 10 ], matriz [ 10 ] [ 10 ];
unsigned int i;int j;
textcolor(LIGHTRED);gotoxy(18,3);printf("ALFABETO : ABRINDO - ARQUIVO - LENDO E ORDENANDO");
textcolor(LIGHTBLUE);gotoxy(33,5);printf("Alfabeto Maiúsculo ");
textcolor(YELLOW);gotoxy(2,7);
for ( i = 0; i <= strlen ( str ); i++ ){
printf(" %c ", str [ i ] );
}
getche();strlwr (str);
textcolor(LIGHTBLUE);gotoxy(33,9);printf("Alfabeto Minúsculo ");
textcolor(YELLOW);gotoxy(2,11);
for ( i = 0; i <= strlen ( str ); i++ ){
printf(" %c ", str [ i ] );
strcpy(str1, str);
}
textcolor(LIGHTRED);gotoxy(30,21);
printf("PRESSIONE QUALQUER TECLA" );getche();
strupr( str );strlwr ( str1 );
strcat( str1, str );system("cls");
textcolor(LIGHTRED);gotoxy(18,3);printf("ALFABETO : ABRINDO - ARQUIVO - LENDO E ORDENANDO");
textcolor(LIGHTBLUE);gotoxy(12,5);
printf("Abaixo, os  %d Caracteres do alfabeto Maiúsculo e Minúsculo ", strlen ( str1 ));
textcolor(YELLOW);gotoxy(2,7);
for ( i = 0; i <= strlen ( str1 ); i++ ){
printf(" %c ", str1 [ i ] );
}
getche();
for ( i = 0; i < strlen( str1 ); i++ ){
j = i + rand() % ( strlen( str1 ) - i  );
char t = str1 [ j ];
str1 [ j ] = str1 [ i ];
str1 [ i ] = t;
}
textcolor(LIGHTBLUE);gotoxy(6,10);
printf("Abaixo, os  %d Caracteres do alfabeto Maiúsculo e Minúsculo Embaralhados", strlen( str1 ) );
textcolor(YELLOW);gotoxy(2,12);
for ( i = 0; i < strlen( str1 ); i++ ){
printf(" %c ", str1 [ i ] );
strcpy (*matriz, str1 );
}
arq = fopen ("Alfabeto Desordenado.txt", "w");
fprintf(arq,"%s",  *matriz);
fclose( arq );Sleep(1800);
textcolor(LIGHTGREEN);gotoxy(18,15);printf("Todos Elementos acima foram salvos num Arquivo de texto");
textcolor(LIGHTRED);gotoxy(30,21);printf("PRESSIONE QUALQUER TECLA" );
getche();system("cls");
textcolor(LIGHTRED);gotoxy(18,3);printf("ALFABETO : ABRINDO - ARQUIVO - LENDO E ORDENANDO");
textcolor(LIGHTBLUE);gotoxy(6,5);
printf("Lendo Abaixo, os  %d Caracteres do alfabeto Por um Arquivo de texto ", strlen ( str1 ));
arq = fopen("Alfabeto Desordenado.txt", "rt");
if (arq == NULL){
textcolor(LIGHTRED);gotoxy(26,17);printf("Problemas na abertura do arquivo\n");
getche();return (0);
}
i = 1;textcolor(YELLOW);gotoxy(6,7);
while (!feof(arq)){
resul = fgets(Linha, 11, arq);  
if (resul)  
textcolor(LIGHTRED);printf("\nLinha %d : ",i);
textcolor(YELLOW);printf(" %s ",Linha);
i++;
}
fclose(arq);getche();
textcolor(LIGHTBLUE);gotoxy(3,15);
printf("Lendo Abaixo, os  %d Caracteres do alfabeto Ordenado Por um Arquivo de texto ", strlen ( str1 ));
fscanf( arq, "%s", &matriz [ i ] );
strcpy (*mat_riz, *matriz );
quicksort( *mat_riz, strlen ( *mat_riz ) );
arq = fopen ("Alfabeto Ordenado.txt", "w");
fprintf(arq,"%s",  *mat_riz);
fclose( arq );
arq = fopen("Alfabeto Ordenado.txt", "rt");
if (arq == NULL){
textcolor(LIGHTRED);gotoxy(26,17);printf("Problemas na abertura do arquivo\n");
getche();return (0);
}
printf("\n\n");
i = 1;textcolor(YELLOW);
while (!feof(arq)){
resul = fgets(Linha, 11, arq); 
if (resul)  
textcolor(LIGHTRED);printf("\nLinha %d : ",i);
textcolor(YELLOW);printf(" %s ",Linha);
i++;
}
fclose(arq);
Sleep(1800);textcolor(LIGHTRED);gotoxy(36,23);printf("MUITO OBRIGADO");
getche();
return(0);
}

Nenhum comentário:

Postar um comentário

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