今天是星期几?小程序实现。。

直接贴代码,,有更好的方法谢谢指出。宏定义也没用到了,先贴着

#include <stdio.h>

#define YEAR 2011
#define MONTH 4
#define DAY  11

struct DATE{
	int year;
	int month;
	int day;

};
bool isLeap(int year){//是否是闰年

	if (year%4!=0)return false; 
	else  if (year%100==0){    
	if(year%400==0)return true;   
	else return false;   
	}   
	else return true; 
}
bool beforeOrNot(int year,int month,int day){//比20110411要早还是晚
	if((year*10000+month*100+day-20110411)>=0)return true;
	return false;

}

int countDay(int year,int month,int day){//求解今天是星期几
	//2011年4月11日是星期一
	int count=day;
	int i=0;
	int yearCount=0;
	int temp=0;
	switch(month-1){
		case 12:
		case 11:count+=30;
		case 10:count+=31;
		case 9:count+=30;
		case 8:count+=31;
		case 7:count+=31;
		case 6:count+=30;
		case 5:count+=31;
		case 4:count+=30;
		case 3:count+=31;
		case 2:count+=29;
		case 1:count+=31;break;
		default:break;
	}



	if(beforeOrNot(year,month,day)){
		//> count
		//不是闰年
		if(!isLeap(year)){
			count--;		
		}
		yearCount=year-2011;
		i =0;
		for(i=1;i<=yearCount;i++){
			if(isLeap(year-i)){
				count+=366;
			}else{count+=365;}
		}
		return (count-101)%7+1;
		
		
	}else{
		if(!isLeap(year)){
			count--;		
		}
		yearCount = 2011-year;
		temp = count;
		count =0;
		for(i=0;i<yearCount;i++){
			if(isLeap(year+i)){
				count+=366;
			}else{count+=365;}
		}
		if(count==0){
			count = 101-temp;
		}else{
			count-=temp;
			count+=101;
		}
		return 7-(count%7)+1;
	}
	
}

bool inputTest(DATE date){//验证非法输入
	
	switch(date.month){
		case 12:if(date.day>31 ||date.day<1)return false;break;
		case 11:if(date.day>30 ||date.day<1)return false;break;
		case 10:if(date.day>31 ||date.day<1)return false;break;
		case 9:if(date.day>30 ||date.day<1)return false;break;
		case 8:if(date.day>31 ||date.day<1)return false;break;
		case 7:if(date.day>31 ||date.day<1)return false;break;
		case 6:if(date.day>30 ||date.day<1)return false;break;
		case 5:if(date.day>31 ||date.day<1)return false;break;
		case 4:if(date.day>30 ||date.day<1)return false;break;
		case 3:if(date.day>31 ||date.day<1)return false;break;
		case 2:if(date.day>29 ||date.day<1)return false;
				if(!isLeap(date.year) && date.day==29)return false;break;

		case 1:if(date.day>31 ||date.day<1)return false;break;
		default:printf("input month error!"); return false;
	}

	return true;


}

void main() {  
DATE data; 
printf("Input the year:\n"); 
scanf("%d",&(data.year)); 
printf("Input the month:\n"); 
scanf("%d",&(data.month)); 
printf("Input the day:\n"); 
scanf("%d",&(data.day)); 
if(!inputTest(data)){
	printf("input error!");
	return;
}

printf("星期%d\n",countDay(data.year,data.month,data.day));

}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值