0 前言
0.1 相关阅读
用C语言打印月历?看下面这个链接:
C语言实现打印月历https://blog.csdn.net/weixin_45339670/article/details/127655834
判断一个月有几天?看下面这个链接:
表驱动法的讲解https://blog.csdn.net/weixin_45339670/article/details/131620615?spm=1001.2014.3001.5501
C#程序由一些个相互调用的“方法”组成(“方法”,在其他语言中常称为“函数”)。优美的实现需要对调用关系进行精良的设计。单纯写上百行的一个方法,虽可正确实现功能,但在编码的容易程度、可读性、高内聚低耦合方面都有欠缺。而将一个功能分为太多的类,亦是画蛇添足。
所以如何划分方法,十分重要。
就思想意识方面来说,本学期,敲计导大作业前,我没有这个意识。
敲大作业中,也没这个意识。
事后写概要设计报告时,函数调用几乎没有,才发现问题所在。
1 方法的划分
打印年历,是复杂的。如果我们在里边调用12次打印月历,即可得到简化。
观察一个月历由哪些方面组成
我们细品哪些跟“打印月历”无关,只是我们需要一个数据。
最终五个方法
2 实现这五个方法
2.1 根据年月日,计算星期数
(C#给了我们现成的工具,即类DateTime,我们再套一层壳即可。若想通过日期的计算实现此方法,请阅读开篇相关阅读1)
private static int GetWeekByDay(int year, int month, int day)
{
DateTime tmp = new DateTime(year, month, day);
return (int)tmp.DayOfWeek;
}
tip:三斜线的妙用——在方法的上一行敲三个杠“///”,自动出来一个框架(自动生成此方法的文档注释标记),填写相应信息,方便调用时知道参数的含义。
键入三个斜杠,填写完整
/// <summary>
/// 根据年月日,返回是星期几(周日返回0)
/// </summary>
/// <param name="year">哪年</param>
/// <param name="month">哪月</param>
/// <param name="day">哪日</param>
/// <returns>星期几</returns>
private static int GetWeekByDay(int year, int month, int day)
{
DateTime tmp = new DateTime(year, month, day);
return (int)tmp.DayOfWeek;
}
2.2 判断闰年
/// <summary>
/// 根据年份,判断是否为闰年
/// </summary>
/// <param name="year">哪年</param>
/// <returns>bool值,是否为闰年</returns>
private static bool isLeapYear(int year)
{
if (year % 400 == 0) return true;
else if (year % 4 == 0 && year % 100 != 0) return true;
else return false;
}
2.3 计算某月总天数
参阅开篇相关阅读2-表驱动法
/// <summary>
/// 根据年月,返回该月总天数
/// </summary>
/// <param name="year">哪年</param>
/// <param name="month">哪月</param>
/// <returns>该月总天数</returns>
private static int GetTotalDaysByMonth(int year, int month)
{
int[,] d = new int[2, 12] {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
return d[Convert.ToInt32(isLeapYear(year)), month - 1];
}
2.4 打月历、打年历
/// <summary>
/// 打印月历
/// </summary>
/// <param name="year">哪年</param>
/// <param name="month">哪月</param>
private static void PrintMonthCalender(int year, int month)
{
Console.WriteLine("日\t一\t二\t三\t四\t五\t六");
int tmp = GetWeekByDay(year, month, 1);
for (int i=0; i<tmp; ++i)
{
Console.Write("\t");
}
int days = GetTotalDaysByMonth(year, month);
for (int i=1; i<=days; ++i)
{
Console.Write(i);
if ((tmp + i) % 7 == 0) Console.WriteLine();
else Console.Write("\t");
}
Console.WriteLine();
}
private static void PrintYearCalender(int year)
{
for (int i=1; i<=12; i++)
{
Console.WriteLine("{0}年{1:d2}月", year, i);
PrintMonthCalender(year, i);
}
}
3 完整代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Day01
{
class Program
{
static void Main(string[] args)
{
PrintYearCalender(2023);
}
/// <summary>
/// 根据年月日,返回是星期几(周日返回0)
/// </summary>
/// <param name="year">哪年</param>
/// <param name="month">哪月</param>
/// <param name="day">哪日</param>
/// <returns>星期几</returns>
private static int GetWeekByDay(int year, int month, int day)
{
DateTime tmp = new DateTime(year, month, day);
return (int)tmp.DayOfWeek;
}
/// <summary>
/// 根据年份,判断是否为闰年
/// </summary>
/// <param name="year">哪年</param>
/// <returns>bool值,是否为闰年</returns>
private static bool isLeapYear(int year)
{
if (year % 400 == 0) return true;
else if (year % 4 == 0 && year % 100 != 0) return true;
else return false;
}
/// <summary>
/// 根据年月,返回该月总天数
/// </summary>
/// <param name="year">哪年</param>
/// <param name="month">哪月</param>
/// <returns>该月总天数</returns>
private static int GetTotalDaysByMonth(int year, int month)
{
int[,] d = new int[2, 12] {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
return d[Convert.ToInt32(isLeapYear(year)), month - 1];
}
/// <summary>
/// 打印月历
/// </summary>
/// <param name="year">哪年</param>
/// <param name="month">哪月</param>
private static void PrintMonthCalender(int year, int month)
{
Console.WriteLine("日\t一\t二\t三\t四\t五\t六");
int tmp = GetWeekByDay(year, month, 1);
for (int i=0; i<tmp; ++i)
{
Console.Write("\t");
}
int days = GetTotalDaysByMonth(year, month);
for (int i=1; i<=days; ++i)
{
Console.Write(i);
if ((tmp + i) % 7 == 0) Console.WriteLine();
else Console.Write("\t");
}
Console.WriteLine();
}
private static void PrintYearCalender(int year)
{
for (int i=1; i<=12; i++)
{
Console.WriteLine("{0}年{1:d2}月", year, i);
PrintMonthCalender(year, i);
}
}
}
}