1. 程式人生 > >剛開始寫算周幾

剛開始寫算周幾

#include<iostream>
void main()
{
	int a,b,c,i=0;
	do
	{
		std::cout<<"請輸入年份(2008~):";
		std::cin>>a;
	}
	while(a<2008);
	do
	{
		std::cout<<"請輸入年份(1~12):";
		std::cin>>b;
	}
	while(b<1 || b>12);
	if(a%4==0)
	{
		if(b==1 ||b==3 ||b==5 ||b==7 ||b==8 ||b==10 ||b==12)
		{
			do
			{
				std::cout<<"請輸入日期(1~31):";
				std::cin>>c;
			}
			while(c<1 ||c>31);
		}
		else if(b==2)
		{
			do
			{
				std::cout<<"請輸入日期(1~29):";
				std::cin>>c;
			}
			while(c<1 ||c>29);
		}
		else
		{
			do
			{
				std::cout<<"請輸入日期(1~30):";
				std::cin>>c;
			}
			while(c<1 ||c>30);
		}
	}
	else
	{
		if(b==1 ||b==3 ||b==5 ||b==7 ||b==8 ||b==10 ||b==12)
		{
			do
			{
				std::cout<<"請輸入日期(1~31):";
				std::cin>>c;
			}
			while(c<1 ||c>31);
		}
		else if(b==2)
		{
			do
			{
				std::cout<<"請輸入日期(1~28):";
				std::cin>>c;
			}
			while(c<1 ||c>28);
		}
		else
		{
			do
			{
				std::cout<<"請輸入日期(1~30):";
				std::cin>>c;
			}
			while(c<1 ||c>30);
		}
	}
	switch (b)
	{
		case 12:i+=30;
		case 11:i+=31;
		case 10:i+=30;
		case 9:i+=31;
		case 8:i+=31;
		case 7:i+=30;
		case 6:i+=31;
		case 5:i+=30;
		case 4:i+=31;
		case 3:(a%4==0?i+=29:i+=28);
		case 2:i+=31;
		case 1:i+=0;
	}
	int n=0;
	if(a%4==0)
		n=((a-2008)*365+(a-2008)/4+i+c)%7;
	else
		n=((a-2008)*365+(a-2008)/4+i+c+1)%7;
	switch(n)
	{
		case 0:std::cout<<a<<"年"<<b<<"月"<<c<<"日這天是週一";break;
		case 1:std::cout<<a<<"年"<<b<<"月"<<c<<"日這天是週二";break;
		case 2:std::cout<<a<<"年"<<b<<"月"<<c<<"日這天是週三";break;
		case 3:std::cout<<a<<"年"<<b<<"月"<<c<<"日這天是週四";break;
		case 4:std::cout<<a<<"年"<<b<<"月"<<c<<"日這天是週五";break;
		case 5:std::cout<<a<<"年"<<b<<"月"<<c<<"日這天是週六";break;
		case 6:std::cout<<a<<"年"<<b<<"月"<<c<<"日這天是週日";break;
	}
	std::cout<<std::endl;
	system("pause");
}