sábado, 7 de dezembro de 2013

Programa União e Intersecçao de Vetores

Conjunto interseção são os elementos comuns entre conjuntos relacionados.
E para os que estão estudando vetores, apresento este excelente código em linguagem c,que está preparado para mostrar união e intersecção de vetores, simulando dois conjuntos de números inteiros naturais de cinco elementos cada um.

Veja algumas imagens do Programa em execução:

                





Veja abaixo o código do programa:

#include<stdio.h>
#include<stdlib.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 );
}
int main ( ) {
#define tam 5
     int V = 10;
     int i, j, k, vezes, aux;
     system ( "title PROGRAMA UNIAO E INTERSECCAO DE VETORES" );
     int Vetor [ tam ], Vet_or [ tam ], Vet [ 10 ], rep [ ] = { 0 };
     for ( i = 0; i < tam ; i++ ) {
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 24 , 3 );
         printf ( "PROGRAMA UNIAO E INTERSECCAO DE VETORES" );
         textcolor ( YELLOW );
         gotoxy ( 24 , 5 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( LIGHTCYAN );
         gotoxy ( 51 , 5 );
         printf ( "Samuel Lima" );
         textcolor ( LIGHTGREEN );
         gotoxy ( 34 , 7 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 19 , 9 );
         printf ( "Digite um numero para a posicao " );
         textcolor ( LIGHTRED );
         printf ( " %d%c " , i + 0 , 167 );
         textcolor ( LIGHTBLUE );
         printf ( "do Vetor A" );
         textcolor ( YELLOW );
         gotoxy ( 66 , 9 );
         scanf ( "%d" , &Vetor [ i ] );
         fflush ( stdin );
         system ( "cls" );
     }
     system ( "cls" );
     for ( i = 0; i < tam ; i++ ) {
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 24 , 3 );
         printf ( "PROGRAMA UNIAO E INTERSECCAO DE VETORES" );
         textcolor ( YELLOW );
         gotoxy ( 24 , 5 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( LIGHTCYAN );
         gotoxy ( 51 , 5 );
         printf ( "Samuel Lima" );
         textcolor ( LIGHTGREEN );
         gotoxy ( 34 , 7 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 19 , 9 );
         printf ( "Digite um numero para a posicao " );
         textcolor ( LIGHTRED );
         printf ( " %d%c " , i + 0 , 167 );
         textcolor ( LIGHTBLUE );
         printf ( "do Vetor B" );
         textcolor ( YELLOW );
         gotoxy ( 66 , 9 );
         scanf ( "%d" , &Vet_or [ i ] );
         fflush ( stdin );
         system ( "cls" );
     }
     Janela5 ( );
     textcolor ( LIGHTRED );
     gotoxy ( 24 , 3 );
     printf ( "PROGRAMA UNIAO E INTERSECCAO DE VETORES" );
     textcolor ( YELLOW );
     gotoxy ( 25 , 5 );
     printf ( "ESTES SAO OS VETORES PREENCHIDOS" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 29 , 7 );
     printf ( "Vetor A " );
     textcolor ( LIGHTRED );
     gotoxy ( 29 , 9 );
     for ( i = 0; i < tam ; i++ ) {
         printf ( " %d " , Vetor [ i ] );
     }
     getche ( );
     textcolor ( LIGHTBLUE );
     gotoxy ( 29 , 11 );
     printf ( "Vetor B " );
     textcolor ( LIGHTRED );
     gotoxy ( 29 , 13 );
     for ( i = 0; i < tam ; i++ ) {
         printf ( " %d " , Vet_or [ i ] );
     }
     getche ( );
     for ( i = 0; i < tam ; i++ ) {
         Vet [ i ] = Vetor [ i ];
         Vet [ i + 5 ] = Vet_or [ i ];
     }
     int temp; // Metodo da selecao
     for ( i = 0; i <= V ; i++ ) {
         temp = i;
         aux = Vet [ i ];
         for ( j = ( i + 1 ); j <= V ; j++ )
              if ( Vet [ j ] <= aux ) {
                   temp = j;
                   aux = Vet [ temp ];
                   Vet [ temp ] = Vet [ i ];
                   Vet [ i ] = aux;
              }
     } // Metodo da selecao
     for ( i = 0; i <= V ; i++ ) {
         vezes = 1;
         j = i + 1;
         while ( j <= V )
              if ( Vet [ j ] != Vet [ i ] )
                   j++;
              else {
                   vezes++;
                   V--;
                   Vet [ j ] = Vet [ V + 1 ];
              }
     }
     for ( i = 0; i <= V ; i++ ) {
         temp = i;
         aux = Vet [ i ];
         for ( j = ( i + 1 ); j <= V ; j++ )
              if ( Vet [ j ] <= aux ) {
                   temp = j;
                   aux = Vet [ temp ];
                   Vet [ temp ] = Vet [ i ];
                   Vet [ i ] = aux;
              }
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 20 , 15 );
     printf ( "Abaixo, a uniao dos dois Vetores ja ordenados" );
     textcolor ( LIGHTRED );
     gotoxy ( 12 , 17 );
     for ( i = 1; i <= V ; i++ )
         printf ( " %d " , Vet [ i - 1 ] );
     getche ( );
     k = 0;
     for ( j = 0; j < tam ; j++ )
         for ( i = 0; i < tam ; i++ )
              if ( Vetor [ i ] == Vet_or [ j ] ) {
                   rep [ k ] = Vetor [ i ];
                   k++;
              }
     textcolor ( LIGHTBLUE );
     gotoxy ( 23 , 19 );
     printf ( "Numeros comuns entre os dois Vetores " );
     textcolor ( LIGHTRED );
     gotoxy ( 29 , 21 );
     for ( j = 0; j < k ; j++ )
         printf ( " %d " , rep [ j ] );
     if ( rep [ j ] == 0 ) {
         textcolor ( LIGHTGREEN );
         printf ( " Vetores distintos" );
     }
     textcolor ( YELLOW );
     gotoxy ( 35 , 23 );
     printf ( "O ROCCO AGRADECE" );
     getche ( );
     exit ( 0 );
}


Nenhum comentário:

Postar um comentário

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