UIT2024_Calendar/stu2024/软件2401范慈惠.cpp
2024-12-07 16:23:34 +08:00

196 lines
4.1 KiB
C++
Raw Permalink 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>
#include<time.h>
#include<stdbool.h>
void Menu1()
{
printf("请输入一个数字来表示你要进行的操作:\n");
printf("1.让我看看这一年的日历(单行)\n");
printf("2.让我看看这一年的日历(双行)\n");
printf("3.让我看看这天是周几\n");
printf("4.让我看看这一年是不是闰年\n");
}//构建菜单
int main() {
int menu, year;
Menu1(); // 显示菜单
scanf("%d", &menu);
if (menu == 1) {
printf("让我看看这一年的日历(单行)\n");
printf("请输入年份:");
scanf("%d", &year);
// 清除输入缓冲区中的换行符
while (getchar() != '\n');
if (year <= 0) {
printf("您输入的年份有误!\n");
return 1; // 使用返回值表示错误
}
}
int runnian(int n)//记录闰年月份日期
{
switch(n)
{
case 1:return 31;
case 2:return 29;
case 3:return 31;
case 4:return 30;
case 5:return 31;
case 6:return 30;
case 7:return 31;
case 8:return 31;
case 9:return 30;
case 10:return 31; }
}
int pingnian(int year,int month)//记录平年月份日期
{
if (year==1582&&month==10)
{
return 21;//1582年10月只有21天
}
switch(month)
{
case 1:return 31;
case 2:return 28;
case 3:return 31;
case 4:return 30;
case 5:return 31;
case 6:return 30;
case 7:return 31;
case 8:return 31;
case 9:return 30;
case 10:return 31;
case 11:return 30;
case 12:return 31;
}
}
void run(int week)//闰年日历输出
{
int n=0;//记录换行次数
int w=week;
int i,j,k;
for(i=1;i<=12;i++)
{
printf("%d月\n",i);
printf("日 一 二 三 四 五 六\n") ;
for(k=0;k<w;k++)//空格对齐
{
printf(" ");
n++;
}
for(j=1;j<=runnian(i);j++){//输出每月的天
if(n%7==0){
printf("\n");
}
printf("%d ",j);
n++;
}
w=(w+runnian(i))%7;//周数
n=n-w;
printf("\n\n");
}
}
void ping(int year)//平年日历输出
{
int n=0;//记录换行
int w=getFirstDayOfYear(year);//获取该年第一天是星期几
int i,j,k;
for(i=1;i<=12;i++)
{
printf("%d月\n", i);
printf("Sun Mon Tue Wed Thu Fri Sat\n");
for (k=0;k<w;k++)
{
printf(" ");
n++;
}
for (j=1;j<=pingnian(year,i);j++)
{//输出每月的天
if(n%7==0) // 七天换行
{
printf("\n");
}
printf("%d ",j);
n++;
}
w=(w+pingnian(year,i))%7;
n=n-w;
printf("\n\n");
}
}
// 获取某个月的天数
int getDaysInMonth(int year, int month) {
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
return 31;
case 4: case 6: case 9: case 11:
return 30;
case 2:
return isLeapYear(year) ? 29 : 28;
default:
return 0; // 不会发生,只是为了完整性
}
}
int main() {
int year;
printf("Enter the year: ");
scanf("%d", &year);
printCalendar(year);
}
if(menu==3)
{
int weekday=week(year,month,day);
else if(menu==3)
{
int year,month,day;
{printf("查询某年某月某日的日期是周几\n");
scanf("%d %d %d",&year,&month,&day);
if(month<3)
{
year--;
month+=12;
printf("%d",year);//一年中前两个月较为特殊归入前一年内计算1月—— 13月2 月—— 14月
}
else
scanf("%d %d %d",&year,&month,&day);
do
{
int y,c;
int week;
y=year/100;
c=year%100;
if(year<1582&&month<10&&day<4)
week=(day+1+2*month+3*(month+1)/5+y+y/4+5) % 7;
else
week=(day+1+2*month+3*(month+1)/5+y+y/4-y/100+y/400)%7;//利用蔡乐公式计算某年某月某日的日期是周几
do{
switch(week)
{
case 1:printf("Monday");break;
case 2:printf("Tuesday");break;
case 3:printf("Wednesday");break;
case 4:printf("Thursday");break;
case 5:printf("Friday");break;
case 6:printf("Saturday");break;
case 7:printf("Sunday");break;
default:break;
}
}while(week>0&&week<8);//利用循环得到某年某月某日的日期是周几
}
if (menu == 4) {
printf("4.让我看看这一年是不是闰年\n");
{
int year,month,day;
printf("请输入一个年份,判断该年是否为闰年");
if((year%4==0&&year%100!=0)||(year%400==0))//判断是闰年还是平年
{printf("是闰年");}
else
{printf("不是闰年");}
}
return 0;
}