日历

本文介绍了一个使用C++编写的月份日历打印程序,包括月份名称、星期名称和日期的展示,支持闰年判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


/*============================================================================
 Name        : Exercise.cpp
 Author      : Haier
 Version     : 1.01
 Copyright   : Copyright (c) 2014
 Description : Calender in C++, Ansi-style, Compile by Eclipse
 ============================================================================*/
#include <iostream>
#include <iomanip>

using namespace std;

const static char *Month[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};
const static char *Week[7] ={"Sun.","Mon.","Tue.","Wed.","Thu.","Fri.","Sat."};

void PrintHead(int Number)
{
    int i;

    cout<<"                      "<<Month[Number]<<endl;

    for(i=0; i<7; i++)
    {
        cout<<setw(8)<<left<<Week[i];
    }
    cout<<endl<<"_________________________________________________"<<endl;
}

int DaysOfMonth(int Number,int Year)
{
	if(Number==2)
	{
		if((Year%4==0 && Year%100!=0) || Year%400==0)
		{
			return 29;
		}
		return 28;
	}
	else if(Number==4 || Number==6 || Number==9 || Number==11)
	{
		return 30;
	}
	else
	{
		return 31;
	}
}

void skip( unsigned int i )
{
    while ( i > 0 )
    {
        cout << " ";
        i--;
    }
}

void PrintDays(int Number,int &n)
{
	int i;

    skip(8*n);

	for(i=1; i<=Number; i++)
	{
	    cout<<left<<i<<"\t";

		if(n==6)
		{
			cout<<endl;
			n=0;
		}
		else
        {
            n++;
        }
	}
	cout<<endl<<"_________________________________________________"<<endl;
}

unsigned int firstDayOfJanuary( unsigned int year )
{
    return ( year + ( 97 * year - 97 ) / 400 ) % 7;
}

int main()
{
    int i;
    int Year;
    int firstDayInCurrentMonth;

    cout<<"Please input a year: ";
    cin>>Year;
    firstDayInCurrentMonth=firstDayOfJanuary(Year);

    cout<<"                         "<<Year;
    cout<<endl<<"_________________________________________________"<<endl;

    for(i=1; i<=12; i++)
    {
        PrintHead(i);
        PrintDays(DaysOfMonth(i,Year),firstDayInCurrentMonth);
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爻渡

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值