速求一个万年表C++程序 急 谢谢~~
发布网友
发布时间:2024-10-10 11:44
我来回答
共2个回答
热心网友
时间:2024-11-16 07:54
//万年历.c
//计算公元(后)某一天为星期几,并输出该月的日历
#include<stdio.h>
int isLeap(int year); //判断闰年
int maxDay(int year,int month); //判断某年某月最多天数
int eachYearTotal(int year,int month,int day); //判断这一天是该年的第几天
int week(int year,int month,int day); //判断某年某月某日的编号(0-6)
char *chineseWeek(int number); //将编号转化为星期(日-六)
void output(int year,int month,int day); //输出该天星期几
void display(int year,int month,int day); //输出日历
int check(int year,int month,int day); //检查有效性
void main()
{
int year,month,day,flag; //flag判断输入是否有效
char ch;
do
{
do
{
printf("\n请输入年月日: ");
scanf("%d%d%d",&year,&month,&day);
flag=check(year,month,day);
if(flag==0)
{
printf("输入无效\n");
}
}while(flag==0);
display(year,month,day);
printf("是否继续Y/y: ");
fflush(stdin);
ch=getchar();
}while(ch=='Y'||ch=='y');
}
int isLeap(int year) //判断闰年
{
if(year%4==0&&year%100!=0||year%400==0)
return 1;
else
return 0;
}
int maxDay(int year,int month) //判断某年某月最多天数
{
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeap(year))
a[1]=29;
return a[month-1];
}
int eachYearTotal(int year,int month,int day) //判断这一天是该年的第几天
{
int i,sum=0;
for(i=1;i<month;i++)
{
sum+=maxDay(year,i);
}
sum+=day;
return sum;
}
int week(int year,int month,int day) //判断某年某月某日的编号(0-6)
{
int number=(year-1)+(year-1)/4-(year-1)/100+(year-1)/400+eachYearTotal(year,month,day);
number%=7;
return number;
}
char *chineseWeek(int number) //将编号转化为星期(日-六)
{
switch(number)
{
case 0:
return "星期日";
case 1:
return "星期一";
case 2:
return "星期二";
case 3:
return "星期三";
case 4:
return "星期四";
case 5:
return "星期五";
case 6:
return "星期六";
default:
return "error";
}
}
void output(int year,int month,int day) //输出该天星期几
{
int number=week(year,month,day);
printf("\n 公元%d年%d月%d日%10s\n\n",year,month,day,chineseWeek(number));
}
void display(int year,int month,int day) //输出日历
{
int max,number,i,j=1;
max=maxDay(year,month);
number=week(year,month,1); //该年该月的1号为星期几作为起始位
output(year,month,day);
printf("%4s%4s%4s%4s%4s%4s%4s\n","日","一","二","三","四","五","六");
for(i=0;i<number;i++) //找起始位
{
printf(" ");
}
while(j<=max)
{
printf("%4d",j);
if(i%7==6) //周六换行
{
printf("\n");
}
i++;
j++;
}
printf("\n\t Made by Zhangbk");
printf("\n");
}
int check(int year,int month,int day) //检查有效性
{
if(year<0||month<1||month>12||day<1||day>maxDay(year,month))
return 0;
else
return 1;
}
热心网友
时间:2024-11-16 07:55
#include <stdio.h>
char *week[] = {"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"};
void main()
{
int Y;
int M;
int D;
int A;
printf("\nEnter year:");
scanf("%d",&Y);
printf("\nEnter month:");
scanf("%d",&M);
printf("\nEnter date:");
scanf("%d",&D);
//下面的四条语句用来计算输入日期的星期数,是程序的核心部分,缺一不可
A = Y > 0 ? (5 + (Y + 1) + (Y - 1)/4 - (Y - 1)/100 + (Y - 1)/400) % 7
: (5 + Y + Y/4 - Y/100 + Y/400) % 7;
A = M > 2 ? (A + 2*(M + 1) + 3*(M + 1)/5) % 7
: (A + 2*(M + 2) + 3*(M + 2)/5) % 7;
if (((Y%4 == 0 && Y%100 != 0) || Y%400 == 0) && M>2)
{
A = (A + 1) % 7;
}
A = (A + D) % 7;
printf("\nI's a %s.\n\n",week[A]);
}