⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 date.cpp

📁 ssd5 exercise1答案
💻 CPP
字号:
/*
* Author:Ma Yaowen
* Version:1.0.0
*/

#include "Date.h"
#include <string>
using namespace std;
//default constructor for date
Date::Date(void){
   year=0;
   month=0;
   day=0;
   hour=0;
   minute=0;
   second=0;
 }

//six parameter constructor for date
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;
}

//set month of date
void Date::setMonth(int& a){
	month=(a>0)&&(a<=12) ? a:0;
}
//set day of date
void Date::setDay(int& b){
	day=b>0 ? b:0;
}
//set year of date
void Date::setYear(int& c){
    year=c>0 ? c:0;
}
//set hour of date
void Date::setHour(int& d){
	hour=(d>0)&&(d<24) ? d:0;
}
//set minute of date
void Date::setMinute(int& e){
    minute=(e>0)&&(e<60) ? e:0;
}
//set second of date
void Date::setSecond(int& f){
    second=(f>0)&&(f<60) ? f:0;
}
//get month of date
int Date::getMonth(void) const{
  return month;
}
//get day of date
int Date::getDay(void) const{
 return day;
}
//get year of date
int Date::getYear(void) const{
  return year;
}
//get hour of date
int Date::getHour(void) const{
  return hour;
}
//get minute of date
int Date::getMinute(void) const{
 return minute;
}
//get second of date
int Date::getSecond(void) const{
 return second;
}
//compares two date object whether they are equal
bool Date::operator== (const Date &rhs){
 
   return (year==rhs.year)&&(month==rhs.month)&&(day==rhs.day)
	   &&(hour==rhs.hour)&&(minute==rhs.minute)&&(second==rhs.second);

}
//compare to see if one date object is less than the other
bool Date::operator< (const Date &rhs){

	if(year<rhs.year){
	 return true;
	}

	if(year>rhs.year){
	 return false;
	}
	
	if(month<rhs.month){
	 return true;
	}
	
	if(month>rhs.month){
	  return false;
	}

	if(day<rhs.day){
	 return true;
	}

	if(day>rhs.day){
	 return false;
	}

	if(hour<rhs.hour){
	return true;
	}

	if(hour>rhs.hour){
	return false;
	}

	if(minute<rhs.minute){
	 return true;
	}

	if(minute>rhs.minute){
	return false;
	}

	if(second<rhs.second){
	return true;
	}
	else{
		return false;
	}
}

//output a date object
ostream &operator<<(ostream& out, const Date& date){

    return out<<date.getMonth()<<"/"<<date.getDay()<<"/"<<date.getYear()<<" "
		<<date.getHour()<<":"<<date.getMinute()<<":"<<date.getSecond()<<endl;
}
//input a date object
istream &operator>>(istream& in, Date& date){

     string p="";
	 int m;

	 getline(in,p,'/');
	 m=atoi(p.c_str());
	 date.setMonth(m);

     getline(in,p,'/');
	 m=atoi(p.c_str());
	 date.setDay(m);

	 getline(in,p,' ');
	 m=atoi(p.c_str());
	 date.setYear(m);

	 getline(in,p,':');
	 m=atoi(p.c_str());
	 date.setHour(m);

	 getline(in,p,':');
	 m=atoi(p.c_str());
	 date.setMinute(m);
	 
	 getline(in,p,'\n');
	 m=atoi(p.c_str());
	 date.setSecond(m);

	 return in;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -