c/c++课的作业合集,都是很简单的文件。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
cpp-work/test1/cadate.cpp

117 lines
2.6 KiB

#include <iostream>
using namespace std;
// 关于::的使用情况,我还是先去仔细看看Google的c++规范吧
class Date {
public:
Date();
Date(int year, int month, int day);
Date(const Date &date);
~Date();
bool operator==(const Date &date) const;
bool operator!=(const Date &date) const;
void printDate();
void setToNextDay();
void setDate(int year, int month, int day);
private:
int year = 1970;
int month = 1;
int day = 1;
int getMaxDayOfMonth() const;
};
Date::Date(int year, int month, int day) : year(year), month(month), day(day) {}
Date::Date() : year(1900), month(1), day(1) {}
Date::Date(const Date &date) {
Date::year = date.year;
Date::month = date.month;
Date::day = date.day;
}
Date::~Date() {
std::cout << "Don'..........t.........go.........away.......... (weakly)" << std::endl;
}
bool Date::operator==(const Date &date) const {
return year == date.year &&
month == date.month &&
day == date.day;
}
bool Date::operator!=(const Date &date) const {
return !(year == date.year &&
month == date.month &&
day == date.day);
}
void Date::printDate() {
std::cout << "--------------------------" << endl;
std::cout << "- year: " << year << std::endl
<< "- month: " << month << std::endl
<< "- day: " << day << std::endl;
std::cout << "--------------------------" << endl;
}
void Date::setToNextDay() {
Date::day++;
// 检查一下是不是到了下一个月
if (Date::day > getMaxDayOfMonth()) {
Date::day = 1;
Date::month++;
// 再检查一下是不是到了下一年
if (Date::month > 12) {
Date::month = 1;
Date::year++;
}
}
}
void Date::setDate(int year, int month, int day) {
Date::year = year;
Date::month = month;
Date::day = day;
}
int Date::getMaxDayOfMonth() const {
switch (Date::month) {
default:
return 0;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (Date::year % 4 == 0) {
if (Date::year % 100 == 0) {
return (Date::year % 400) == 0 ? 29 : 28;
} else {
return 29;
}
}
return 28;
}
}