用C++编写一个具有如下功能的万年历与日历查询系统:显示某一年的全年日历(每天要有对应的星期);显示任意指定某年某月的月历(每天要有对应的星期);显示某年某月某日是星期几;显示某年某月某日是该年的第几天。已知1980年1月1日是星期二。
时间: 2023-05-25 15:03:22 浏览: 95
用C++开发的万年历,实现年月日和星期的查询
5星 · 资源好评率100%
思路:
根据1980年1月1日是星期二,我们可以求出该年每个月的第一天是星期几,以及该年的每一天是星期几。
实现时,我们可以定义一个二维数组,第一维表示月份,第二维表示天数。对于某一天,我们可以通过算出它距离1980年1月1日有多少天,然后对7取模,得到该天是星期几。
代码如下:
```c
#include <stdio.h>
// 每个月有多少天
int daysOfMon[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 1980年1月1日是星期二
int base = 2;
// 判断是否为闰年
int isLeap(int year) {
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 1;
} else {
return 0;
}
}
// 算出某一天是星期几
int getWeekday(int year, int month, int day) {
int i, days = 0;
for(i = 1980; i < year; i++) {
if(isLeap(i)) {
days += 366;
} else {
days += 365;
}
}
for(i = 1; i < month; i++) {
days += daysOfMon[i];
if(i == 2 && isLeap(year)) {
days++;
}
}
days += day - 1;
return (base + days) % 7;
}
// 显示某一年的全年日历
void showYear(int year) {
printf("================= %d ====================\n", year);
printf(" J F M A M J J A S O N D\n");
int i, j, month, days;
for(month = 1; month <= 12; month++) {
days = daysOfMon[month];
if(month == 2 && isLeap(year))
days++;
printf(" ");
for(i = 0; i < month-1; i++) {
printf(" ");
}
for(i = 1; i <= days; i++) {
printf("%2d", i);
int day = getWeekday(year, month, i);
if(day == 0) {
printf(" Su ");
} else if(day == 1) {
printf(" Mo ");
} else if(day == 2) {
printf(" Tu ");
} else if(day == 3) {
printf(" We ");
} else if(day == 4) {
printf(" Th ");
} else if(day == 5) {
printf(" Fr ");
} else if(day == 6) {
printf(" Sa ");
}
}
printf("\n");
}
}
// 显示任意指定某年某月的月历
void showMonth(int year, int month) {
printf("=============== %d.%d ===============\n", year, month);
printf(" Su Mo Tu We Th Fr Sa\n");
int i, j, days = daysOfMon[month];
if(month == 2 && isLeap(year))
days++;
for(i = 1; i <= days; i++) {
int day = getWeekday(year, month, i);
if(i == 1) {
for(j = 0; j < day; j++) {
printf(" ");
}
}
printf("%2d", i);
if(day == 0) {
printf(" Su ");
} else if(day == 1) {
printf(" Mo ");
} else if(day == 2) {
printf(" Tu ");
} else if(day == 3) {
printf(" We ");
} else if(day == 4) {
printf(" Th ");
} else if(day == 5) {
printf(" Fr ");
} else if(day == 6) {
printf(" Sa ");
}
if((day+i) % 7 == 0) {
printf("\n");
}
}
printf("\n");
}
// 显示某年某月某日是星期几
void showWeekday(int year, int month, int day) {
int weekday = getWeekday(year, month, day);
printf("%d.%d.%d is ", year, month, day);
if(weekday == 0) {
printf("Sunday\n");
} else if(weekday == 1) {
printf("Monday\n");
} else if(weekday == 2) {
printf("Tuesday\n");
} else if(weekday == 3) {
printf("Wednesday\n");
} else if(weekday == 4) {
printf("Thursday\n");
} else if(weekday == 5) {
printf("Friday\n");
} else if(weekday == 6) {
printf("Saturday\n");
}
}
// 显示某年某月某日是该年的第几天
void showDay(int year, int month, int day) {
int i, days = 0;
for(i = 1; i < month; i++) {
days += daysOfMon[i];
if(i == 2 && isLeap(year)) {
days++;
}
}
days += day;
printf("%d.%d.%d is the %dth day of %d\n", year, month, day, days, year);
}
// main函数
int main() {
int year, month, day;
printf("Please enter year(YYYY): ");
scanf("%d", &year);
printf("Please enter month(MM): ");
scanf("%d", &month);
printf("Please enter day(DD): ");
scanf("%d", &day);
showYear(year);
showMonth(year, month);
showWeekday(year, month, day);
showDay(year, month, day);
return 0;
}
```
阅读全文