欢迎来到萧静默的博客

书山有路勤为径,学海无涯苦作舟。

计算周几的程序(基姆拉尔森公式)

using System;

namespace HELLO
{
    class Program
    {
        int week;
        int y, m, d;
        static void Main(string[] args)
        {
            int y, m, d;
            Console.WriteLine("请输入年月日");
            Console.WriteLine("现在请输入年份");
            y = int.Parse(Console.ReadLine());
            Console.WriteLine("现在请输入月份");
            m = int.Parse(Console.ReadLine());
            Console.WriteLine("现在请输入日期");
            d = int.Parse(Console.ReadLine());
            Console.WriteLine("您输入的年月日为" + y + m + d);
            Program program = new Program();
          
            int week1 = program.WEEKS(d,m,y);
            if (week1 == 1)
            {
                Console.WriteLine("周一");
            }
            else if (week1 == 2)
            {
                Console.WriteLine("周二");
            }
            else if (week1 == 3)
            {
                Console.WriteLine("周三");
            }
            else if (week1 == 4)
            {
                Console.WriteLine("周四");
            }
            else if (week1 == 5)
            {
                Console.WriteLine("周五");
            }
            else if (week1 == 6)
            {
                Console.WriteLine("周六");
            }
            else if (week1 == 7)
            {
                Console.WriteLine("周七");
            }
            else
            {
                Console.WriteLine("什么乱七八糟的");
            }
            int week;
            //改良版的基姆拉尔森公式在计算1月和2月就完美了
            if (m == 1 | m == 2)
            {
                m += 12;
                y--;
            }
            week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7;
            if (week == 1)
            {
                Console.WriteLine("周一");
            }
            else if (week == 2)
            {
                Console.WriteLine("周二");
            }
            else if (week == 3)
            {
                Console.WriteLine("周三");
            }
            else if (week == 4)
            {
                Console.WriteLine("周四");
            }
            else if (week == 5)
            {
                Console.WriteLine("周五");
            }
            else if (week == 6)
            {
                Console.WriteLine("周六");
            }
            else if (week == 7)
            {
                Console.WriteLine("周七");
            }
            else
            {
                Console.WriteLine("什么乱七八糟的");
            }
        }
        /// <summary>
        /// 用来输入年月日计算周几
        /// </summary>
        /// <param name="d">日期</param>
        /// <param name="m">月份</param>
        /// <param name="y">年份</param>
        /// <returns></returns>
        int WEEKS(int d, int m, int y) {
            //单独的基姆拉尔森公式在计算1月和二月有缺陷
            week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7;
            return ( week);
        }
    }

}

 

 

 

posted @ 2019-10-31 17:41  萧静默  阅读(706)  评论(2编辑  收藏  举报