quinta-feira, 24 de janeiro de 2013

Programa os Doze Mêses do Ano.

Uma matriz de string e dois ifs na função void Meses();
apoiado por um else,foram suficiente para criar este simples
 programa indicado a iniciantes em C.
O segredo é a variável num, que serve de índice para a matriz
de string meses [ ] [ ]; que dentro da declaração do if, mostra
 o mês correspondente ao número digitado, já o else,
reprova números acima de doze mostrando uma mensagem de advertência.

Veja abaixo imagens do programa em execução:





Veja abaixo o código do programa:

#include <stdio.h>
#include <conio.h>
void Janela6 ( ) {
     int lin, col;
     col = 0;
     for ( lin = 2; lin <= 24; lin++ )
         for ( col = 3; col <= 78; col++ ) {
              gotoxy ( col, lin );
              textbackground ( LIGHTRED );
              printf ( " " );
         }
     for ( lin = 5; lin <= 21; lin++ )
         for ( col = 6; col <= 75; col++ ) {
              gotoxy ( col, lin );
              textbackground ( BLACK );
              printf ( " " );
         }
}
void Meses ( ) {
     int num;
     char meses [ ] [ 10 ] = { "", "Janeiro", "Fevereiro", "Marco", "Abril",
              "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro",
              "Novembro", "Dezembro" };
     do {
         system ( "cls" );
         Janela6 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 30, 7 );
         printf ( "PROGRAMA OS DOZE MÊSES" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 20, 9 );
         printf ( "Digite o número de um mês ou" );
         textcolor ( LIGHTRED );
         printf ( " 0" );
         textcolor ( LIGHTBLUE );
         printf ( " pra sair " );
         textcolor ( LIGHTRED );
         scanf ( "%d", &num );
         fflush ( stdin );
         if ( num == 0 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 35, 15 );
              printf ( "MUITO OBRIGADO" );
              getche ( );
              exit ( 0 );
         }
         if ( ( num >= 1 ) && ( num < 13 ) ) {
              textcolor ( LIGHTBLUE );
              gotoxy ( 33, 11 );
              printf ( "O mês" );
              textcolor ( LIGHTRED );
              printf ( " %d", num );
              textcolor ( LIGHTBLUE );
              printf ( " é %s", meses [ num ] );
              Sleep ( 1800 );
              textcolor ( AMARELO );
              gotoxy ( 30, 17 );
              printf ( "PRESSIONE QUALQUER TECLA" );
              getche ( );
         } else {
              textcolor ( LIGHTBLUE );
              gotoxy ( 28, 11 );
              printf ( "\aNão existe este mês no ano" );
              Sleep ( 1800 );
              textcolor ( AMARELO );
              gotoxy ( 30, 17 );
              printf ( "PRESSIONE QUALQUER TECLA" );
              getche ( );
         }
     } while ( 1 );
}
void inicio ( ) {
     system ( "title PROGRAMA OS DOZE MÊSES" );
     int i;
     do {
         system ( "cls" );
         Janela6 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 31, 7 );
         printf ( "PROGRAMA OS DOZE MÊSES" );
         textcolor ( YELLOW );
         gotoxy ( 22, 10 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( LIGHTCYAN );
         gotoxy ( 49, 10 );
         printf ( "Samuel Lima" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 23, 14 );
         printf ( "Digite" );
         textcolor ( LIGHTRED );
         printf ( " 1" );
         textcolor ( LIGHTBLUE );
         printf ( " para sair" );
         printf ( " ou" );
         textcolor ( LIGHTRED );
         printf ( " 2" );
         textcolor ( LIGHTBLUE );
         printf ( " para continuar" );
         gotoxy ( 38, 20 );
         textcolor ( LIGHTRED );
         scanf ( "%d", &i );
         fflush ( stdin );
         if ( i == 1 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 35, 20 );
              printf ( "MUITO OBRIGADO" );
              Sleep ( 1800 );
              exit ( 0 );
         }
         if ( i == 2 ) {
              Meses ( );
         } else {
              textcolor ( WHITE );
              gotoxy ( 36, 20 );
              printf ( "\aOpção errada" );
              Sleep ( 1800 );
              inicio ( );
         }
     } while ( i );
}
int main ( ) {
     inicio ( );

}

Nenhum comentário:

Postar um comentário

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