quarta-feira, 28 de janeiro de 2015

Fatorial em struct

À prática de se multiplicar um número por todos os seus
antecessores, denomina-se "fatorial".
Definindo o fatorial de 3 representa-se por 3! e lê-se 3 fatorial.

Veja abaixo o fatorial de alguns números:

3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24
5! = 5 * 4 * 3 * 2 * 1 = 120
6! = 6 * 5 * 4 * 3 * 2 * 1 = 720
7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040
8! = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 40 320
9! = 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 362 880
10! = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 3 628 800

Más estamos em linguagem c, que é nossa paixão, portanto já
aviso que o propósito deste código e mostrar passagem por cópia de valor da estrutura,
na verdade estamos passando a struct inteira como parâmetros para função.

Veja abaixo algumas imagens do programa em execução:



#include <stdio.h>
#include <conio.h>
#define TAM 3
void Janela5(){
    int lin, col;
    col = 0;
    for ( lin = 2; lin <= 24; lin++ )
        for ( col = 2; col <= 78; col++ ){
            gotoxy(col,lin);textbackground(LIGHTGRAY);printf(" ");
        }
}
int continuando ( ){
    system ("title FATORIAL EM STRUCT");
    int nr; do{system("cls");system("Color 20");Janela5();
    textcolor(LIGHTRED);gotoxy(34,3);printf("FATORIAL EM STRUCT");
    textcolor(BROWN);gotoxy(25,7);printf("Programa desenvolvido por:");
    textcolor(WHITE);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(YELLOW);gotoxy(36,16);printf ("\aopcão errada!");
    getche();
    }while ( 1 );
    return 1;
}
struct fact{
    int fat1, fat2, fat3;
};
struct fatorial{
    struct fact fatorando;
};
int Vetor_struct ( struct fatorial fator [ TAM ] ){
    int i;int cont = 1;
    int fat = 1;
    system("cls");system("Color 90");Janela5();
    textcolor(LIGHTRED);gotoxy(34,3);printf("FATORIAL EM STRUCT");
    textcolor(LIGHTBLUE);gotoxy(31,5);printf("Abaixo o vetor de struct");
    for ( i = 0; i < 3; i++ ){
        gotoxy(30, i + 7 );textcolor(LIGHTRED);
        printf("%5d %5d %5d", fator [ i ].fatorando.fat1,
                fator [ i ].fatorando.fat2,
                fator [ i ].fatorando.fat3 );
    }
    getche();
    for ( i = 0; i  < 3; i++ ){
        gotoxy( 16, i + 11 );
        while ( cont <= fator [ i ].fatorando.fat1  ){
            fat *= cont;
            cont += 1;
        }
        textcolor(LIGHTBLUE);printf("Fatorial de ");Sleep(500);
        textcolor(LIGHTRED);printf(" %d ", fator [ i ].fatorando.fat1 );Sleep(500);
        textcolor(LIGHTBLUE);printf(" Equivale a ");Sleep(500);
        textcolor(LIGHTRED);printf(" %d ", fat );Sleep(500);
    }
    for ( i = 0; i  < 3; i++ ){
        gotoxy( 16, i + 14 );
        while ( cont <= fator [ i ].fatorando.fat2  ){
            fat *= cont;
            cont += 1;
        }
         textcolor(LIGHTBLUE);printf("Fatorial de ");Sleep(500);
        textcolor(LIGHTRED);printf(" %d ", fator [ i ].fatorando.fat2 );Sleep(500);
        textcolor(LIGHTBLUE);printf(" Equivale a ");Sleep(500);
        textcolor(LIGHTRED);printf(" %d ", fat );Sleep(500);
    }
    for ( i = 0; i  < 3; i++ ){
        gotoxy( 16, i + 17 );
        while ( cont <= fator [ i ].fatorando.fat3  ){
            fat *= cont;
            cont += 1;
        }
        textcolor(LIGHTBLUE);printf("Fatorial de ");Sleep(500);
        textcolor(LIGHTRED);printf(" %d ", fator [ i ].fatorando.fat3 );Sleep(500);
        textcolor(LIGHTBLUE);printf(" Equivale a ");Sleep(500);
        textcolor(LIGHTRED);printf(" %d ", fat );
    }
    Sleep(1000);
    textcolor(LIGHTRED);gotoxy(36,23);
    printf("MUITO OBRIGADO");
    getche();
    return (0);
}
int main(){
    continuando ( );
    int i;
    struct fatorial fator [ TAM ] =
           {{1, 4, 7  },
           { 2, 5, 8  },
           { 3, 6, 9 }};
    i = Vetor_struct ( fator );
    return (0);
}

Nenhum comentário:

Postar um comentário

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