# 编程求解，如内容所示题

• 写回答

#### 1条回答默认 最新

• _GX_ 2022-02-09 07:09
关注
``````#include <iostream>
#include <stdexcept>
#include <ctime>

class YearMonth
{
public:
YearMonth() {}
YearMonth(int year, int month) : _year(year), _month(month)
{
if (year < 0 || month < 1 || month > 12)
throw std::invalid_argument("invalid year or month");
}

int year() const { return _year; }
int month() const { return _month; }

bool isLeapYear() const
{
return ((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0);
}
bool isTypeA() const;
bool isTypeB() const;

YearMonth &operator++()
{
if (++_month > 12)
{
_month -= 12;
++_year;
}
return *this;
}

bool operator<=(const YearMonth &other) const
{
return _year < other._year || (_year == other._year && _month <= other._month);
}

private:
int _year = 0;
int _month = 1;
};

bool YearMonth::isTypeA() const
{
std::tm tm = {.tm_mday = 1, .tm_mon = _month - 1, .tm_year = _year - 1900};
std::time_t time = std::mktime(&tm);
const std::tm *t = std::localtime(&time);
return t->tm_wday == 0 || t->tm_wday == 1 || t->tm_wday == 6;
}

bool YearMonth::isTypeB() const
{
static int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int lastDay = days[_month - 1];
if (isLeapYear() && _month == 2)
lastDay++;
std::tm tm = {.tm_mday = lastDay, .tm_mon = _month - 1, .tm_year = _year - 1900};
std::time_t time = std::mktime(&tm);
const std::tm *t = std::localtime(&time);
return t->tm_wday == 0 || t->tm_wday == 5 || t->tm_wday == 6;
}

std::istream &operator>>(std::istream &is, YearMonth &yearMonth)
{
int year, month;
is >> year >> month;
yearMonth = YearMonth(year, month);
return is;
}

int main()
{
YearMonth start, end;
std::cin >> start >> end;
int typeA = 0, typeB = 0;
while (start <= end)
{
if (start.isTypeA())
typeA++;
if (start.isTypeB())
typeB++;
++start;
}
std::cout << "Type A: " << typeA << '\n'
<< "Type B: " << typeB << '\n';
return 0;
}
``````
``````\$ g++ -Wall main.cpp
\$ ./a.out
2006 6 2006 6
Type A: 0
Type B: 1
\$ ./a.out
2006 1 2006 6
Type A: 3
Type B: 3
``````
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论 编辑记录

• 系统已结题 2月17日
• 已采纳回答 2月9日
• 创建了问题 2月9日

#### 悬赏问题

• ¥15 WPF如何用Chart绘画出Y轴的左边数据
• ¥15 pycharm无法查看内置代码
• ¥15 跑hls xfopencv的例程standalone_hls_axi_example出的错误，csim没问题，c synthesis出的错误
• ¥15 sqlserver update语句逐行生效
• ¥15 mssql2000游标循环次数对不上
• ¥20 Windows10系统命令行调用
• ¥15 php环境如何实现国密SM2相关功能
• ¥15 关于#单片机#的问题：K210 例程里面提示我iomem.h: No such file or directory
• ¥15 LSPI算法的大问题
• ¥15 java导出word 动态填充数据