quarta-feira, 20 de novembro de 2013

Programa Struct Ordenada

Para os estudantes avançados em linguagem C, apresento este excelente código, de ordenação de struct, matéria
de Estrutura de dados, onde com muita paciência criei, testei, e aprovei e o funcionamento do programa foi perfeito.
Pra entender este código, o estudante tem que ter conhecimentos prévios em vetores, e struct, saber criar e declarar.
A ordenação, é baseada nas funções strcoll(); e strcpy(); Ambas da biblioteca string.h.

Vejas várias imagens do programa em execução:


                          













Veja o código abaixo:

#include <stdio.h>
#include <string.h>
#include <conio.h>
#define T 3
void Janela5(){
    int lin, col;
    col = 0;
    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);
}
struct endereco{
    char nome [30];
    char rua [50];
    char bairro [20];
    char cidade [20];
    char sigla_estado [3];
    char cep [15];
};int i, j;
char Nome [ 30 ];char Rua [ 50 ];char Bairro [20];char Cidade [20];char Sigla_Estado [3];char CEP [15];
int main(){
    system ("title PROGRAMA STRUCT ORDENADA");
    struct endereco teste [20];
    system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(30,3); printf("PROGRAMA STRUCT ORDENADA");
    textcolor(YELLOW);gotoxy(24,5);printf("Programa Desenvolvido Por:");
    textcolor(LIGHTCYAN);gotoxy(51,5);printf("Samuel Lima");
    textcolor(LIGHTGREEN);gotoxy(35,7);printf("sa_sp10@hotmail.com");
    textcolor(LIGHTBLUE);gotoxy(30,11); printf("PRESSIONE QUALQUER TECLA");
    textcolor(YELLOW);gotoxy(40,13); printf("PARA");
    textcolor(LIGHTBLUE);gotoxy(37,15); printf("CONTINUAR");
    getche();
    for( j = 0; j < T ; j++ ){
        clrscr ();Janela5();
        textcolor(LIGHTRED);gotoxy(30,3); printf("PROGRAMA STRUCT ORDENADA");
        textcolor(LIGHTBLUE);gotoxy(20,5);printf("Digite seu nome: ");
        textcolor(LIGHTRED);fgets( teste [j] . nome, 30, stdin);fflush ( stdin );
        textcolor(LIGHTBLUE);gotoxy(20,7);printf("Digite o nome da sua rua: ");
        textcolor(LIGHTRED);fgets( teste [j] . rua, 50, stdin );fflush ( stdin );
        textcolor(LIGHTBLUE);gotoxy(20,9);printf("Digite o nome do seu Bairro: ");
        textcolor(LIGHTRED);fgets( teste [j] . bairro, 20, stdin );fflush ( stdin );
        textcolor(LIGHTBLUE);gotoxy(20,11);printf("Digite o nome da sua Cidade:  ");
        textcolor(LIGHTRED);fgets( teste [j] . cidade, 20, stdin );fflush ( stdin );
        textcolor(LIGHTBLUE);gotoxy(20,13);printf("Digite a sigla do seu Estado: ");
        textcolor(LIGHTRED);fgets( teste [j] . sigla_estado, 3, stdin );fflush ( stdin );
        textcolor(LIGHTBLUE);gotoxy(20,15);printf("Digite o CEP da sua rua: ");
        textcolor(LIGHTRED);fgets( teste [j] . cep, 15, stdin );fflush ( stdin );
    }
    clrscr ();
    for( j = 0; j < T ; j++ ){Janela5();
    textcolor(LIGHTRED);gotoxy(30,3); printf("PROGRAMA STRUCT ORDENADA");
    textcolor(LIGHTBLUE);gotoxy(20,5);printf("DADOS DO CADASTRO DESORDENADOS");
    textcolor(LIGHTCYAN);gotoxy(20,7);printf(" Nome => ");
    textcolor(LIGHTRED);gotoxy(30,7);printf( " %s ",teste [j] . nome);
    textcolor(LIGHTCYAN);gotoxy(20,9);printf(" Rua =>");
    textcolor(LIGHTRED);gotoxy(30,9);printf(" %s",teste [j] . rua );
    textcolor(LIGHTCYAN);gotoxy(20,11);printf(" Bairro =>");
    textcolor(LIGHTRED);gotoxy(30,11);printf(" %s",teste [j] . bairro );
    textcolor(LIGHTCYAN);gotoxy(20,13);printf(" Cidade =>     ");
    textcolor(LIGHTRED);gotoxy(30,13);printf(" %s",teste [j] . cidade );
    textcolor(LIGHTCYAN);gotoxy(20,15);printf(" Sigla =>");
    textcolor(LIGHTRED);gotoxy(30,15);printf(" %s",teste [j] . sigla_estado );
    textcolor(LIGHTCYAN);gotoxy(20,17);printf(" Cep =>");
    textcolor(LIGHTRED);gotoxy(30,17);printf(" %s",teste [j] . cep );
    getche();clrscr ();
    }
    for( i = 0; i < T; i++ ) {
        for( j = i; j > 0; j-- ) {
            if( strcoll( teste [ j ] . nome, teste [ j -1 ] . nome ) < 0 ){
                strcpy ( Nome, teste [ j ] . nome );
                strcpy ( teste [ j ] . nome, teste [ j -1 ] . nome );
                strcpy ( teste [ j -1 ] . nome, Nome );
            }
            if( strcoll( teste [ j ] . rua, teste [ j -1 ] . rua ) < 0 ){
                strcpy ( Rua, teste [ j ] . rua );
                strcpy ( teste [ j ] . rua, teste [ j -1 ] . rua );
                strcpy ( teste [ j -1 ] . rua, Rua );
            }
            if( strcoll( teste [ j ] . bairro, teste [ j -1 ] . bairro ) < 0 ){
                strcpy ( Bairro, teste [ j ] . bairro );
                strcpy ( teste [ j ] . bairro, teste [ j -1 ] . bairro );
                strcpy ( teste [ j -1 ] . bairro, Bairro );
            }
            if( strcoll( teste [ j ] . cidade, teste [ j -1 ] . cidade ) < 0 ){
                strcpy ( Cidade, teste [ j ] . cidade );
                strcpy ( teste [ j ] . cidade, teste [ j -1 ] . cidade );
                strcpy ( teste [ j -1 ] . cidade, Cidade );
            }
            if( strcoll( teste [ j ] . sigla_estado, teste [ j -1 ] . sigla_estado ) < 0 ){
                strcpy ( Sigla_Estado, teste [ j ] . sigla_estado );
                strcpy ( teste [ j ] . sigla_estado, teste [ j -1 ] . sigla_estado );
                strcpy ( teste [ j -1 ] . sigla_estado, Sigla_Estado );
            }
            if( strcoll( teste [ j ] . cep, teste [ j -1 ] . cep ) < 0 ){
                strcpy ( CEP, teste [ j ] . cep );
                strcpy ( teste [ j ] . cep, teste [ j -1 ] . cep );
                strcpy ( teste [ j -1 ] . cep, CEP );
            }
        }
    }
    system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(30,3); printf("PROGRAMA STRUCT ORDENADA");
    textcolor(LIGHTBLUE);gotoxy(28,5);printf(" Nomes de Pessoas Ordenadas");
    textcolor(LIGHTCYAN);
    for ( i = 0; i < T; i++ )
        printf("\n\n\t\t\t\t%s", teste [ i ] . nome );
    getche();system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(30,3); printf("PROGRAMA STRUCT ORDENADA");
    textcolor(LIGHTBLUE);gotoxy(28,5);printf(" Nomes de Ruas Ordenadas");
    textcolor(LIGHTCYAN);
    for ( i = 0; i < T; i++ )
        printf("\n\n\t\t\t\t%s", teste [ i ] . rua );
    getche();system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(30,3); printf("PROGRAMA STRUCT ORDENADA");
    textcolor(LIGHTBLUE);gotoxy(27,5);printf(" Nomes de Bairros Ordenados");
    textcolor(LIGHTCYAN);
    for ( i = 0; i < T; i++ )
        printf("\n\n\t\t\t\t%s", teste [ i ] . bairro );
    getche();system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(30,3); printf("PROGRAMA STRUCT ORDENADA");
    textcolor(LIGHTBLUE);gotoxy(27,5);printf(" Nomes de Cidades Ordenadas");
    textcolor(LIGHTCYAN);
    for ( i = 0; i < T; i++ )
        printf("\n\n\t\t\t\t%s", teste [ i ] . cidade );
    getche();system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(30,3); printf("PROGRAMA STRUCT ORDENADA");
    textcolor(LIGHTBLUE);gotoxy(26,5);printf(" Siglas de Estados Ordenados");
    textcolor(LIGHTCYAN);
    for ( i = 0; i < T; i++ )
        printf("\n\n\t\t\t\t%s", teste [ i ] . sigla_estado );
    getche();system("cls");Janela5();
    textcolor(LIGHTRED);gotoxy(30,3); printf("PROGRAMA STRUCT ORDENADA");
    textcolor(LIGHTBLUE);gotoxy(26,5);printf(" Ceps de Estados Ordenados");
    textcolor(LIGHTCYAN);
    for ( i = 0; i < T; i++ )
        printf("\n\n\t\t\t\t%s", teste [ i ] . cep );
    getche();
}









Nenhum comentário:

Postar um comentário

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