C语言入门 -- 输出某一天是星期几(2021/1/7)

C语言入门 专栏收录该内容
15 篇文章 0 订阅

输出某一天是星期几

用户输入日期判断是否有效,若有效输出星期几

输入年份
输入月份(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 <stdio.h>
#include <string.h>

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://当月份为135781012 
     		    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;
  } 
  • 3
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页

打赏

RowdyKid

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值