Decisión – Ejemplo 4

Enunciado

Ingresar una fecha en formato DDMMAA e informar si DD y MM son correctos.

Código

Python

def main():
    cor = " Fecha valida / Valid date "
    inc = " Fecha invalida / Invalid date "
    
    f = int( input( "Fecha/Date (DDMMAA): " ) )

    ddmm = f // 100
    aa =  f % 100
    dd = ddmm // 100
    mm = ddmm % 100
    
    print( "\n", dd, " / ", mm, " / ", aa )

    if mm==1 or mm==3 or mm==5 or mm==7 or mm==8 or mm==10 or mm==12:
        if dd>0 and dd<=31: print( cor )
        else: print( inc )
    elif mm==2:
        if aa%4==0 and aa!=0:
            if dd>0 and dd<=29: print( cor )
            else: print( inc )
        elif dd>0 and dd<=28: print( cor )
        else: print( inc )
    elif mm==4 or mm==6 or mm==9 or mm==11:
        if dd>0 and dd<=30: print( cor )
        else: print( inc )
    else: print( inc )

    input( "Presionar/Press Enter to exit " )
main()

C++

#include <iostream>
using namespace std ;
#include <conio.h>

#define cor cout << "Fecha valida / Valid date "
#define inc cout << "Fecha invalida / Invalid date "

int main()
{
   int f, aa, dd, mm, ddmm ;

   cout << "Ingresar la fecha en formato DDMMAA: " ;
   cin >> f ;

   ddmm = f / 100 ;
   aa =  f % 100 ;
   dd = ddmm / 100 ;
   mm = ddmm % 100 ;
   cout << dd << " / " << mm << " / " << aa ;

   switch ( mm )
   {
   	case  1:
   	case  3:
        case  5:
   	case  7:
   	case  8:
   	case 10:
   	case 12:
            ( dd>0 && dd<=31 )? cor : inc ;
	    break;
   	case  2:
            ( aa%4==0 && aa!=0 )?( dd>0 && dd<=29 )?
            cor : inc : ( dd>0 && dd<=28 )? cor : inc ;
            break;
   	case  4:
   	case  6:
   	case  9:
   	case 11:
            ( dd>0 && dd<=30 )? cor : inc ;
	    break;
        default: inc ;
  }
   
  cout << "Presionar/Press Enter to exit " ;
  getch();
  return 0;
}

Pascal

Program Problema7_8 ;

const
    c = 'Fecha valida / Valid date' ; 
    noc = 'Fecha invalida / Invalid date' ;

var
    f : longint ;
    aa, dd, mm, ddmm : integer ;
    test : boolean ;

begin
    write( 'Fecha/Date (DDMMAA): ' ) ;
    test := false ;
    readln( f ) ;
    
    ddmm := f div 100 ;
    aa := f mod 100 ;
    dd := ddmm div 100 ;
    mm := ddmm mod 100 ;
    
    writeln( dd ,' / ', mm ,' / ' , aa ) ;
    
    case mm of
    1, 3, 5, 7, 8, 10, 12 : if (dd<=31) and (dd>0) 
                            then test := true ;
              4, 6, 9, 11 : if (dd<=30) and (dd>0) 
                            then test := true ;
                        2 : if ((aa mod 4)=0) and 
                                (aa<>0)
                            then if (dd<=29) and (dd>0) 
                                 then test := true
                                 else if (dd<=28) and 
                                         (dd>0) 
                                      then test := true
    end ;
    
    if test then writeln( c ) else writeln( noc ) ;
    
    writeln( 'Presionar/Press Enter to exit' ) ;
    readln ;
end.

Diagramas