本帖最后由 qq575633247 于 2023-9-27 14:58 编辑
还缺少检查输入日期是否合法的代码 #include <iostream>
using namespace std;
class Time {
private:
int time;
int year;
int mon;
int day;
int mon_list[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public:
Time(int t) {
time = t;
year = t / 10000;
mon = (t % 10000) / 100;
day = t % 100;
if (leap(year)) {
mon_list[1] = 29;
cout << year << "年是闰年,2月为" << mon_list[1] << "天。"<< endl;
}else{
cout << year << "年是平年,2月为" << mon_list[1] << "天。"<< endl;
}
}
bool leap(int y) {
if (y % 400 == 0) {
return true;
} else if (y % 100 != 0 && y % 4 == 0) {
return true;
} else {
return false;
}
}
int get_year() {
return year;
}
int get_mon() {
return mon;
}
int get_day() {
return day;
}
int get_num() {
int sum = 0;
for (int i = 0; i < mon - 1; i++) {
sum += mon_list[i];
}
sum += day;
return sum;
}
void print_time() {
cout << year << "年" << mon << "月" << day << "日";
cout << "是" << year << "年" << "第" << get_num() << "天";
}
};
int main() {
int t;
cin >> t;
Time time = Time(t);
time.print_time();
return 0;
}
|