📄 date.cpp
字号:
/*Class Date models a date and time. This class contains
private data members to store the day, month,
and year of a date and the hour, minute, and second of a time.
The class declaration includes the following public members.*/
#include <iostream>
#include <string>
#include "Date.h"
//Default constructor
Date::Date():month(0),day(0),year(0),hour(0),minute(0),second(0) {}
//Six parameter constructor
Date::Date(int month, int day, int year, int hour, int minute, int second) {
this->month = month;
this->day = day;
this->year = year;
this->hour = hour;
this->minute = minute;
this->second = second;
}
//Accessors and Mutators
void Date::setMonth(int& newmonth) {
month = newmonth;
}
void Date::setDay(int& newday) {
day = newday;
}
void Date::setYear(int& newyear) {
year = newyear;
}
void Date::setHour(int& newhour) {
hour = newhour;
}
void Date::setMinute(int& newminute) {
minute = newminute;
}
void Date::setSecond(int& newsecond) {
second = newsecond;
}
int Date::getMonth(void) const {
return month;
}
int Date::getDay(void) const {
return day;
}
int Date::getYear(void) const {
return year;
}
int Date::getHour(void) const {
return hour;
}
int Date::getMinute(void) const {
return minute;
}
int Date::getSecond(void) const {
return second;
}
//operator==
bool Date::operator== (const Date &rhs) {
if ((month == rhs.month)&&(day == rhs.day)&&(year == rhs.year)
&&(hour == rhs.hour)&&(minute == rhs.minute)&&(second == rhs.second))
return true;
else
return false;
}
//operator<
bool Date::operator< (const Date &rhs) {
if(year > rhs.year)
return false;
else if(year < rhs.year)
return true;
else if(month > rhs.month)
return false;
else if(month < rhs.month)
return true;
else if(day > rhs.day)
return false;
else if(day < rhs.day)
return true;
else if(hour > rhs.hour)
return false;
else if(hour < rhs.hour)
return true;
else if(minute > rhs.minute)
return false;
else if(minute < rhs.minute)
return true;
else if(second >= rhs.second)
return false;
return true;
}
//operator<<
ostream &operator<<(ostream& os, const Date& d) {
os << d.getMonth() << "/" << d.getDay() << "/" << d.getYear()
<< " " << d.getHour() << ":" << d.getMinute() << ":" << d.getSecond();
return os;
}
//operator>>
istream &operator>>(istream& is, Date& d)
{ int posn,posn1,posn2,posn3,posn4;
int month,day,year;
int hour,minute,second;
string date_string;
getline(is, date_string);
posn = date_string.find_first_of("/", 0);
month = atoi(date_string.substr(0, posn).c_str());
d.setMonth(month);
day = atoi(date_string.substr(posn + 1,date_string.find_last_of("/", 0)).c_str());
d.setDay(day);
posn1 = date_string.find_first_of("/", posn + 1) + 1;
posn2 = date_string.find_first_of(" ", posn) + 1;
year = atoi(date_string.substr(posn1, posn2).c_str());
d.setYear(year);
posn3 = date_string.find_first_of(":", posn2 + 1);
hour = atoi(date_string.substr(posn2, posn3).c_str());
d.setHour(hour);
posn4 = date_string.find_first_of(":", posn3 + 1);
minute = atoi(date_string.substr(posn3 + 1, posn4).c_str());
d.setMinute(minute);
second = atoi(date_string.substr(posn4 + 1, date_string.length()).c_str());
d.setSecond(second);
return is;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -