sábado, 16 de novembro de 2013

Struct Dinamica para função

Passar uma Estrutura inteira para uma função não é tão difícil
e em alguns casos pode ser necessário, e neste programa
mostro uma struct alocada dinâmicamente e passada inteira,para função.

Veja abaixo algumas imagens do programa em execusão :








Veja abaixo o código com a Estrutura alocada dinamicamente e passada para função:

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#define T 10
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( WHITE );
}
struct endereco{
    char nome [30];
    char rua [50];
    int numero;
    char bairro [20];
    char cidade [20];
    char sigla_estado [3];
    long int CEP;
};    int i = 0, j = 0;
char cad = '\0';
void StructFunc( struct endereco *teste [T] ){
    system ("title STRUCT DINAMICA II PARA FUNCAO");
    system("cls");
    for( j = 0; j < 40 ; j++ ){
        system("cls");Janela5();textcolor(LIGHTRED);gotoxy(28,3); printf("STRUCT DINAMICA II PARA FUNCAO");
        textcolor(BROWN);gotoxy(24,5);printf("Programa Desenvolvido Por:");
        textcolor(LIGHTMAGENTA);gotoxy(51,5);printf("Samuel Lima");
        textcolor(BLACK);gotoxy(35,7);printf("sa_sp10@hotmail.com");
        textcolor(LIGHTBLUE);gotoxy(28,9);printf("Aceita criar um cadastro " );
        gotoxy(18,10);printf(" [ C Para sim / S Para fim / E Para Exibir  ]  " );
        textcolor(LIGHTRED);    scanf(" %c", &cad );fflush ( stdin );
        if( tolower ( cad ) == 'e' )
            break;
        if( tolower ( cad ) == 's' ) {textcolor(LIGHTRED);gotoxy(33,15);
        printf ("O ROCCO AGRADECE");Sleep(1800);exit(0);}
        teste [ j ] = ( struct endereco * ) malloc ( sizeof(struct endereco  ) );
        system("cls");Janela5();
        textcolor(LIGHTRED);gotoxy(28,3); printf("STRUCT DINAMICA II PARA FUNCAO");
        textcolor(LIGHTBLUE);gotoxy(20,5);printf("Digite seu nome: ");
        textcolor(LIGHTRED);fgets( teste [j] -> nome, 30, stdin);
        textcolor(LIGHTBLUE);gotoxy(20,7);printf("Digite o nome da sua rua: ");
        textcolor(LIGHTRED);fgets( teste [j] -> rua, 50, stdin );
        textcolor(LIGHTBLUE);gotoxy(20,9);printf("Digite o nome do seu Bairro: ");
        textcolor(LIGHTRED);fgets( teste [j] -> bairro, 20, stdin );
        textcolor(LIGHTBLUE);gotoxy(20,11);printf("Digite o nome da sua Cidade:  ");
        textcolor(LIGHTRED);fgets( teste [j] -> cidade, 20, stdin );
        textcolor(LIGHTBLUE);gotoxy(20,13);printf("Digite a sigla do seu Estado: ");
        textcolor(LIGHTRED);fgets( teste [j] -> sigla_estado, 3, stdin);
        textcolor(LIGHTBLUE);gotoxy(20,15);printf("Digite o CEP da sua rua: ");
        textcolor(LIGHTRED);scanf( "%li", &teste [j] -> CEP );fflush(stdin);
    }
}
void StructFuncLer( struct endereco *teste [T] ){
    for( i = 0; i < j ; i++ ){
        system("cls");Janela5();
        textcolor(LIGHTRED);gotoxy(28,3);printf("STRUCT DINAMICA II PARA FUNCAO");
        textcolor(BLACK);gotoxy(30,5);printf("sa_sp10@hotmail.com");
        textcolor(LIGHTBLUE);gotoxy(20,7);printf("Meu nome e:");
        textcolor(LIGHTRED);gotoxy(32,7);printf( teste [i] -> nome );
        textcolor(LIGHTBLUE);gotoxy(20,9);printf("Minha rua se chama:");
        textcolor(LIGHTRED);gotoxy(41,9);printf( teste [i] -> rua );
        textcolor(LIGHTBLUE);gotoxy(20,11);printf("Meu bairro se chama:");
        textcolor(LIGHTRED);gotoxy(41,11);printf( teste [i] -> bairro);
        textcolor(LIGHTBLUE);gotoxy(20,13);printf("Minha cidade se chama:");
        textcolor(LIGHTRED);gotoxy(44,13);printf( teste [i] -> cidade);
        textcolor(LIGHTBLUE);gotoxy(20,15);printf("A sigla do meu estado e:");
        textcolor(LIGHTRED);gotoxy(45,15);printf( teste [i] -> sigla_estado );
        textcolor(LIGHTBLUE);gotoxy(20,17);printf("O CEP do meu bairro e:");
        textcolor(LIGHTRED);gotoxy(45,17);printf( "%li",teste [i] -> CEP );
        getche();system("cls");
    }
    free ( teste [ i ] );
}
int main(){
    struct endereco *teste [T];
    StructFunc( teste );
    StructFuncLer( teste );
}

veja abaixo a mesma Estrutura em modo simples:

[code
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#define T 10
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( WHITE );
}
struct endereco{
    char nome [30];
    char rua [50];
    int numero;
    char bairro [20];
    char cidade [20];
    char sigla_estado [3];
    long int CEP;
}teste [T];int i = 0, j = 0;
char cad = '\0';
void StructFunc( ){
    system ("title STRUCT DINAMICA II PARA FUNCAO");
    system("cls");
    for( j = 0; j < 40 ; j++ ){
        system("cls");Janela5();textcolor(LIGHTRED);gotoxy(28,3); printf("STRUCT DINAMICA II PARA FUNCAO");
        textcolor(BROWN);gotoxy(24,5);printf("Programa Desenvolvido Por:");
        textcolor(LIGHTMAGENTA);gotoxy(51,5);printf("Samuel Lima");
        textcolor(BLACK);gotoxy(35,7);printf("sa_sp10@hotmail.com");
        textcolor(LIGHTBLUE);gotoxy(28,9);printf("Aceita criar um cadastro " );
        gotoxy(18,10);printf(" [ C Para sim / S Para fim / E Para Exibir  ]  " );
        textcolor(LIGHTRED);    scanf(" %c", &cad );fflush ( stdin );
        if( tolower ( cad ) == 'e' )
            break;
        if( tolower ( cad ) == 's' ) {textcolor(LIGHTRED);gotoxy(33,15);
        printf ("O ROCCO AGRADECE");Sleep(1800);exit(0);}
        //teste [ j ] = ( struct endereco * ) malloc ( sizeof(struct endereco  ) );
        system("cls");Janela5();
        textcolor(LIGHTRED);gotoxy(28,3); printf("STRUCT DINAMICA II PARA FUNCAO");
        textcolor(LIGHTBLUE);gotoxy(20,5);printf("Digite seu nome: ");
        textcolor(LIGHTRED);fgets( teste [j] . nome, 30, stdin);
        textcolor(LIGHTBLUE);gotoxy(20,7);printf("Digite o nome da sua rua: ");
        textcolor(LIGHTRED);fgets( teste [j] . rua, 50, stdin );
        textcolor(LIGHTBLUE);gotoxy(20,9);printf("Digite o nome do seu Bairro: ");
        textcolor(LIGHTRED);fgets( teste [j] . bairro, 20, stdin );
        textcolor(LIGHTBLUE);gotoxy(20,11);printf("Digite o nome da sua Cidade:  ");
        textcolor(LIGHTRED);fgets( teste [j] . cidade, 20, stdin );
        textcolor(LIGHTBLUE);gotoxy(20,13);printf("Digite a sigla do seu Estado: ");
        textcolor(LIGHTRED);fgets( teste [j] . sigla_estado, 3, stdin);
        textcolor(LIGHTBLUE);gotoxy(20,15);printf("Digite o CEP da sua rua: ");
        textcolor(LIGHTRED);scanf( "%li", &teste [j] . CEP );fflush(stdin);
    }
}
void StructFuncLer( ){
    for( i = 0; i < j ; i++ ){
        system("cls");Janela5();
        textcolor(LIGHTRED);gotoxy(28,3);printf("STRUCT DINAMICA II PARA FUNCAO");
        textcolor(BLACK);gotoxy(30,5);printf("sa_sp10@hotmail.com");
        textcolor(LIGHTBLUE);gotoxy(20,7);printf("Meu nome e:");
        textcolor(LIGHTRED);gotoxy(32,7);printf( teste [i] . nome );
        textcolor(LIGHTBLUE);gotoxy(20,9);printf("Minha rua se chama:");
        textcolor(LIGHTRED);gotoxy(41,9);printf( teste [i] . rua );
        textcolor(LIGHTBLUE);gotoxy(20,11);printf("Meu bairro se chama:");
        textcolor(LIGHTRED);gotoxy(41,11);printf( teste [i] . bairro);
        textcolor(LIGHTBLUE);gotoxy(20,13);printf("Minha cidade se chama:");
        textcolor(LIGHTRED);gotoxy(44,13);printf( teste [i] . cidade);
        textcolor(LIGHTBLUE);gotoxy(20,15);printf("A sigla do meu estado e:");
        textcolor(LIGHTRED);gotoxy(45,15);printf( teste [i] . sigla_estado );
        textcolor(LIGHTBLUE);gotoxy(20,17);printf("O CEP do meu bairro e:");
        textcolor(LIGHTRED);gotoxy(45,17);printf( "%li",teste [i] . CEP );
        getche();system("cls");
    }
    //free ( teste [ i ] );
}
int main(){
    //struct endereco teste [T];
    StructFunc( );
    StructFuncLer( );
}



Nenhum comentário:

Postar um comentário

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