Advertisement
lukhavi

FechaCorrecta

Sep 23rd, 2019
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.33 KB | None | 0 0
  1. #include<stdio.h>
  2. //#include<conio.h> //getch()
  3.  
  4. /* Ingresa fecha: mes, dia y anio, y retorna la fecha si es valida.
  5. */
  6.  
  7. #define true 1
  8. #define false 0
  9. #define FEBRERO 2
  10. //#define esAnioBisiesto(x) ((x%4 == 0)&&( x%100!=0||x%400==0))
  11.  
  12. unsigned short int digitaNumeroDeMes(void);
  13. unsigned short int digitaNumeroDeDia(unsigned short int mes);
  14. unsigned long int digitaAnio(unsigned short int dia, unsigned short int mes);
  15. unsigned short int esAnioBisiesto(unsigned long int anio);
  16. void limpiaBufferIntermedio(void);
  17. void mainLoop(void);
  18.  
  19. /*Abril, Junio, Septiembre y Noviembre. = 30 dias
  20. Enero, Marzo, Mayo, Julio, Agosto, Octubre y Diciembre = 31 dias
  21. Febrero = 28 dias excepto anio bisiesto que son 29 dias
  22.  */
  23.  
  24.  /*Casos
  25.     1.- mes == Febrero y dias mayor a 28
  26.     2.- mes = Abril, Junio, Septiembre y Noviembre && dia mayor a 30
  27.     3.- Un anio es bisiesto si cumple:
  28.         divisible por 4 y (no es divisible por 100 v si es divisible por 400)
  29.     4.- Corregir la adquisicion de datos, da muchos errores, dejar de usar scanf
  30.         y utilizar getchar() o fgets() para formar un string de tamanio fijo con la linea ingresada
  31.         luego parsear los datos de esa linea ingresada y obtener los caracteres numericos
  32.         y descartar cualquier otro caracter, luego convertir a un tipo de datos entero.
  33.         De esta forma tambien nos olvidamos del buffer intermedio.
  34.  */
  35.  
  36. struct MonthDay{
  37.     unsigned char *mes;
  38.     unsigned short int dias;
  39.     };
  40.  
  41. //typedef MonthDay * PMonthDay;
  42.  
  43. struct MonthDay mesPorDias[]={  {"Enero",31},           //0
  44.                                 {"Febrero",29},         // solo cada cuatro años
  45.                                 {"Marzo",31},           //2
  46.                                 {"Abril",30},
  47.                                 {"Mayo",31},            //4
  48.                                 {"Junio",30},  
  49.                                 {"Julio",31},           //6
  50.                                 {"Agosto",31},          //7
  51.                                 {"Septiembre",30},
  52.                                 {"Octubre",31},         //9
  53.                                 {"Noviembre",30},
  54.                                 {"Diciembre",31}        //11
  55.                         };     
  56. //#define N_MESES sizeof(mesPordias)/sizeof(mesPorDias[0])
  57.  
  58. //PMonthDay p = &mesPorDias[0];
  59.  
  60. /*enum Mes {ENERO = 1, FEBRERO, MARZO, ABRIL, MAYO, JUNIO,JULIO,AGOSTO,SEPTIEMBRE,
  61.     OCTUBRE,NOVIEMBRE, DICIEMBRE};*/
  62.  
  63. int main (int argc, char* argv[]){
  64.    
  65.     mainLoop();
  66.    
  67.     return 0;
  68. }
  69.  
  70. void mainLoop(void){
  71.     unsigned char seguirLoop;
  72.     unsigned short int mes,dia;
  73.  
  74.     do{
  75.         dia = digitaNumeroDeDia(mes=digitaNumeroDeMes());
  76.         printf("\nLa fecha indicada es: %hu de %s del %lu\n",dia,mesPorDias[mes-1].mes/*(p+mes-1)->mes*/,digitaAnio(dia, mes));
  77.         printf("\n%c Quieres continuar (Y/N)?",168);
  78.         limpiaBufferIntermedio();
  79.         scanf("%c",&seguirLoop);
  80.         system("cls");
  81.     }while( seguirLoop == 'y' || seguirLoop == 'Y' );
  82.    
  83.     system("pause");
  84. }
  85.  
  86. // entre 1 y 12 son los meses
  87. unsigned short int digitaNumeroDeMes(void){
  88.     unsigned short int seguirMes;
  89.     unsigned short int  mes;
  90.    
  91.     printf("\n");
  92.         do{
  93.             printf("Digite el numero del mes que desea ver: ");
  94.             scanf("%hu", &mes);
  95.             seguirMes = false;
  96.             if(mes<1 || mes>12){
  97.                 printf("\n Error: El numero del mes ingresado no es valido, vuelva a intentarlo !!!\n\n");
  98.                 seguirMes = true;
  99.             }
  100.         }while(seguirMes);
  101.        
  102.         return mes;
  103. }
  104. // dias entre 1 y 31 maximo dependiendo del mes.
  105. unsigned short int digitaNumeroDeDia(unsigned short int mes){
  106.     unsigned short int seguirDia;
  107.     unsigned short int dia;
  108.     do{
  109.             printf("\nDigite el dia del respectivo mes: ");
  110.             scanf("%hu", &dia);
  111.             seguirDia = false;
  112.             if(dia<1 || dia>mesPorDias[mes-1].dias){    //  dia inexistente
  113.                 printf("\n Error: El dia %hu indicado no es valido, vuelva a intentarlo !!!\n",dia);
  114.                 seguirDia = true;
  115.             }
  116.         }while(seguirDia);
  117.        
  118.         return dia;
  119. }
  120.  
  121. unsigned long int digitaAnio(unsigned short int dias, unsigned short int mes){
  122.     unsigned short int seguirAnio;
  123.     unsigned long int anio;
  124.         do{
  125.             printf("\nDigite el anio del respectivo mes: ");
  126.             scanf("%ul", &anio);
  127.             seguirAnio = false;
  128.             if((mes == FEBRERO && dias == 29) && !esAnioBisiesto(anio)){
  129.                 printf("\n Error: Ingrese anio bisiesto, vuelva a intentarlo !!!\n");
  130.                 seguirAnio = true;
  131.             }
  132.             if(anio<0)
  133.                 printf("\n Error:El numero del anio ingresado no es valido, vuelva a intentarlo !!!\n");
  134.             }while(anio<0 || seguirAnio);
  135.         return anio;
  136. }
  137.  
  138. unsigned short int esAnioBisiesto(unsigned long int anio){
  139.     return ((anio%4 == 0)&&( anio%100!=0||anio%400==0)); // retorna si anio es bisiesto o no
  140. }
  141.  
  142. void limpiaBufferIntermedio(void){
  143.     char c;
  144.     while((c=getchar())!='\n'&& c!=EOF);
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement