用C实现简单的万年日历

已知:

     2010年1月1日是星期五,为了简便,我们假设每一年都为365天,请编程实现,任意输入某年某月,打印出该月的第三个星期五是多少号? 

分析:首先,我们要编码实现,每一年的第一天是星期几;再编码实现,每一年的每一个月是星期几;最后,只需调用函数便能解决问题。 

#include <stdio.h>

int getEveryStartDay(int year)//获取每一年的第一天星期几
{
	return (((year - 2010)*365 % 7 + 5) % 7);
}
int getEveryMonthDay(int year,int month)//获取某一年的某个月是星期几
{
	int monthDay[] = {31,28,31,30,31,30,31,31,30,31,30,31};
	int sum = 0;
	for(int i = 0;i < month - 1;i ++)
	{
		sum += monthDay[i];
	}
	return ((sum % 7 + getEveryStartDay(year)) % 7);
}
int main()
{	
	int y,m;

	printf("please input Year and Month:");

	scanf("%d%d",&y,&m);

	if(y < 2010 || m <= 0 || m > 12)
	{
		printf("the data you input is error.\n");
	}

	int startDay =  getEveryMonthDay(y,m);

	if(startDay <= 5)
	{
		printf("%2d\n",14 + 5 - startDay);
	}
	else
	{
		printf("%2d\n",14 + 13 - startDay);
	}
		
}

 

深入探讨:假如要分平年和闰年,那又如何解决问题呢?

判断是否为润年:

/*
 * input: year 
 * 
 * if year is a leap year,return ture
 *
 * else return false
 */
bool IsOrNotLeapYear(int year)
{
	if(year <= 0)
		return ;
	bool tag;
	if(year % 4 == 0)
	{
		if(year % 100 == 0)
		{
			if(year % 400 == 0)
			{
				tag = true;
			}
			else
			{
				tag = false;
			}
		}
		else
		{
			tag = true;
		}
	}
	else
	{
		tag = false;
	}
	return tag;
}

判断是否为素数

void IsOrNotPrimeNum(int nNum,int *dest,int *len)//int dest[100],len;IsOrNotPrimeNum(100,dest,&len);
{
	if( (nNum < 2) || (dest == NULL) || (len == NULL) )
		return ;
	bool tag;
	int nCount = 0;
	for(int i = 2;i < nNum;i ++)
	{
		tag = false ;
		for(int j = 2;j <= sqrt(i);j ++)
		{
			if(i % j == 0)
			{
				tag = true;
				break;
			}
		}
		if(tag == false)
		{
			dest[nCount ++] = i;
		}
	}
	*len = nCount;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<iostream> #include<iomanip> #include<windows.h> using namespace std; void setcolor(unsigned short color) { HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hCon,color); } int runnian(int x) { if(x%4==0) {if(x%100==0) {if(x%400==0) x=2; else x=1;} else x=2;} else x=1; return(x); } void main() { int f=1,year,n,m,t,a; cout<<"请输入年份"<<endl; cin>>year; int k[12]={31,27+runnian(year),31,30,31,30,31,31,30,31,30,31}; for(a=1,n=0;a<year;a++) n=n+runnian(a); m=(n+1)%7; for(a=0;a<=11;a++) { switch(f) { case 1:setcolor(5);cout<<setw(16)<<"一"<<endl;setcolor(3);cout<<"****************************";setcolor(7);cout<<endl;break; case 2:setcolor(5);cout<<setw(16)<<"二"<<endl;setcolor(3);cout<<"****************************";setcolor(7);cout<<endl;break; case 3:setcolor(5);cout<<setw(16)<<"三"<<endl;setcolor(3);cout<<"****************************";setcolor(7);cout<<endl;break; case 4:setcolor(5);cout<<setw(16)<<"四"<<endl;setcolor(3);cout<<"****************************";setcolor(7);cout<<endl;break; case 5:setcolor(5);cout<<setw(16)<<"五"<<endl;setcolor(3);cout<<"****************************";setcolor(7);cout<<endl;break; case 6:setcolor(5);cout<<setw(16)<<"六"<<endl;setcolor(3);cout<<"****************************";setcolor(7);cout<<endl;break; case 7:setcolor(5);cout<<setw(16)<<"七"<<endl;setcolor(3);cout<<"****************************";setcolor(7);cout<<endl;break; case 8:setcolor(5);cout<<setw(16)<<"八"<<endl;setcolor(3);cout<<"****************************";setcolor(7);cout<<endl;break; case 9:setcolor(5);cout<<setw(16)<<"九"<<endl;setcolor(3);cout<<"****************************";setcolor(7);cout<<endl;break; case 10:setcolor(5);cout<<setw(16)<<"十"<<endl;setcolor(3);cout<<"****************************";setcolor(7);cout<<endl;break; case 11:setcolor(5);cout<<setw(16)<<"十一"<<endl;setcolor(3);cout<<"****************************";setcolor(7);cout<<endl;break; case 12:setcolor(5);cout<<setw(16)<<"十二"<<endl;setcolor(3);cout<<"****************************";setcolor(7);cout<<endl;break; } setcolor(4); cout<<" Sun"; setcolor(7); cout<<" Mon Tue Wed Thu Fri"; setcolor(4); cout<<" Sat"<<endl; setcolor(7); if(f==1||f==5||f==10) {setcolor(9);cout<<setw(4*m+4)<<"1";setcolor(7);} else {if(m==0||m==6) {setcolor(4);cout<<setw(4*m+4)<<"1";setcolor(7);} else cout<<setw(4*m+4)<<"1";} if(m+1==7) cout<<endl; f++; for(t=2;t<=k[a];t++) { if(a==8&&t==10) {setcolor(9);cout<<setw(4)<<t;setcolor(7);} else {if((m+t)%7==0||(m+t)%7==1) {setcolor(4);cout<<setw(4)<<t;setcolor(7);} else cout<<setw(4)<<t;} if((m+t)%7==0) cout<<endl; } cout<<endl; setcolor(3);cout<<"****************************";setcolor(7);cout<<endl; m=(m+k[a])%7; cout<<endl<<endl; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值