忘了上传了

main
lensfrex 3 years ago
parent 876f011ee8
commit 9b6fbb183c
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 115
      test1/20220317-5.cpp
  2. 117
      test1/cadate.cpp

@ -0,0 +1,115 @@
#include <iostream>
#include <cstring>
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;
}

@ -0,0 +1,117 @@
#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;
}
}
Loading…
Cancel
Save