UIT2024_Calendar/stu2024/数科2401谢诗涵.c
2024-12-07 16:23:34 +08:00

151 lines
3.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
int main() {
int choice;
int j,k,l,m,n;
printf("菜单选项:\n");
printf("1.查询对应年份的日历\n");
printf("2.查询对应日期的星期\n");
printf("3.查询对应年份是否为闰年\n");
printf("4.退出\n");
printf("请输入你的选择(1-4)");
scanf("%d", &choice);
start:
switch (choice) {
case 1:
printf("请输入需要查询的年份\n");
int year;
scanf("%d", &year);
int month,day;
int i,k;
// 单列输出日历
printf("单列日历输出如下:\n");
for (month = 1; month <= 12; month++) {
// 每个月的天数数组先按非闰年设置后续根据是否闰年调整2月天数
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) {
daysInMonth[1] = 29;
}
// 计算该月1号是星期几蔡氏公式简化版思路
int y = year % 100;
int c = year / 100;
if (month < 3) {
y -= 1;
c -= 1;
}
int firstDay = (1 + (13 * (month + 1) / 5) + y + (y / 4) + (c / 4) - 2 * c) % 7;
firstDay = firstDay < 0? firstDay + 7 : firstDay;
// 输出月份标题
printf(" %d年%d月\n", year, month);
printf("日 一 二 三 四 五 六\n");
// 输出该月1号前的空格
for (i = 0; i < firstDay; i++) {
printf(" ");
}
// 输出该月每天的日期
int numDays = daysInMonth[month - 1];
for (day = 1; day <= numDays; day++) {
printf("%2d ", day);
if ((firstDay + day) % 7 == 0) {
printf("\n");
}
}
// 如果该月最后一行不满7个补充换行
if ((firstDay + numDays) % 7!= 0) {
printf("\n");
}
printf("\n");
}
break;
case 2:
printf("请输入需要查询的日期\n");
{
printf("请输入该查询的年份:\n");
scanf("%d",&year);
printf("请输入该查询的月份:\n");
scanf("%d",&month);
printf("请输入该查询的日期: \n");
scanf("%d",&day);
if(month<3)
{
year--;
month+=12;
}
k=year%100;j=year/100;l=day;m=month;
n=(l+(26*(m+1)/10)+k+(k/4)+(j/4)+5*j)%7;
switch(n)
{
case 0:
printf("星期六");
break;
case 1:
printf("星期日");
break;
case 2:
printf("星期一");
break;
case 3:
printf("星期二");
break;
case 4:
printf("星期三");
break;
case 5:
printf("星期四");
break;
case 6:
printf("星期五");
break;
}
break;
}
case 3:
printf("请输入需要查询的年份\n");
{
int year;
printf("请输入一个年份:");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100!= 0) || year % 400 == 0) {
printf("%d年是闰年\n", year);
} else {
printf("%d年不是闰年\n", year);
}
}
break;
case 4:
printf("正在退出程序...\n");
break;
default:
printf("无效的选择,请重新输入(1~4)\n");
scanf("%d",&choice);
if(choice<=4)
{
goto start;
}
}
return 0;
}