sexta-feira, 30 de novembro de 2012

Ainda mais operações em Vetor

Ainda mais operações em Vetor, Vem apresentando mais evoluções neste Programa,
que é semelhante ao programa "Preenchendo um Vetor e operando", só que com mais recursos;
Pra começar, quando os números são digitados no teclado, ele ínserido
no Vetor [a], que tem o tamanho de 8 posições, então, de imediato, ele
já entra num bloco para ser ordenado, depois da ordenação, segue o seu curso normal.
Introduzi novas funções neste programa, a saber:

1 - Número de elementos pares
2 - Número de elementos ímpares
3 - Porcentagem dos números pares contido no vetor
4 - Porcentagem dos números ímpares contido no vetor

Copie, cole e compile:

CODE
#include <stdio.h>
#include <conio2.h>
int Vetor [8];int par [8]; int impar [8];
void Elementos_Vetor();
void Digitando();
void Numeros_Elementos_Vetor();
void Soma_Indices_Vetor();
//void Mostra_Elementos_Vetor();
void Maior_Menor_Elemento_Vetor();
void Numeros_Pares_Impares();
void Imprime_Pares_Maior_Menor();
void Imprime_Impares_Maior_Menor();
void Porcentagem_Pares_Impares();
void Janela6(){
    int lin, col;
    for(lin = 2; lin <= 24; lin++)
        for(col = 3; col<= 78; col++)
        {
            gotoxy(col,lin);textbackground(6);printf(" ");
        }
    for(lin = 5; lin <= 21; lin++)
        for(col = 6; col <= 75; col++)
        {
            gotoxy(col, lin);textbackground(16); printf(" ");
        }
}
void inicio(){
    system("cls");
    int i;
    textbackground(BLACK);Janela6();
    do{
        textcolor(LIGHTRED);gotoxy(27,7);printf("PREENCHENDO UM VETOR E OPERANDO");
        textcolor(YELLOW);gotoxy(22,10);printf("Programa desenvolvido por:");
        textcolor(LIGHTCYAN);gotoxy(49,10);printf("Samuel Lima");
        textcolor(LIGHTGREEN);gotoxy(30,12);printf("sa_sp10@hotmail.com");
        textcolor(LIGHTBLUE);gotoxy(21,14);printf("DIGITE    PARA SAIR OU   PARA CONTINUAR:  ");
        textcolor(LIGHTRED);gotoxy(29,14);printf("1");textcolor(LIGHTRED);gotoxy(44,14);printf("2");
        gotoxy(21,16);
        scanf("%d",&i);fflush(stdin);
        if(i == 1){
            textcolor(LIGHTRED);gotoxy(33,20);printf ("O ROCCO AGRADECE");Sleep(1800);exit(0);
        }
        if(i == 2){system("cls");
        Digitando();
        }
        else {
            textcolor(LIGHTBLUE);gotoxy(30,20);printf ("\aOPCAO ERRADA");Sleep(1800);inicio();
        }
    }
    while(i);
}
void Digitando(){
    Elementos_Vetor();
    Numeros_Elementos_Vetor();
    Soma_Indices_Vetor();
    Maior_Menor_Elemento_Vetor();
    Numeros_Pares_Impares();
    Porcentagem_Pares_Impares();
    Imprime_Pares_Maior_Menor();
    Imprime_Impares_Maior_Menor();
}
void Elementos_Vetor(){
    system("cls");
    textcolor(LIGHTRED);gotoxy(30,3);printf ( "PREENCHENDO UM VETOR E OPERANDO" );
    int a = -1, b, f = 0, aux;
    for(a = 0; a < 8; a++){
        textcolor(LIGHTGREEN);gotoxy(25,5);printf("Digite um Numero para a posicao");
        textcolor(LIGHTRED);gotoxy(57,5);printf("%d:  ", f);
        textcolor(YELLOW);scanf("%d",& Vetor [a]);fflush(stdin);
    }
    a--;
    for(b = 0; b < a; b++)
        for(f = 0; f < a; f++)
            if(Vetor [f] > Vetor [f + 1]){
                aux = Vetor [f];
                Vetor [f] = Vetor [f + 1];
                Vetor [f + 1] = aux;
            }
    textcolor(LIGHTBLUE);gotoxy(8,7);printf("Estes são os elementos do Vetor: ");
    textcolor(LIGHTMAGENTA);for(b = 0; b <= a; b++)printf("%i ",Vetor[b]);
    Sleep(800);
}

void Mostra_Elementos_Vetor(){
    int b;
    system("cls");
    textcolor(LIGHTRED);gotoxy(30,3);printf ( "PREENCHENDO UM VETOR E OPERANDO" );
    textcolor(LIGHTBLUE);gotoxy(8,5);printf("Estes são os elementos do Vetor: ");
    textcolor(LIGHTMAGENTA);gotoxy(8,7);
    for(b = 0; b < 8; b++)
        printf(" %i ",Vetor[b]);
    Sleep(800);
}

void Numeros_Elementos_Vetor(){
    int i, f, pr;
    for (f = 0; f < 8; f++){
        pr = par [i] / Vetor [f] * 100;
    }
    textcolor(LIGHTBLUE);gotoxy(8,9);Sleep(800);printf("O numero de elementos no Vetor e: ");
    Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(44,9);printf("%d \n", f);
}
void Soma_Indices_Vetor(){
    int f, resp = 0;
    textcolor(LIGHTBLUE);gotoxy(8,11);Sleep(800);printf ( "Esta e a soma dos indices do Vetor: " );
    gotoxy(43,11);textcolor(LIGHTMAGENTA);Sleep(800);
    for ( f = 0; f < 8; f++ ){
        resp += Vetor [f];
    }
    printf ( "  %d ",  resp);
}
void Maior_Menor_Elemento_Vetor(){
    int  f = 0;
    int  m = Vetor [f];
    int g = Vetor [f];
    for ( f = 0; f < 8; f++ ){
        if ( Vetor [f] > m ){
            m = Vetor [f];
        }
        if ( Vetor [f] < g ){
            g = Vetor [f];
        }
    }
    Sleep(800);
    textcolor(LIGHTBLUE);gotoxy(8,13);printf("O maior Numero no Vetor e:");
    Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(36,13);printf(" %d ", m);
    Sleep(800);
    textcolor(LIGHTBLUE);gotoxy(8,15);printf("O menor Numero no Vetor e:");
    Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(36,15);printf("%d", g);
}
void Numeros_Pares_Impares(){
    int f, i = 0, j = 0, ip, pr;
    for (f = 0; f < 8; f++){
        if (Vetor [f] % 2 == 0) par [i++] = Vetor [f];
        else
            impar [j++] = Vetor [f];
    }
    for (f = 0; f < i; f++){
        pr = par [i] / Vetor [f] * 100;
    }
    for (f = 0; f < i; f++){
        ip = impar [j] / Vetor [f] * 100;
    }
    Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,17);printf("O numero de elementos Pares e: ");
    Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(43,17);printf("%d", f);
    Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,19);printf("O numero de elementos Impares e: ");
    Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(43,19);printf("%d", j);
    getche();
}
void Porcentagem_Pares_Impares(){
    Mostra_Elementos_Vetor();

    int f, i = 0, j = 0, ip, pr;
    for (f = 0; f < 8; f++){
        if (Vetor [f] % 2 == 0) par [i++] = Vetor [f];
        else
            impar [j++] = Vetor [f];
    }
    for (f = 0; f < i; f++){
        pr = par [i] / Vetor [f] * 100;
    }
    for (f = 0; f < i; f++){
        ip = impar [j] / Vetor [f] * 100;
    }
    pr = f * 100 / 8;
    ip = j * 100 / 8;
    Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,9);printf("A porcentagem dos numeros pares e: ");
    Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(45,9);printf(" %d ", pr);
    Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,11);printf("A porcentagem dos numeros impares e: ");
    Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(45,11);printf(" %d \n",  ip);       // até aki ok
    Sleep(800);
}
void Imprime_Pares_Maior_Menor(){
    int f, g, i = 0, j = 0, m = 0, soma_pares = 0;
    textcolor(LIGHTBLUE);gotoxy(8,13);printf("Numeros pares no vetor: ");
    gotoxy(35,13);
    for (f = 0; f < 8; f++){
        if (Vetor [f] % 2 == 0) par [i++] = Vetor [f];
        else
            impar [j++] = Vetor [f];
    }
    for (f = 0; f < i; f++){
        textcolor(LIGHTMAGENTA);printf(" %d ", par [f]);
    }
    Sleep(800);
    textcolor(LIGHTBLUE);gotoxy(8,15);printf ( "Esta e a soma dos Pares no Vetor: " );
    gotoxy(41,15);textcolor(LIGHTMAGENTA);Sleep(800);
    for ( f = 0; f < 8; 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,17);printf("O maior Numero par e:");
    Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(33,17);printf(" %d ", m);
    Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,19);printf("O menor Numero par e:");
    Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(33,19);printf(" %d ", g);
    getche();
}
void Imprime_Impares_Maior_Menor(){
    system("cls");
    Mostra_Elementos_Vetor();
    int f, g, i = 0, j = 0,  m, soma_impares = 0;
    Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,9);printf("Numeros Impares no vetor: ");
    gotoxy(35,9);
    for (f = 0; f < 8; f++){
        if (Vetor [f] % 2 == 0) par [i++] = Vetor [f];
        else
            impar [j++] = Vetor [f];
    }
    for (f = 0; f < 8; f++){
        textcolor(LIGHTMAGENTA);printf(" %d ", impar [f]);
    }
    Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,11);printf ( "Esta e a soma dos Impares no Vetor: " );
    Sleep(800);gotoxy(43,11);textcolor(LIGHTMAGENTA);
    for ( f = 0; f < 8; f++ ){
        soma_impares += impar [f];
    }
    printf ( "  %d ",  soma_impares);
    f = 1;
    m = impar [f];
    g = impar [f];
    for ( f = 0; f < 8; f++ ){
        if ( impar [f] > m ){
            m = impar [f];
        }
    }

    f = 0;
    if ( impar [f] < g ){
        g = impar [f];
    }
    Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,13);printf("O maior Numero impar e:");
    Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(36,13);printf(" %d ", m);
    Sleep(800);textcolor(LIGHTBLUE);gotoxy(8,15);printf("O menor Numero impar e:");
    Sleep(800);textcolor(LIGHTMAGENTA);gotoxy(36,15);printf(" %d ", g);
    Sleep(800);textcolor(LIGHTRED);gotoxy(33,23);printf ( "FIM DO PROGRAMA" );
    getche(); exit(0);
}
int main() {
    system ("title PREENCHENDO UM VETOR E OPERANDO");
    inicio();
    getche();
}

Nenhum comentário:

Postar um comentário

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