Print Calendar

1. Zeller Formulation

   it's wonderfull. https://www.zhihu.com/question/42879877/answer/593102704

2. Calendar Print

#include <iostream>
#include <cstdbool>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

typedef unsigned int uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;

typedef int int32_t;
typedef short int16_t;

int32_t dacount[2][12] = {{31,28,31,30,31,30,31,31,30,31,30,31},
                          {31,29,31,30,31,30,31,31,30,31,30,31}};
const vector<string> weektitle = {"Sun","Mon","Tue","Wen","Thu","Fri","Sat"};

bool IsLeapYear(uint32_t y)
{
    return (0==y%400 || (0==y%4 && 0!=y%100));
}

int32_t CalcWeekDay(int32_t y = 1, int32_t m = 3, int32_t d = 1)
{
    int32_t w = (y%100 + y%100/4 - y/100*2 + y/100/4 - 1 +13*(m + 1)/5 + d);

    if (0 > w){
        w += (-1 * w / 7 + 1) * 7;
    }
    return w % 7;
}


int main(void)
{
    int32_t y, m, d, w;

    cout << "input year you want to print:";
    cin >> y;
    cout << "you want to show year " << y << endl;
    if (1 > y) return 0;

    setfill(' ');

    bool isLeap = IsLeapYear(y);
    
    for (uint32_t mon = 1; mon <= 12; mon++){
        /*print title*/
        for (int32_t i = 0; i < weektitle.size(); i++)
            cout << setfill(' ') << setw(8) << std::left<< weektitle[i];
        cout << endl;

        /*calc weekday*/
        if (3 > mon)
            w = CalcWeekDay(y-1, mon + 12);
        else
            w = CalcWeekDay(y, mon);

        for (uint32_t i = 0; i < w; i++)
            cout << setw(8) << std::internal << " ";
        for (uint32_t day = 1; day <= dacount[isLeap][mon-1]; day++){
            if (0 != w && 0 == w % 7)    cout << endl;
            cout << setw(8) << std::left << day;
            w++;
        }
        cout << endl << endl;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章