Algoritmo bisiesto
De Wikipedia, la enciclopedia libre
Un año es bisiesto si es divisible entre 4, excepto aquellos divisibles entre 100 pero no entre 400.
En programación, el algoritmo para calcular si un año es bisiesto es un algoritmo útil para la realización de calendarios.
Considérese las siguientes proposiciones o enunciados lógicos:
- p: Es divisible entre 4
- ¬q: No es divisible entre 100
- r: Es divisible entre 400
La fórmula lógica que indica si un año es bisiesto sería cuando [p y ¬q] ó [r] es verdadera.
[editar] Diagrama de flujo
[editar] Implementaciones en diversos lenguajes de programación
[editar] Método programado en Fortran90
program bisiestopr implicit none integer :: a write(*,*) "Dime un año:" read(*,*) a if (esbisiesto(a)) then write(*,*) "El año es bisiesto" else write(*,*) "El año no es bisiesto" endif contains function esbisiesto(an) logical :: esbisiesto integer :: an if (mod(an,4) == 0 .and. mod(an,100) /= 0 .or. mod(an,400)==0) then esbisiesto=.true. else esbisiesto=.false. endif end function esbisiesto end program bisiestopr
[editar] Método programado en Lisp
(defun leap-p (year) (or (and (zerop (mod year 4)) (not (zerop (mod year 100)))) (zerop (mod year 400))))
[editar] Método programado en C#
static void Main(string[] args)
{
int año;
Console.WriteLine("Ingrese año");
año = int.Parse(Console.ReadLine());
if (año % 4 == 0 && año % 100 != 0 || año % 400 == 0)
{
Console.WriteLine("El año {0} es bisiesto", año);
}
else
{
Console.WriteLine("El año {0} no es bisiesto", año);
}
Console.ReadLine();
}
[editar] Método programado en C# (alternativo con if anidados)
//---Declaro--- int año = 0; int moduloCuatro = 0; int moduloCien = 0; int moduloCuatroCien = 0; salida = ""; //--- Ingreso el año --- Console.WriteLine("Ingrese el Año:"); temp = Console.ReadLine(); año = Int32.Parse(temp); moduloCuatro = año %4; moduloCien = año %100; moduloCuatroCien = año % 400; salida = "El año " + año ; if (moduloCuatro==0) { if (moduloCien==0) { if (moduloCuatroCien == 0) { salida += " es Bisiesto"; } else { salida += " no es Bisiesto"; } } else { salida += " es Bisiesto"; } } else { salida += " no es Bisiesto"; } Console.WriteLine(salida);
[editar] Método programado en AS3
var today:Date = new Date(); function esBisiesto(year:Number):Boolean { return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); } //uso: var yearactual=String(today.getFullYear()); var annoActual=Number(yearactual); bisiesto=esBisiesto(annoActual); // respuesta: true o false
[editar] Método programado en PHP (opción 1)
function esBisiesto($year=NULL) { $year = ($year==NULL)? date('Y'):$year; return ( ($year%4 == 0 && $year%100 != 0) || $year%400 == 0 ); // devolvemos true si es bisiesto }
[editar] Método programado en PHP (opción 2)
function esBisiesto($year=NULL) { return checkdate(2, 29, ($year==NULL)? date('Y'):$year); // devolvemos true si es bisiesto }
[editar] Método programado en Delphi 5
Sea A una variable Integer = Año a analizar. Sea B una variable Booleana cuyo valor True=Bisiesto, False=No bisiesto.
If A mod 4=0 then If A mod 100=0 then If A mod 400=0 then B:=True else B:=False else B:=True else B:=False
Como todo lenguaje de programación de alto nivel, Delphi tiene una función para resolver lo anterior: IsLeapYear (A), que es una función Booleana cuyo valor True=Bisiesto, False=No bisiesto.
[editar] Método programando en C++/C
#include <iostream> bool esBisiesto(int year) { return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); } int main() { int anno; std::cout << "Año a consultar: "; std::cin >> anno; std::cout << "El año " << anno << (esBisiesto(anno) ? "" : " no") << " es bisiesto." << std::endl; }
[editar] Método programado en ASP/VB
function bisiesto(ano) if ( (ano mod 4 = 0) and (ano mod 100 <> 0) ) or (ano mod 400 = 0) then bisiesto = true 'Es bisiesto else bisiesto = false 'No es bisiesto end if end function
[editar] Método programado en PL/I
COMPROBAR_ANY_BISIESTO: PROC(ANY); DCL ANY PIC'(4)9'; DCL NUMANY BIN FIXED(15) INIT (0); DCL MODANY BIN FIXED(15) INIT (0); NUMANY = ANY; IF MOD(NUMANY,4) = 0 THEN DO; MODANY = MOD(NUMANY,400); IF MODANY = 100 | MODANY = 200 | MODANY = 300 THEN TIPANY = 0; ELSE TIPANY = 1; END; ELSE TIPANY = 0; END COMPROBAR_ANY_BISIESTO;
[editar] Método programado en Ruby
def es_bisiesto(anio);return (anio%4==0 && anio%100!=0)||(anio%400==0);end; #true si es bisiesto
[editar] Método programado en Visual FoxPro (opción 1)
FUNCTION EsBisiesto(tnAnio) RETURN (tnAnio%4 = 0 AND tnAnio%100 # 0) OR tnAnio%400 = 0 ENDFUNC
[editar] Método programado en Visual FoxPro (opción 2)
FUNCTION EsBisiesto(tnAnio) RETURN NOT EMPTY(DATE(tnAnio, 02, 29)) ENDFUNC
[editar] Método programado en Python
def esBisiesto(year): return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
Al igual que en Delphi, Python también tiene una función para averiguar si un año es bisiesto o no:
import calendar def esBisiesto(year): return calendar.isleap(year)
[editar] Método programado en Java
public boolean isLeap(int anno) { if((anno % 4 == 0 && anno % 100 != 0) || anno % 400 == 0) { return true; }else { return false; } }
[editar] Método programado en VBA, también válido para VB, asp
Public Function AnioBisiesto(ByVal ElAnio As Long) As Boolean 'Funcion para verificar si un año es bisiesto. Ju@nK 2009 www.juank.es Dim x(2) As Long x(0) = ElAnio Mod (4) ' tiene que ser 0 x(1) = ElAnio Mod (100) ' no tiene que ser 0 x(2) = ElAnio Mod (400) ' tiene que ser 0 AnioBisiesto = (x(2) = 0 Or (x(0) = 0 And x(1) <> 0)) End Function

