📄 date.cpp
字号:
#include "Date.h"
#include <string>
//Default constructor.Initializes the private data members to default values
Date::Date(void):month(0), day(0), year(0), hour(0), minute(0), second (0){}
//Six parameter constructor.Accepts integers for the day, month, year, hour, minute, and second
Date::Date (int imonth, int iday, int iyear, int ihour, int iminute, int isecond):
month(imonth), day(iday), year(iyear), hour(ihour), minute(iminute), second(isecond){}
//Accessors and Mutators
//Set the value of the day, month, year, hour, minute, and second
void Date::setMonth(int&m){month =m;}
void Date::setDay(int&d){day=d;}
void Date::setYear(int&y){year=y;}
void Date::setHour(int&h){hour=h;}
void Date::setMinute(int&m){minute=m;}
void Date::setSecond(int&s){second=s;}
//Return the value of the day, month, year, hour, minute, and second
int Date::getMonth() const{return month;}
int Date::getDay() const{return day;}
int Date::getYear() const{return year;}
int Date::getHour() const{return hour;}
int Date::getMinute() const{return minute;}
int Date::getSecond() const{return second;}
//Compares Date objects for equality
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;
}
return false;
}
//Compares Date objects, returns true if the invoking Date object is less than the other Date object
bool Date::operator< (const Date &rhs){
int cyear,cmonth,cday,chour,cminute,csecond;
cyear = year - rhs.getYear();
cmonth = month - rhs.getMonth();
cday = day - rhs.getDay();
chour = hour - rhs.getHour();
cminute = minute - rhs.getMinute();
csecond = second - rhs.getSecond();
if(cyear==0 && cmonth==0 && cday==0 && chour==0 && cminute==0 && csecond<0)
return true;
else if(cyear==0 && cmonth==0 && cday==0 && chour==0 && cminute<0)
return true;
else if(cyear==0 && cmonth==0 && cday==0 && chour<0)
return true;
else if(cyear==0 && cmonth==0 && cday<0)
return true;
else if(cyear==0 && cmonth<0)
return true;
else if(cyear<0)
return true;
else
return false;
}
//Outputs a Date object to an output stream in the form "mm/dd/yyyy hh:nn:ss"
ostream &operator<<(ostream& out, const Date& mdy){
out<<mdy.getMonth()<<"/"<<mdy.getDay()<<"/"<<mdy.getYear()<<""
<<mdy.getHour()<<":"<<mdy.getMinute()<<":"<<mdy.getSecond();
return out;
}
//This operator reads a Date object from an input stream
istream &operator>>(istream& in, Date& mdy){
string date_string,time_string;
int month,day,year,hour,minute,second;
int posn;
if(in == cin) {
cout << "Form : mm/dd/yyyy hh:nn:ss\n";
}
in >> date_string;
in >> time_string;
posn = date_string.find_first_of ("/", 0);
month = atoi(date_string.substr(0, posn).c_str());
mdy.setMonth(month);
day = atoi(date_string.substr(posn + 1, date_string.length()).c_str());
mdy.setDay(day);
posn = date_string.find_first_of("/", posn + 1) ;
year = atoi(date_string.substr(posn + 1,date_string.length()).c_str());
mdy.setYear(year);
posn = time_string.find_first_of (":", 0);
hour = atoi(time_string.substr(0, posn).c_str());
mdy.setHour( hour);
minute = atoi(time_string.substr(posn + 1,time_string.length()).c_str());
mdy.setMinute(minute);
posn = time_string.find_first_of(":",posn +1);
second = atoi(time_string.substr(posn + 1,time_string.length()).c_str());
mdy.setSecond(second);
return in;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -