命理编程-日柱算法

根据年月算当月所有的日期

//万年历输入年份月份查找当月的日历
            #region

            Console.WriteLine("==========================万年历=========================\n");

            int[] daysArr = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

            Console.Write("请输入 1900 至 2099 年之间的年份:");
            int inputYear = Convert.ToInt32(Console.ReadLine());

            Console.Write("请输入查询的月份:");
            int inputMonth = Convert.ToInt32(Console.ReadLine());

            int yearDays = 0;
            for (int year = 1900; year <= inputYear - 1; year++)
            {
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                {
                    yearDays += 366;
                }
                else
                {
                    yearDays += 365;
                }
            }
            //Console.WriteLine(yearDays);
            int monthDays = 0;
            Console.WriteLine("---------------------第" + inputYear + "年------------第" + inputMonth + "月份--------------------\n");
            for (int month = 0; month < inputMonth - 1; month++)
            {
                if ((inputYear % 4 == 0 && inputYear % 100 != 0) || inputYear % 400 == 0)
                {
                    daysArr[1] = 29;
                    monthDays += daysArr[month];
                }
                else
                {
                    daysArr[1] = 28;
                    monthDays += daysArr[month];
                }
            }
            //Console.Write(monthDays);
            yearDays += monthDays;  //计算出输入的前一月的天数总和

            ////这是将知道的每月的第一天星期几,放在相对应的周几

            int dayOfWeek = yearDays % 7 + 1;   //通过总天数计算周数,加1为了计算下一月的第一天星期几
            //Console.WriteLine("本月第一天是星期" + dayOfWeek);
            int[,] weeks = new int[(daysArr[inputMonth - 1] / 7) + 1, 7];//二位数组,加载周


            Console.WriteLine("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日\t");
            int cols = 0;
            int m = 1;
            switch (dayOfWeek)
            {
                case 1: cols = 0; break;
                case 2: cols = 1; break;
                case 3: cols = 2; break;
                case 4: cols = 3; break;
                case 5: cols = 4; break;//通过列cols承载星期几
                case 6: cols = 5; break;
                case 7: cols = 6; break;
                default: break;
            }
            for (int row = 0; row < (daysArr[inputMonth - 1] / 7) + 1; row++)
            {
                if (cols != 0)
                {
                    for (int i = 0; i < cols; i++)
                    {
                        weeks[row, i] = 0;//如果新月份的第一天不是星期一,用空填充星期
                        Console.Write("\t");
                    }
                }
                for (; cols < 7; cols++)//通过cols给每一列赋值,也就是天数
                {
                    if (m <= daysArr[inputMonth - 1])//小于没月份的天数
                    {
                        weeks[row, cols] = m;
                        Console.Write("\0\0" + m + "\t");
                        ++m;
                    }
                }
                cols = 0;//循环给零,不能放上面,因为就不能循环为零
                Console.WriteLine();
            }

            #endregion

当月所有日期日历算法

你可能感兴趣的:(命理编程-日柱算法)