sábado, 17 de maio de 2014

Pares e Ímpares em Arquivo



Neste código mostro como fazer leitura de uma Matriz de inteiros
através de um arquivo de texto, e o programa é capaz de mostrar todos os números pares e ímpares contidos no arquivo, mostra ainda a quantidade de pares e de ímpares, as porcentagens de pares e de ímpares, e a soma de todos os pares e ímpares.

Veja algumas imagens do programa em execução:








//Veja abaixo o código do programa:
//A Matriz abaixo foi usada para testar o programa
/*

 36  48  34  89  25  56  1   59  2  81
 15  22  65  17  91  6   11  93  26 55
 85  8   35  24  39  27  76  14  21 99
 66  95  87  69  100 51  20  18  71 46
 33  45  57  67  80  50  64  82  83 92
 31  13  94  16  47  10  61  63  74 60
 54  88  37  77  23  38  86  43  72 78
 70  98  73  52  42  28  68  3   41 75
 58  79  84  97  4   5   9   19  7  32
 44  62  90  53  40  30  96  29  12 49

 */

#include <stdio.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 par [ 100 ];
int impar [ 100 ];
int Ler_Matriz_Txt ( int pr [ 9 ] [ 9 ] ) {
int mat_riz [ 10 ] [ 10 ];
Janela5 ( );
int i, j;
FILE *arq = fopen ( "Matriz de Pares e Ímpares.txt", "r" );
if ( arq == NULL ) {
textcolor ( YELLOW );
gotoxy ( 26, 12 );
printf ( "Problemas na abertura do arquivo " );
getche ( );
return ( 0 );
}
textcolor ( LIGHTRED );
gotoxy ( 30, 3 );
printf ( "PARES E ÍMPARES EM ARQUIVO" );
textcolor ( LIGHTBLUE );
gotoxy ( 26, 5 );
printf ( "ABAIXO, A MATRIZ LIDA DO ARQUIVO TXT" );
textcolor ( WHITE );
for ( i = 0; i < 10; i++ ) {
gotoxy ( 18, i + 10 );
for ( j = 0; j < 10; j++ ) {
fscanf ( arq, "%3d", &pr [ i ] [ j ] );
printf ( " %3d ", pr [ i ] [ j ] );
mat_riz [ i ] [ j ] = pr [ i ] [ j ];
}
}
fclose ( arq );
Sleep ( 1000 );
textcolor ( LIGHTRED );
gotoxy ( 32, 22 );
printf ( "PRESSIONE QUALQUER TECLA" );
getche ( );
system ( "cls" );
return ( 0 );
}
void Porcentagem_Pares_Impares ( int pr [ 9 ] [ 9 ] ) {
int f, g, i = 0, j = 0, ip, Pr;
for ( g = 0; g < 10; g++ ) {
for ( f = 0; f < 10; f++ ) {
if ( pr [ g ] [ f ] % 2 == 0 )
par [ i++ ] = pr [ g ] [ f ];
else impar [ j++ ] = pr [ g ] [ f ];
}
}
for ( f = 0; f < i; f++ ) {
Pr = par [ i ] / pr [ g ] [ f ] * 100;
}
for ( f = 0; f < i; f++ ) {
ip = impar [ j ] / pr [ g ] [ f ] * 100;
}
Pr = f * 100 / 100;
ip = j * 100 / 100;
Sleep ( 800 );
textcolor ( LIGHTBLUE );
gotoxy ( 8, 11 );
printf ( "A porcentagem dos numeros pares e: " );
Sleep ( 800 );
textcolor ( LIGHTMAGENTA );
gotoxy ( 45, 11 );
printf ( " %d ", Pr );
Sleep ( 800 );
textcolor ( LIGHTBLUE );
gotoxy ( 8, 13 );
printf ( "A porcentagem dos numeros impares e: " );
Sleep ( 800 );
textcolor ( LIGHTMAGENTA );
gotoxy ( 45, 13 );
printf ( " %d \n", ip );
Sleep ( 800 );
textcolor ( LIGHTGRAY );
gotoxy ( 30, 23 );
printf ( "PRESSIONE QUALQUER TECLA" );
getche ( );
}
void Numeros_Pares_Impares ( int pr [ 9 ] [ 9 ] ) {
system ( "cls" );
Janela5 ( );
textcolor ( LIGHTRED );
gotoxy ( 30, 3 );
printf ( "PARES E ÍMPARES EM ARQUIVO" );
textcolor ( LIGHTGRAY );
gotoxy ( 22, 5 );
printf ( "IMPRIMINDO A QUANTIDADE DE PARES E IMPARES" );
int f, g, i = 0, j = 0, ip, Pr;
for ( g = 0; g < 10; g++ ) {
for ( f = 0; f < 10; f++ ) {
if ( pr [ g ] [ f ] % 2 == 0 )
par [ i++ ] = pr [ g ] [ f ];
else impar [ j++ ] = pr [ g ] [ f ];
}
}
for ( f = 0; f < i; f++ ) {
Pr = par [ i ] / pr [ g ] [ f ] * 100;
}
for ( f = 0; f < i; f++ ) {
ip = impar [ j ] / pr [ g ] [ f ] * 100;
}
Sleep ( 800 );
textcolor ( LIGHTBLUE );
gotoxy ( 8, 7 );
printf ( "O numero de elementos Pares e: " );
Sleep ( 800 );
textcolor ( LIGHTMAGENTA );
gotoxy ( 43, 7 );
printf ( "%d", f );
Sleep ( 800 );
textcolor ( LIGHTBLUE );
gotoxy ( 8, 9 );
printf ( "O numero de elementos Impares e: " );
Sleep ( 800 );
textcolor ( LIGHTMAGENTA );
gotoxy ( 43, 9 );
printf ( "%d", j );
Sleep ( 800 );
textcolor ( LIGHTGRAY );
gotoxy ( 30, 23 );
printf ( "PRESSIONE QUALQUER TECLA" );
getche ( );
Porcentagem_Pares_Impares ( pr );
}
void Exibe_Matriz ( int pr [ 9 ] [ 9 ] ) {
int a = 0, i = 0, j = 0, result = 0;
Janela5 ( );
textcolor ( LIGHTRED );
gotoxy ( 30, 3 );
printf ( "PARES E ÍMPARES EM ARQUIVO" );
textcolor ( LIGHTBLUE );
gotoxy ( 29, 5 );
printf ( "IMPRIMINDO A MATRIZ ORIGINAL" );
textcolor ( YELLOW );
printf ( "\n" );
for ( i = 0; i < 10; i++ ) {
gotoxy ( 18, i + 8 );
for ( j = 0; j < 10; j++ ) {
par [ a ] = pr [ i ] [ j ];
printf ( " %3d ", pr [ i ] [ j ] );
result += par [ a ];
}
}
textcolor ( LIGHTBLUE );
gotoxy ( 24, 20 );
printf ( "A SOMA DOS ELEMENTOS DA MATRIZ E: " );
textcolor ( LIGHTMAGENTA );
gotoxy ( 58, 20 );
printf ( " %d ", result );
textcolor ( LIGHTGRAY );
gotoxy ( 30, 23 );
printf ( "PRESSIONE QUALQUER TECLA" );
getche ( );
}
void Imprime_Pares_Maior_Menor ( int pr [ 9 ] [ 9 ] ) {
system ( "cls" );
int f, g, i = 0, j = 0, m = 0, soma_pares = 0;
textcolor ( LIGHTRED );
gotoxy ( 30, 3 );
printf ( "PARES E ÍMPARES EM ARQUIVO" );
textcolor ( LIGHTBLUE );
gotoxy ( 29, 5 );
printf ( "IMPRIMINDO OS NUMEROS PARES" );
textcolor ( DARKGRAY );
gotoxy ( 32, 7 );
printf ( "Numeros pares da Matriz " );
gotoxy ( 2, 9 );
for ( g = 0; g < 10; g++ ) {
for ( f = 0; f < 10; f++ ) {
if ( pr [ g ] [ f ] % 2 == 0 )
par [ i++ ] = pr [ g ] [ f ];
else impar [ j++ ] = pr [ g ] [ f ];
}
}
for ( f = 0; f < i; f++ ) {
textcolor ( LIGHTMAGENTA );
printf ( " %d ", par [ f ] );
}
Sleep ( 800 );
textcolor ( LIGHTBLUE );
gotoxy ( 8, 13 );
printf ( "Esta e a soma dos Pares da Matriz: " );
gotoxy ( 41, 13 );
textcolor ( LIGHTMAGENTA );
Sleep ( 800 );
for ( f = 0; f < i; f++ ) {
soma_pares += par [ f ];
}
printf ( "  %d ", soma_pares );
f = 0;
m = par [ f ];
g = par [ f ];
for ( f = 0; f < i; f++ ) {
if ( par [ f ] > m ) {
m = par [ f ];
}
if ( par [ f ] < g ) {
g = par [ f ];
}
}
Sleep ( 800 );
textcolor ( LIGHTBLUE );
gotoxy ( 8, 15 );
printf ( "O maior Numero par e:" );
Sleep ( 800 );
textcolor ( LIGHTMAGENTA );
gotoxy ( 33, 15 );
printf ( " %d ", m );
Sleep ( 800 );
textcolor ( LIGHTBLUE );
gotoxy ( 8, 17 );
printf ( "O menor Numero par e:" );
Sleep ( 800 );
textcolor ( LIGHTMAGENTA );
gotoxy ( 33, 17 );
printf ( " %d ", g );
Sleep ( 800 );
textcolor ( LIGHTGRAY );
gotoxy ( 30, 23 );
printf ( "PRESSIONE QUALQUER TECLA" );
getche ( );
}
void Imprime_Ipares_Maior_Menor ( int pr [ 9 ] [ 9 ] ) {
system ( "cls" );
int f, g, i = 0, j = 0, m = 0, soma_pares = 0;
textcolor ( LIGHTRED );
gotoxy ( 30, 3 );
printf ( "PARES E ÍMPARES EM ARQUIVO" );
textcolor ( LIGHTBLUE );
gotoxy ( 29, 5 );
printf ( "IMPRIMINDO OS NUMEROS IMPARES" );
textcolor ( DARKGRAY );
gotoxy ( 32, 7 );
printf ( "Numeros Impares da Matriz " );
gotoxy ( 2, 9 );
for ( g = 0; g < 10; g++ ) {
for ( f = 0; f < 10; f++ ) {
if ( pr [ g ] [ f ] % 2 == 0 )
par [ i++ ] = pr [ g ] [ f ];
else impar [ j++ ] = pr [ g ] [ f ];
}
}
for ( f = 0; f < i; f++ ) {
textcolor ( LIGHTMAGENTA );
printf ( " %d ", impar [ f ] );
}
Sleep ( 800 );
textcolor ( LIGHTBLUE );
gotoxy ( 8, 13 );
printf ( "Esta e a soma dos Impares da Matriz: " );
gotoxy ( 44, 13 );
textcolor ( LIGHTMAGENTA );
Sleep ( 800 );
for ( f = 0; f < i; f++ ) {
soma_pares += impar [ f ];
}
printf ( "  %d ", soma_pares );
f = 0;
m = impar [ f ];
g = impar [ f ];
for ( f = 0; f < i; f++ ) {
if ( impar [ f ] > m ) {
m = impar [ f ];
}
if ( impar [ f ] < g ) {
g = impar [ f ];
}
}
Sleep ( 800 );
textcolor ( LIGHTBLUE );
gotoxy ( 8, 15 );
printf ( "O maior Numero Impar e:" );
Sleep ( 800 );
textcolor ( LIGHTMAGENTA );
gotoxy ( 33, 15 );
printf ( " %d ", m );
Sleep ( 800 );
textcolor ( LIGHTBLUE );
gotoxy ( 8, 17 );
printf ( "O menor Numero Impar e:" );
Sleep ( 800 );
textcolor ( LIGHTMAGENTA );
gotoxy ( 33, 17 );
printf ( " %d ", g );
Sleep ( 1000 );
textcolor ( LIGHTCYAN );
gotoxy ( 35, 23 );
printf ( "MUITO OBRIGADO" );
getche ( );
}
int main ( ) {
system ( "title PARES E ÍMPARES EM ARQUIVO" );
int pr [ 9 ] [ 9 ];
Ler_Matriz_Txt ( pr );
Exibe_Matriz ( pr );
Numeros_Pares_Impares ( pr );
Imprime_Pares_Maior_Menor ( pr );
Imprime_Ipares_Maior_Menor ( pr );
getche ( );
exit ( 0 );
}

Nenhum comentário:

Postar um comentário

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