|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本来想用 Python 写的,但 Python 本身就有模组 calendar,所以就用 C++ 写了。
代码随便写的,有点乱,见笑了
输出结果如图:
代码:- #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有你更精彩^_^ |
查看全部评分
|