万年历源码

// 万年历.cpp : Defines the entry point for the console application.
// 显示1900年以后任何年份的日历,日历以月份顺序排列,每月以星期顺序排列。
//采用一般日历计算方法,先给出一般年份的每月天数,如果是闰年,第二个月天数为29,再计算出制定年份的1月1日是星期几,然后根据这些数据
//计算出全年日历,边计算边显示。
//函数leap()判定是否闰年
//函数week()计算year年份的1月1日是星期几。计算方法是,已知1900年1月1日是星期一,先计算从1900年到year年份的前一年一共有多少(s)天,
//则(s+1)%7为年份year的1月1日的星期号。
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
static char *week0="  SUN  MON  TUE  WED  SUR  FRI  SAT";         //指针形式定义一个字符串,或者是week0[36]
int leap(int year)
{
	if((year%4==0 && year%100!=0)|| year%400==0)
		return 1;
	else return 0;
}

int week(int year)
{
	int i,w;
	int s=0;
	for(i=1900;i<year;i++)
	{
		if(leap(i))
			s+=366;
		else s+=365;                         //计算从1900年到year年的天数
	}
	w=(s+1)%7;
	return w;
}

void main()
{
	int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	int i,j,k;
	int year;
	int w;
	do
	{
		cout<<"输入年份:";
		cin>>year;
	}while(year<=1900);
	w=week(year);
	if(leap(year))
		month[1]=29;
	for(i=0;i<12;i++)
	{
		for(k=0;k<35;k++)
			cout<<"_";                       //输出一条横线
		cout<<endl;
		cout<<setw(20)<<i+1<<"月"<<endl;
		cout<<endl;
		cout<<week0<<endl;
		for(k=0;k<w;k++)
			cout<<"     ";                   //输出若干空格
		for(j=1;j<=month[i];j++)
		{
			if((j+w)%7==1)                   //遇到星期日时,换一行输出
				cout<<endl;
			cout<<setw(5)<<j;
		}
		cout<<endl;
		for(k=0;k<35;k++)
			cout<<"_";                       //输出一条横线
		cout<<endl;
		w=(w+month[i])%7;                   //计算下个月1号是星期几
	}
	cout<<endl;
	system("pause");
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值