1. 程式人生 > >萬年歷算法

萬年歷算法

計算周 fwe else 加載 () put 承載 星期 int

        //萬年歷輸入年份月份查找當月的日歷
            #region

            Console.WriteLine("==========================Max Jiang的萬年歷=========================\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

自己沒事瞎寫的,有想法請評論。

萬年歷算法