使用C++通过输入指定年份打印出一年日历

/*
// Author:  SSPU suming
// date:2019.12.17
*/
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;

#include <iomanip>
using std::setw;

// 定义全局变量供自定义函数处理
int year0,month0,day0,year,month,day;

// 将部分功能提取作为 API 使用
int dateAPI (int togetY, int togetM, int togetD, int type) {
    int week;
    switch (type) {
        case 1:
            // 判断是否闰年,返回指定年份总天数
            return (togetY % 400 == 0 || (togetY % 4 == 0 && togetY % 100 != 0))?366:365;
        case 2:
            // 判断月份,返回指定月份总天数
            switch (togetM) {
                case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                    return 31;
                case 4: case 6: case 9: case 11:
                    return 30;
                case 2:
                    return (dateAPI(togetY, togetM, 1, 1) == 366)?29:28;
            }
        case 3:
            // 基于 基姆拉尔森公式 修改,返回指定日期星期数
            // 这里周一至周日依次返回 1 ~ 7 ...
            if (togetM < 3) {
                togetM += 12;
                togetY -= 1;
            }
            week = (togetD + 2*togetM + 3*(togetM + 1)/5 + togetY + togetY/4 - togetY/100 + togetY/400) % 7 + 1;
            if (togetM > 12) {
                togetM -= 12;
                togetY += 1;
            }
            return week;
    }
}


int main(){
	
	year = 2019;
	month = 12;
	int a[25][22] = {0};
	int cweek = 0;
	int quarter = 0;
	//定义每年总天数 
 	int year_days = 0;
	//定义每月天数
	int month_days = 0;
	//将一年的数据填充进入 
	cout<<"请输入需要打印日历的年份(格式如:2019)"<<endl;
	cin >> year;
	for( int i=0;i<12;++i ){
		month = i+1;
		//定义变量cday用来记录当月天数
		int cday = 0; 
		//获取当前月的第一天是星期几 
		cweek = dateAPI(year, month, 1, 3);
		//获取是第几季度
		quarter = int(floor( i/3 ) + 1);
		//获取该月有多少天 
		month_days = dateAPI(year,month,1,2); 
		//获取当前月的第一天是星期几 
		cweek = dateAPI(year,month, 1, 3);
		//从第一行开始插入数据 
		for( int row=1+(quarter-1)*6;row<=24;++row ){
			
			for( int j=1+(7*(i%3));j<8+(7*(i%3));++j){
				//判断是否是第一行,若是,则小于cweek输入空值 
				if( row==1+(quarter-1)*6 && j<cweek+7*(i%3) && cday<month_days ){
					continue;
				}
				//判断是否等于第一天 的星期,相等则赋值 
				if( row==1+(quarter-1)*6 && j==cweek+7*(i%3) && cday<month_days	){
					j == cweek+7*(i%3);
				}
				//判断共有多少天,并累加赋值
				if(cday < month_days){
					//cout << "当前行数:" << row << "月份:" << i << "当前天数:" << cday << endl; 
					cday++;
					//循环赋值 
					a[row][j] = cday;
				} 
				
			}
					
		}
	}
	//打印输出 
	for( int m=1;m<25;++m ){
		
		for(int n=1;n<22;++n ){
			//指定输出格式
			//判断为每月第一行,打印星期数
		
			if( (m==1 && n==1 ) || ( m%6==1 && n==1 )){

				cout<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thr"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<setw(5)<<"Sun"<<"   ";
				cout<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thr"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<setw(5)<<"Sun"<<"   ";
				cout<<setw(5)<<"Mon"<<setw(5)<<"Tue"<<setw(5)<<"Wed"<<setw(5)<<"Thr"<<setw(5)<<"Fri"<<setw(5)<<"Sat"<<setw(5)<<"Sun"<<endl;
			}
			//输出日期 为0不输出 
			if(a[m][n]==0){
				cout << setw(5) << "";
			}
			else{
				cout << setw(5) << a[m][n];
			}
			
			if( n%7==0 ){ 
				cout << "  ";
			}
			if( n%21==0 ){
				cout << endl;
			}
		}
		if( m%6==0 ){
			cout << endl;
		}
	}
	
	return 0;
}
  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值