鱼C论坛

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

[技术交流] 全年日历

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

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

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

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


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

代码:
  1. #include <iostream>
  2. #include <array>
  3. #include <iomanip>
  4. #include <vector>

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

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

  26. // 重载输出
  27. using std::string, std::endl;
  28. ostream& operator<<(ostream& os, const Calendar& cld) {
  29.         string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "Octorber", "November", "December" };
  30.         vector<string> week{ "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
  31.         os << std::setfill('-') << std::setw(104) << "" << endl <<  std::setfill(' ') << std::setw(52) << cld.year << " Calendar" << endl << std::setfill('-') << std::setw(104) << "" << endl;
  32.         // 打印全月份,每 3 个月为一行
  33.         for (int M = 1; M < 12; M += 3) {
  34.                 // 每个月共 6 行
  35.                 for (int line = 0; line < 6; ++line) {
  36.                         if (not line) {
  37.                                 for (int m = M; m < M + 3; ++m) os << std::setfill(' ') << std::setw(28) << month[m - 1] << std::setfill(' ') << std::setw(10) << "";
  38.                                 os << endl;
  39.                                 for (int m = M; m < M + 3; ++m) os << std::setfill('-') << std::setw(28) << "" << std::setfill(' ') << std::setw(10) << "";
  40.                                 os << endl;
  41.                                 for (int m = M; m < M + 3; ++m) {
  42.                                         for (const string& str : week) os << std::setfill(' ') << std::setw(4) << str;
  43.                                         os << std::setfill(' ') << std::setw(10) << "";
  44.                                 }
  45.                                 os << endl;
  46.                                 for (int m = M; m < M + 3; ++m) os << std::setfill('-') << std::setw(28) << "" << std::setfill(' ') << std::setw(10) << "";
  47.                                 os << endl;
  48.                         }
  49.                         for (int m = M; m < M + 3; ++m) {
  50.                                 for (int day : cld.fullCalendar[m-1][line]) {
  51.                                         if (day) os << std::setfill(' ') << std::setw(4) << day;
  52.                                         else os << std::setfill(' ') << std::setw(4) << "";
  53.                                 }
  54.                                 os << std::setfill(' ') << std::setw(10) << "";
  55.                         }
  56.                         os << endl;
  57.                 }
  58.                 os << endl;
  59.         }
  60.         return os;
  61. }

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

  69. // 初始化成员
  70. void Calendar::calculate() {
  71.         int days = 0;
  72.         for (int year = 1900; year < static_cast<signed>(this->year); ++year) {
  73.                 days += isLeapYear(year) ? 366 : 365;
  74.         }
  75.         this->dweek = days % 7 + 1;
  76.         for (int m = 1, tdays, d = this->dweek; m <= 12; ++m) {
  77.                 // 计算每个月的天数 tdays
  78.                 if (m == 2) tdays = isLeapYear(this->year) ? 29 : 28;
  79.                 else if (m < 8) tdays = m & 1 ? 31 : 30;
  80.                 else tdays = m & 1 ? 30 : 31;
  81.                 // 赋值每个月的历表
  82.                 (this->fullCalendar).push_back(this->calendar(d, tdays));
  83.                 // 每个月的第一天是星期几?
  84.                 d += tdays;
  85.                 d %= 7;
  86.         }
  87. }

  88. using std::cout;
  89. int main(void) {
  90.         Calendar A(2022);
  91.         cout << A << endl;
  92.         return 0;
  93. }
复制代码

评分

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

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-7-6 08:57:12 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-6 09:00:58 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-7-6 09:08:17 From FishC Mobile | 显示全部楼层
让我想起了wttr.in这个网站
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-6 09:49:40 | 显示全部楼层
观摩学习一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-6 11:05:40 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

输出格式很好看不乱,代码写的也很干净工整 良作
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-16 23:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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