写一个程序,根据年份和月份,输出这个月的日历,
平年的2月份是28天,闰年的二月份是29天。
日历是有周的概念的,几号得与周几对应上(比如23年4月1号是周六)。
首先根据用户输入的年月确定这个月有多少天,其次需要确定这个月的1号对应周几。
C/C++代码如下:
#include
// 数组用来存每个月包含的天数,这里存的是平年的天数
// 数组以0开头,月份以1开头,不想再去考虑数组与月份的对应关系,索性弃掉数组位置为0的那块空间吧
const int DAY_OF_MONTH[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 从1900年算起吧,格林威治时间,那年的1月1日恰好是周一,当然这个数可以改成1,代表公元1年
const int START_YEAR = 1900;
// 符合规范的话代码内最好不出现字面量,当然学习中的话无所谓,这里我将开始月份定义成常量了
const int START_MONTH = 1;
// 该函数根据年月,返回该月包含的天数
int getDaysOfCurrentMonth(int year, int month){
// 判断月份是否为2并且年份是否为闰年,是的话返回29,方法结束
if((month == 2) && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))){
return 29;
}
// 不是的话会执行到该处,直接返回月份数组中的天数。
return DAY_OF_MONTH[month];
}
// 该函数根据年月,返回这个月的前一天排到了周几
int getWeekOfBefore(int year, int month){
int days = 0;
// 先按年份累加天数
for(int y = START_YEAR; y < year; y++){
if((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)){
days += 366;
} else {
days += 365;
}
}
// 再按月份累加天数
for(int m = START_MONTH; m < month; m++){
days += getDaysOfCurrentMonth(year, m);
}
// 总天数与7取余返回的是最后一天占一周中的周几
return days % 7;
}
// 主函数用于测试,重点是上面的两个函数
// 该函数内写的可能有点复杂,但主要就是用来输出显示的
int main(){
while(1){
int year, month, day;
printf("请输入年:");
scanf("%d", &year); // 输入函数
printf("请输入月:");
scanf("%d", &month); // 输入函数
int daysOfCurrentMonth = getDaysOfCurrentMonth(year, month); // 获取该月有多少天
int weekOfBefore = getWeekOfBefore(year, month); // 获取该月的前一天排在了周几
printf(" Mon Tue Wed Thu Fri Sat Sun");
printf("\n-----------------------------------\n");
int formatVar = 0; // 用来控制换行,每7个单位换一次行
for(int space = 1; space <= weekOfBefore; space++){
printf(" "); // 用五个空格占位
formatVar++;
}
for(int day = 1; day <= daysOfCurrentMonth; day++){
printf("%5d", day);
formatVar++;
if(formatVar % 7 == 0 && day != daysOfCurrentMonth){
printf("\n");
}
}
printf("\n-----------------------------------\n\n");
}
}
Java代码如下:
public class Test02 {
// 数组用来存每个月包含的天数,这里存的是平年的天数
// 数组以0开头,月份以1开头,不想再去考虑数组与月份的对应关系,索性弃掉数组位置为0的那块空间吧
static final int[] DAY_OF_MONTH = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 从1900年算起吧,格林威治时间,那年的1月1日恰好是周一,当然这个数可以改成1,代表公元1年
static final int START_YEAR = 1900;
// 符合规范的话代码内最好不出现字面量,当然学习中的话无所谓,这里我将开始月份定义成常量了
static final int START_MONTH = 1;
// 该方法根据年月,返回该月包含的天数
static int getDaysOfCurrentMonth(int year, int month){
// 判断月份是否为2并且年份是否为闰年,是的话返回29,方法结束
if((month == 2) && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))){
return 29;
}
// 不是的话会执行到该处,直接返回月份数组中的天数。
return DAY_OF_MONTH[month];
}
// 该方法根据年月,返回这个月的前一天排到了周几
static int getWeekOfBefore(int year, int month){
int days = 0;
// 先按年份累加天数
for(int y = START_YEAR; y < year; y++){
if((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)){
days += 366;
} else {
days += 365;
}
}
// 再按月份累加天数
for(int m = START_MONTH; m < month; m++){
days += getDaysOfCurrentMonth(year, m);
}
// 总天数与7取余返回的是最后一天占一周中的周几
return days % 7;
}
// 主方法用于测试,重点是上面的两个函数
// 该方法内写的可能有点复杂,但主要就是用来输出显示的
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true){
int year, month, day;
System.out.print("请输入年:");
year = scanner.nextInt();
System.out.print("请输入月:");
month = scanner.nextInt();
int daysOfCurrentMonth = getDaysOfCurrentMonth(year, month); // 获取该月有多少天
int weekOfBefore = getWeekOfBefore(year, month); // 获取该月的前一天排在了周几
System.out.print(" Mon Tue Wed Thu Fri Sat Sun");
System.out.print("\n-----------------------------------\n");
int formatVar = 0; // 用来控制换行,每7个单位换一次行
for(int space = 1; space <= weekOfBefore; space++){
System.out.print(" "); // 用五个空格占位
formatVar++;
}
for(int d = 1; d <= daysOfCurrentMonth; d++){
System.out.printf("%5d", d);
formatVar++;
if(formatVar % 7 == 0 && d != daysOfCurrentMonth){
System.out.print("\n");
}
}
System.out.print("\n-----------------------------------\n\n");
}
}
}
无。