From 9b6fbb183c0f801cebb65c052dc22a9479597ecc Mon Sep 17 00:00:00 2001 From: lensferno Date: Wed, 23 Mar 2022 12:34:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BF=98=E4=BA=86=E4=B8=8A=E4=BC=A0=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test1/20220317-5.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++ test1/cadate.cpp | 117 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 test1/20220317-5.cpp create mode 100644 test1/cadate.cpp diff --git a/test1/20220317-5.cpp b/test1/20220317-5.cpp new file mode 100644 index 0000000..ac2c0cf --- /dev/null +++ b/test1/20220317-5.cpp @@ -0,0 +1,115 @@ +#include +#include + +class Date { + int year; + int month; + int day; +public: + Date(int year, int month, int day) : year(year), month(month), day(day) {} + + Date() : year(1970), month(1), day(1) {} + + Date(const Date &date) { + Date::year = date.year; + Date::month = date.month; + Date::day = date.day; + } + + int getYear() const { + return year; + } + + void setYear(int year) { + Date::year = year; + } + + int getMonth() const { + return month; + } + + void setMonth(int month) { + Date::month = month; + } + + int getDay() const { + return day; + } + + void setDay(int day) { + Date::day = day; + } + + void setDate(int year, int month, int day) { + Date::year = year; + Date::month = month; + Date::day = day; + } +}; + +class Staff { +private: + char *name; + int id; + char sex; + Date birthday; +public: + Staff(char *name, int id, char sex, const Date &birthday) : name(name), id(id), sex(sex), birthday(birthday) {} + + Staff() : name("name"), id(0), sex('M') { + + } + + Staff(const Staff &staff) { + strcpy(name, staff.name); + id = staff.id; + sex = staff.sex; + birthday = staff.birthday; + } + + char *getName() const { + return name; + } + + void setName(char *name) { + strcpy(Staff::name, name); + } + + int getId() const { + return id; + } + + void setId(int id) { + Staff::id = id; + } + + char getSex() const { + return sex; + } + + void setSex(char sex) { + Staff::sex = sex; + } + + const Date &getBirthday() const { + return birthday; + } + + void setBirthday(const Date &birthday) { + Staff::birthday = birthday; + } + + void setStaff(char *name, int id, char sex, const Date &birthday) { + strcpy(Staff::name, name); + Staff::id = id; + Staff::sex = sex; + Staff::birthday = birthday; + } + + +}; + +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} diff --git a/test1/cadate.cpp b/test1/cadate.cpp new file mode 100644 index 0000000..746df9c --- /dev/null +++ b/test1/cadate.cpp @@ -0,0 +1,117 @@ +#include + +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; + } +} \ No newline at end of file