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

📄 date.cpp

📁 This assessment requires the creation of three of the core classes of the auction project. Class Dat
💻 CPP
字号:
#include "date.h"

Date::Date(int month, int day, int year, int hour, int minute, int second)
			:month(month),day(day),year(year),hour(hour),minute(minute),second(second)
{
}
Date::Date():month(0),day(0),year(0),hour(0),minute(0),second(0)
{
}
//month
int Date::getMonth()const
{
	return month;
}
void Date::setMonth(int& m)
{
	month = m;
}
//day
int Date::getDay()const
{
	return day;
}
void Date::setDay(int& d)
{
	day = d;
}
//year
int Date::getYear()const
{
	return year;
}
void Date::setYear(int& y)
{
	year =y;
}
//hour
int Date::getHour()const
{
	return hour;
}
void Date::setHour(int& h)
{
	hour = h;
}
//minute
int Date::getMinute()const
{
	return minute;
}
void Date::setMinute(int& m)
{
	minute = m;
}
//second
int Date::getSecond()const
{
	return second;
}
void Date::setSecond(int& s)
{
	second = s;
}
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
		);
}
bool Date::operator <(const Date& rhs)
{
	if(year < rhs.year )return true;
	else if (year > rhs.year)
		return false;
	else
	{
		if(month <rhs.month )
			return true;
		else if(month > rhs.month)
			return false;
		else 
		{
			if(day <rhs.day )
				return true;
			else if(day > rhs.day)
				return false;
			else
			{
				if(hour < rhs.hour)
					return true;
				else if(hour > rhs.hour)
					return false;
				else 
				{
					if(minute < rhs.minute)
						return true;
					else if(minute > rhs.minute)
						return false;
					else 
					{
						if(second < rhs.second)
							return true;
						else if(second > rhs.second)
							return false;						
					}

				}
			}			
		}
	}
	return false;
}

ostream& operator << (ostream& os,const Date& d)
{
	return os<<d.getMonth()<<"/"<<d.getDay()<<"/"<<d.getYear()<<" "<<d.getHour()<<":"
		<<d.getMinute()<<":"<<d.getSecond();
}
istream &operator>>(istream& is, Date&d)
{
	string str;//"mm/dd/yyyy hh:mm:ss\n"
	is >> str;

	int month,day,year,hour,minute,second;
	string::size_type pos1,pos2;
	pos1 = str.find_first_of ("/");
    month = atoi(str.substr(0, pos1).c_str());

	pos2 = str.find_last_of("/");
	day = atoi(str.substr(pos1+1,pos2).c_str());
     
	year = atoi(str.substr(pos2+1,pos2+5).c_str());
	is >> str;
	
	pos1 = str.find_first_of(":");
	hour = atoi(str.substr(0,pos1).c_str());

	pos2 = str.find_last_of(":");
	minute = atoi(str.substr(pos1+1,pos2).c_str());

	second = atoi(str.substr(pos2+1).c_str());

	d.setMonth(month);
	d.setDay(day);
	d.setYear(year);
	d.setHour(hour);
	d.setMinute(minute);
	d.setSecond(second);
	return is;
}
//another mothod  to solve the >> operator
/*const stringstream& operator >> (const stringstream& s,Date& d)
{
	string str = s.str();
	int month,day,year,hour,minute,second;
	string::size_type pos1,pos2,pos3,pos4,pos5,pos6;
	pos1 = str.find_first_of ("/");
    month = atoi(str.substr(0, pos1).c_str());

	pos2 = str.find_last_of("/");
	day = atoi(str.substr(pos1+1,pos2).c_str());

	pos3 = str.find_first_of(" ");
	year = atoi(str.substr(pos2+1,pos3).c_str());

	pos4 = str.find_first_of(":");
	hour = atoi(str.substr(pos3+1,pos4).c_str());

	pos5 = str.find_last_of(":");
	minute = atoi(str.substr(pos4+1,pos5).c_str());

	pos6 = str.find_last_of("\n");
	second = atoi(str.substr(pos5+1,pos6).c_str());

	d.setMonth(month);
	d.setDay(day);
	d.setYear(year);
	d.setHour(hour);
	d.setMinute(minute);
	d.setSecond(second);
	return s;
}*/

⌨️ 快捷键说明

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