📄 cdate.cpp
字号:
//a date class method definitions
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cdate.h"
//only set values if they are valid
CDate::CDate(int d, int m, int y) {
bool valid = true;
if(y == 0)
valid = false;
else if((m < January) ||
(m > December))
valid = false;
else if((d < 0) ||
(d > daysInMonth(m)))
valid = false;
if(valid) {
year = y;
month = m;
day = d;
}
else {
year = 1970;
month = January;
day = 1;
}
}
//destructor
CDate::~CDate() { //invalid values signal a dead object
day = month = year = 0; //there was no year 0
}
//copy constructor
CDate::CDate(const CDate& rhs) {
assert(rhs.isAlive()); //check for dead rhs object
if(this != &rhs) { //check for self
year = rhs.year;
month = rhs.month;
day = rhs.day;
}
}
//assigment operator
CDate& CDate::operator=(const CDate& rhs) {
assert(isAlive());
assert(rhs.isAlive()); //check for dead rhs object
if(this != &rhs) { //check for self
year = rhs.year;
month = rhs.month;
day = rhs.day;
}
return(*this);
}
//pre increment
CDate CDate::operator++(void) {
assert(isAlive());
//do increment
day++;
if(day > daysInMonth(month)) {
month++;
day = 1;
}
if(month > December) {
month = January;
year++;
}
return(*this);
}
//post increment
CDate CDate::operator++(int) {
assert(isAlive());
CDate temp = *this; //save original value
operator++(); //do increment
return(temp); //return original value
}
bool CDate::operator==(const CDate& rhs) const {
assert(isAlive());
assert(rhs.isAlive());
if(this != &rhs) {
return((year == rhs.year) &&
(month == rhs.month) &&
(day == rhs.day));
}
return(true); //this is equal to itself
}
void CDate::Set(int d, int m, int y) {
assert(isAlive());
bool valid = true;
if(y == 0)
valid = false;
else if((m < January) ||
(m > December))
valid = false;
else if((d < 0) ||
(d > daysInMonth(m)))
valid = false;
if(valid) {
year = y;
month = m;
day = d;
}
}
bool CDate::isLeapYear(void) const {
assert(isAlive());
bool leap = (year % 4) ? false : true;
if (leap)
leap = (year % 400) ? true : false;
return(leap);
}
int CDate::DayOfYear(void) const {
assert(isAlive());
int days = 0;
for(int mon = January; mon < month; mon++)
days += daysInMonth(mon);
return(days + day);
}
int CDate::daysInMonth(int mon) const {
switch(mon) {
case February:
if(isLeapYear())
return(29);
else
return(28);
case April:
case June:
case September:
case November:
return(30);
case January:
case March:
case May:
case July:
case August:
case October:
case December:
return(31);
}
return(0); //default case
}
void CDate::show(void) const {
assert(isAlive());
char monStr[10], //name of month as string
era[3]; //"AD" or "BC"
int yr; //absolute value of year for "BC"
switch(month) {
case January:
strcpy(monStr, "January");
break;
case February:
strcpy(monStr, "February");
break;
case March:
strcpy(monStr, "March");
break;
case April:
strcpy(monStr, "April");
break;
case May:
strcpy(monStr, "May");
break;
case June:
strcpy(monStr, "June");
break;
case July:
strcpy(monStr, "July");
break;
case August:
strcpy(monStr, "August");
break;
case September:
strcpy(monStr, "September");
break;
case October:
strcpy(monStr, "October");
break;
case November:
strcpy(monStr, "November");
break;
case December:
strcpy(monStr, "December");
break;
default:
strcpy(monStr, "invalid");
}
if(year > 0)
strcpy(era, "AD");
else {
strcpy(era, "BC");
yr = abs(year);
}
// printf(" %d %s %d %s ", day, monStr, yr, era);
}
//========================
//private helper functions
//========================
inline bool CDate::isAlive(void) const {
return((month != Invalid) &&
(year != 0) &&
(day != 0) );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -