输入年份
输入月份(1表示一月,2表示二月,等等):
您的程序应该接受并存储一个名为year的变量以响应第一个提示,并在名为month的变量中接受并存储一个数字,以响应第二个提示。
如果有效,将输出星期几。
如果输入的月份无效,程序应通知用户输入的月份无效。
【提示:
(1) 输入的月份应介于1到12之间;
(2) 一月、三月、五月、七月、八月、十月和十二月有31天;其他月份都有30天。
(3) 二月在非闰年有28天,闰年有29天。
(4) 要判断闰年,可以使用布尔表达式
(年份%4 == 0和年份%100 != 0)| |(年%400 ==0)]
/*
Name:programme3.c
Author:祁麟
Copyright:BJTU | school of software
Date:2020/10/16
Description:Based on the assignment 2. , input the year,
month, and day to determine whether the input is a valid
date. If valid, the day of the week will be output.
*/
#include
#include
int main (){
int year,month,day;
int iWeekDay = -1;
char Weekday[10];
printf("\n请输入日期:\n格式为:xxxx xx xx\n若要退出,请输入0 0 0\n\n");
while (1){
scanf("%d %d %d",&year,&month,&day);
if (year==0&&month==0&&day==0) break;
if (year<0){
printf ("您输入的日期有误,请重新输入\n");
}
switch (month) {
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12://当月份为1,3,5,7,8,10,12
if (day<1 || day>31){
//先判断是否为正确日期
printf ("您输入的日期有误,请重新输入\n");
}
else{
if (1 == month || 2 == month) {
month += 12;
year--;// 把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
}
iWeekDay = (day + 1 + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;// 利用基姆拉尔森计算日期公式 w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)
switch(iWeekDay){
case 0 :
strcpy(Weekday, "Sunday");
break;
case 1 :
strcpy(Weekday, "Monday");
break;
case 2 :
strcpy(Weekday, "Tuesday");
break;
case 3 :
strcpy(Weekday, "Wednesday");
break;
case 4 :
strcpy(Weekday, "Thursday");
break;
case 5 :
strcpy(Weekday, "Friday");
break;
case 6 :
strcpy(Weekday, "Saturday");
break;
default :
break;
}
printf ("weekday:%s\n",Weekday);
printf("\n请输入日期:\n格式为:xxxx xx xx\n");
}
break;
case 2://当月份为2月时
if ( year%4==0 && year%100!=0 || year%400==0 ){
//先判断是否为闰年
if (day<1 || day>29){
//判断日期是否正确
printf ("您输入的日期有误,请重新输入\n");
}
else {
//若日期正确,进行后续计算
if (1==month || 2==month){
month += 12;
year--;
}
iWeekDay = (day + 1 + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
switch(iWeekDay){
case 0 :
strcpy(Weekday, "Sunday");
break;
case 1 :
strcpy(Weekday, "Monday");
break;
case 2 :
strcpy(Weekday, "Tuesday");
break;
case 3 :
strcpy(Weekday, "Wednesday");
break;
case 4 :
strcpy(Weekday, "Thursday");
break;
case 5 :
strcpy(Weekday, "Friday");
break;
case 6 :
strcpy(Weekday, "Saturday");
break;
default :
break;
}
printf ("weekday:%s\n",Weekday);
printf("\n请输入日期:\n格式为:xxxx xx xx\n");
}
}
else{
//不是闰年的情况
if (day<1 || day>28){
//判断日期是否有误
printf ("您输入的日期有误,请重新输入\n");
}
else {
//若日期无误,进行后续计算
if (1 == month || 2 == month) {
month += 12;
year--;
}
iWeekDay = (day +1+ 2*month + 3*(month+1)/5 + year + year/4 - year/100 + year/400)%7;
switch(iWeekDay){
case 0 :
strcpy(Weekday, "Sunday");
break;
case 1 :
strcpy(Weekday, "Monday");
break;
case 2 :
strcpy(Weekday, "Tuesday");
break;
case 3 :
strcpy(Weekday, "Wednesday");
break;
case 4 :
strcpy(Weekday, "Thursday");
break;
case 5 :
strcpy(Weekday, "Friday");
break;
case 6 :
strcpy(Weekday, "Saturday");
break;
default :
break;
}
printf ("weekday:%s\n",Weekday);
printf("\n请输入日期:\n格式为:xxxx xx xx\n");
}
}
break;
case 4 :
case 6 :
case 9 :
case 11: //当月份为4,6,9,11时
if (day<1 || day>30){
//判断日期是否有误
printf ("您输入的日期有误,请重新输入\n");
}
else {
//若日期无误,进行后续计算
if (1 == month || 2 == month) {
month += 12;
year--;
}
iWeekDay = (day +1+ 2*month + 3*(month+1)/5 + year + year/4 - year/100 + year/400)%7;
switch(iWeekDay){
case 0 :
strcpy(Weekday, "Sunday");
break;
case 1 :
strcpy(Weekday, "Monday");
break;
case 2 :
strcpy(Weekday, "Tuesday");
break;
case 3 :
strcpy(Weekday, "Wednesday");
break;
case 4 :
strcpy(Weekday, "Thursday");
break;
case 5 :
strcpy(Weekday, "Friday");
break;
case 6 :
strcpy(Weekday, "Saturday");
break;
default :
break;
}
printf ("weekday:%s\n",Weekday);
}
break;
default://月份不是1~12
printf ("您输入的日期有误,请重新输入");
printf("\n请输入日期:\n格式为:xxxx xx xx\n");
}
}
return 0;
}