segunda-feira, 24 de agosto de 2015

Múltiplos de números em vetor

Neste programa eu aproveito a função Gera_Num_Sem_Repetidos ( int *A );
Que foi usada num código anterior, ela gera números aleatórios de 1 a 100 sem repetições e copia num vetor que foi alocado dinamicamente na função main(); e passado uma cópia como parâmetros para a função citada acima.
Após a impressão dos números na tela do cmd personalizado pelas côres utilizadas e textos bem posicionados com o uso fe funções da biblioteca conio.h, aliás, isto é um hábito que tenho, e muitos me criticam por isto, a entrada de dados pede que se digite um número entre 2 e 50, e aqui há restrições, para que não passe números inferiores ou superiores ao que foi solicitado, barrando também caracteres.
Então é apresentado os múltiplos do número inserido.
Mais uma contribuição aos iniciantes em linguagem C.

Veja abaixo imagens do programa em execução:







Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
#define tam 100
void Janela5 ( ) {
     int lin, col;
     col = 0;
     system ( "Color 10" );
     for ( lin = 2; lin <= 31; lin++ )
         for ( col = 2; col <= 78; col++ ) {
              gotoxy ( col, lin );
              textbackground ( WHITE );
              printf ( " " );
         }
}
int continuando ( ) {
     int nr;
     do {
         system ( "Color 90" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 28, 3 );
         printf ( "MÚLTIPLOS DE NÚMEROS EM VETOR" );
         textcolor ( BROWN );
         gotoxy ( 25, 7 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( GREEN );
         gotoxy ( 52, 7 );
         printf ( "Samuel Lima" );
         textcolor ( BLACK );
         gotoxy ( 34, 9 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 23, 11 );
         printf ( "Digite " );
         textcolor ( LIGHTRED );
         printf ( " 0 " );
         textcolor ( LIGHTBLUE );
         printf ( " para sair ou " );
         textcolor ( LIGHTRED );
         printf ( " 1 " );
         textcolor ( LIGHTBLUE );
         printf ( " para continuar " );
         textcolor ( LIGHTRED );
         gotoxy ( 24, 13 );
         scanf ( "%d", &nr );
         fflush ( stdin );
         if ( nr == 0 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 36, 18 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         } else if ( nr == 1 ) {
              return 1;
         }
         textcolor ( BLACK );
         gotoxy ( 36, 16 );
         printf ( "\aopcão errada!" );
         getche ( );
     } while ( 1 );
     return 1;
}
int Gera_Num_Sem_Repetidos ( int *A ) {
     int aut, i, j, result = 0, t = 100;
     for ( i = 0; i < 100; i++ ) {
         do {
              do {
                   t = rand ( ) % 100;
                   if ( t == 0 ) {
                        t = 100;
                   }
                   break;
              } while ( 1 );
              aut = 0;
              for ( j = 0; j < 100; j++ )
                   if ( t == A [ j ] )
                        aut = 1;
              if ( aut == 0 ) {
                   A [ i ] = t;
              }
         } while ( aut == 1 );
     }
     textcolor ( LIGHTRED );
     gotoxy ( 28, 3 );
     printf ( "MÚLTIPLOS DE NÚMEROS EM VETOR" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 21, 5 );
     printf ( "Abaixo os 100 números gerados sem repetições\n" );
     textcolor ( BLACK );
     gotoxy ( 17, 7 );
     for ( i = 0; i < 100; i++ ) {
         if ( i == 10 ) {
              gotoxy ( 17, 8 );
         }
         if ( i == 20 ) {
              gotoxy ( 17, 9 );
         }
         if ( i == 30 ) {
              gotoxy ( 17, 10 );
         }
         if ( i == 40 ) {
              gotoxy ( 17, 11 );
         }
         if ( i == 50 ) {
              gotoxy ( 17, 12 );
         }
         if ( i == 60 ) {
              gotoxy ( 17, 13 );
         }
         if ( i == 70 ) {
              gotoxy ( 17, 14 );
         }
         if ( i == 80 ) {
              gotoxy ( 17, 15 );
         }
         if ( i == 90 ) {
              gotoxy ( 17, 16 );
         }
         printf ( " %3d ", A [ i ] );
         result += A [ i ];
     }
     textcolor ( LIGHTBLUE );
     gotoxy ( 20, 18 );
     Sleep ( 500 );
     printf ( "Total de números gerados ==> " );
     textcolor ( LIGHTRED );
     printf ( "%d", i );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20, 19 );
     Sleep ( 500 );
     printf ( "Soma dos números gerados ==> " );
     textcolor ( LIGHTRED );
     printf ( "%d", result );
     Sleep ( 500 );
     return ( 0 );
}
int Multiplos ( char num [ 4 ] ) {
     unsigned int i;
     for ( i = 0; i < strlen ( num ); i++ ) {
         if ( num [ i ] < '0' || num [ i ] > '9' )
              return 0;
     }
     return 1;
}
int main ( ) {
     system ( "title MÚLTIPLOS DE NÚMEROS EM VETOR" );
     continuando ( );
     Janela5 ( );
     int *A, i, j, x = 0, Vet [ tam ];
     char num [ 3 ];
     A = ( int* ) malloc ( tam * sizeof(int) );
     Gera_Num_Sem_Repetidos ( A );
     do {
         textcolor ( LIGHTBLUE );
         gotoxy ( 20, 21 );
         printf ( "Digite um número entre 2 e 50 para sabermos" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 20, 22 );
         printf ( "quais são seus múltiplos no vetor: " );
         textcolor ( LIGHTRED );
         gotoxy ( 20, 36 );
         gets ( num );
         fflush ( stdin );
         j = atoi ( num );
         gotoxy ( 20, 24 );
         textcolor ( LIGHTBLUE );
         if ( j < 2 || j > 50 ) {
              printf ( "\aSó são aceitos Números Acima de 2 e menor que 50" );
              textcolor ( YELLOW );
              gotoxy ( 31, 28 );
              printf ( "PRESSIONE QUALQUER TECLA" );
              getche ( );
              gotoxy ( 15, 24 );
              clreol ( );
              gotoxy ( 20, 25 );
              clreol ( );
              gotoxy ( 53, 22 );
              clreol ( );
              gotoxy ( 31, 28 );
              clreol ( );
         }
         if ( Multiplos ( num ) == 0 ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 20, 25 );
              printf ( "Você digitou " );
              textcolor ( LIGHTRED );
              printf ( "%s", num );
              textcolor ( LIGHTBLUE );
              gotoxy ( 20, 26 );
              printf ( "\aIsso não é um número válido !" );
              textcolor ( YELLOW );
              gotoxy ( 31, 28 );
              printf ( "PRESSIONE QUALQUER TECLA" );
              getche ( );
              gotoxy ( 15, 25 );
              clreol ( );
              gotoxy ( 20, 26 );
              clreol ( );
              gotoxy ( 32, 22 );
              clreol ( );
              gotoxy ( 31, 28 );
              clreol ( );
         }
     } while ( ( j < 2 ) || ( j > 50 ) );
     textcolor ( LIGHTBLUE );
     gotoxy ( 20, 24 );
     printf ( "Abaixo estão os Múltiplos de ==> " );
     textcolor ( LIGHTRED );
     printf ( "%d", j );
     textcolor ( LIGHTRED );
     gotoxy ( 3, 26 );
     for ( i = 0; i < 100; i++ )
         if ( ! ( A [ i ] % j ) ) {
              Vet [ x ] = A [ i ];
              x++;
         }
     int temp;
     for ( i = 0; i < x - 1; i++ ) {
         for ( j = 0; j < x - 1; j++ ) {
              if ( Vet [ j ] > Vet [ j + 1 ] ) {
                   temp = Vet [ j ];
                   Vet [ j ] = Vet [ j + 1 ];
                   Vet [ j + 1 ] = temp;
              }
         }
     }
     textcolor ( BLACK );
     gotoxy ( 11, 26 );
     for ( i = 0; i < x; i++ ) {
         if ( i == 15 ) {
              gotoxy ( 11, 27 );
         }
         if ( i == 30 ) {
              gotoxy ( 11, 28 );
         }
         if ( i == 45 ) {
              gotoxy ( 11, 29 );
         }
         if ( i == 60 ) {
              gotoxy ( 11, 30 );
         }
         printf ( " %2d ", Vet [ i ] );
     }
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 36, 30 );
     printf ( "MUITO OBRIGADO" );
     getche ( );

}

Nenhum comentário:

Postar um comentário

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