鱼C论坛

 找回密码
 立即注册
查看: 1527|回复: 7

[技术交流] 全年日历

[复制链接]
发表于 2022-7-5 23:18:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本来想用 Python 写的,但 Python 本身就有模组 calendar,所以就用 C++ 写了。
代码随便写的,有点乱,见笑了


输出结果如图:
输出结果.jpg

代码:
#include <iostream>
#include <array>
#include <iomanip>
#include <vector>

// 判断闰年
auto isLeapYear = [](int year)->bool {return (not(year % 4) and (year % 100)) or (not (year % 400)); };

using std::array, std::vector;
using std::ostream, std::operator<<;
class Calendar {
        friend ostream& operator<<(ostream&, const Calendar&);
public:
        unsigned year; // 年份
        unsigned dweek; // 年份的第一天是星期几?
        vector<array<array<int, 7>, 6>> fullCalendar; // 完整一年份日历表,共 12 个月、每个月 6 行、每行 7 天
        Calendar() {
                this->year = 1900; // 未设定年份,默认为 1900 年
                this->calculate(); // 初始化成员
        }
        Calendar(unsigned year) {
                this->year = year;
                this->calculate(); // 初始化成员
        }
        inline array<array<int, 7>, 6> calendar(int, int); // 月份历表
        inline void calculate(); // 初始化成员函数
};

// 重载输出
using std::string, std::endl;
ostream& operator<<(ostream& os, const Calendar& cld) {
        string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "Octorber", "November", "December" };
        vector<string> week{ "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
        os << std::setfill('-') << std::setw(104) << "" << endl <<  std::setfill(' ') << std::setw(52) << cld.year << " Calendar" << endl << std::setfill('-') << std::setw(104) << "" << endl;
        // 打印全月份,每 3 个月为一行
        for (int M = 1; M < 12; M += 3) {
                // 每个月共 6 行
                for (int line = 0; line < 6; ++line) {
                        if (not line) {
                                for (int m = M; m < M + 3; ++m) os << std::setfill(' ') << std::setw(28) << month[m - 1] << std::setfill(' ') << std::setw(10) << "";
                                os << endl;
                                for (int m = M; m < M + 3; ++m) os << std::setfill('-') << std::setw(28) << "" << std::setfill(' ') << std::setw(10) << "";
                                os << endl;
                                for (int m = M; m < M + 3; ++m) {
                                        for (const string& str : week) os << std::setfill(' ') << std::setw(4) << str;
                                        os << std::setfill(' ') << std::setw(10) << "";
                                }
                                os << endl;
                                for (int m = M; m < M + 3; ++m) os << std::setfill('-') << std::setw(28) << "" << std::setfill(' ') << std::setw(10) << "";
                                os << endl;
                        }
                        for (int m = M; m < M + 3; ++m) {
                                for (int day : cld.fullCalendar[m-1][line]) {
                                        if (day) os << std::setfill(' ') << std::setw(4) << day;
                                        else os << std::setfill(' ') << std::setw(4) << "";
                                }
                                os << std::setfill(' ') << std::setw(10) << "";
                        }
                        os << endl;
                }
                os << endl;
        }
        return os;
}

// 月份历表
array<array<int, 7>, 6> Calendar::calendar(int dweek, int tdays) {
        array<array<int, 7>, 6> res;
        res.fill({ {0} });
        for (int i = dweek, day = 1; day <= tdays; ++i, ++day) res[i / 7][i % 7] = day;
        return res;
}

// 初始化成员
void Calendar::calculate() {
        int days = 0;
        for (int year = 1900; year < static_cast<signed>(this->year); ++year) {
                days += isLeapYear(year) ? 366 : 365;
        }
        this->dweek = days % 7 + 1;
        for (int m = 1, tdays, d = this->dweek; m <= 12; ++m) {
                // 计算每个月的天数 tdays
                if (m == 2) tdays = isLeapYear(this->year) ? 29 : 28;
                else if (m < 8) tdays = m & 1 ? 31 : 30;
                else tdays = m & 1 ? 30 : 31;
                // 赋值每个月的历表
                (this->fullCalendar).push_back(this->calendar(d, tdays));
                // 每个月的第一天是星期几?
                d += tdays;
                d %= 7;
        }
}

using std::cout;
int main(void) {
        Calendar A(2022);
        cout << A << endl;
        return 0;
}

评分

参与人数 1荣誉 +2 鱼币 +3 贡献 +3 收起 理由
小甲鱼 + 2 + 3 + 3 鱼C有你更精彩^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-6 07:36:36 | 显示全部楼层

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-7-6 08:57:12 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-6 09:00:58 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-6 09:08:17 From FishC Mobile | 显示全部楼层
让我想起了wttr.in这个网站
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-6 09:49:40 | 显示全部楼层
观摩学习一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-6 11:05:40 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-6 11:46:47 | 显示全部楼层

输出格式很好看不乱,代码写的也很干净工整 良作
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 06:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表