using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace practicadelinternet
{
class Program
{
static void Main(string[] args)
{
while (true)
{
//Pedimos la fecha al usuario
Console.WriteLine("Escribe una fecha" + '\n');
Console.Write("Dia:");
int d = int.Parse(Console.ReadLine());
if (d == 0) break;
Console.Write("Mes:");
int m = int.Parse(Console.ReadLine());
if (m == 0) break;
Console.Write("Año:");
int a = int.Parse(Console.ReadLine());
if (a == 0) break;
//Creamos un array con los dias de la semana
string[] days = { "Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado" };
//Esta es la respuesta usando el metodo DayOfWeek()
Console.WriteLine("Ese dia es: " + days[DayOfWeek(d, m, a)]);
}
}
public static int DayOfWeek(int day, int month, int year)
{
int[] mesCode = { 0, 6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
int resul = year % 100 + (year % 100) / 4 + day + mesCode[month];
if (year / 100 == 17) resul += 5;
else if (year / 100 == 18) resul += 3;
else if (year / 100 == 19) resul += 1;
else if (year / 100 == 20) resul += 0;
else if (year / 100 == 21) resul += -2;
else if (year / 100 == 12) resul += -4;
//Vemos si es bisisesto y quitamos un dia si el mes es enero o febrero
if (Esbisiesto(year) && (month == 1 || month == 2))
resul += -1;
//Este devulve un numero entre 0 y 7 que nos da el dia de la semana
return resul % 7;
}
//Metodo para saber si es año bisiesto
private static bool Esbisiesto(int a)
{
return (a % 4 == 0 && a % 100 != 0) || a % 400 == 0;
}
}
}
viernes, 27 de febrero de 2015
Dia de la semana C#
Hola bienvenidos!!!!
Suscribirse a:
Comentarios (Atom)
.jpg)