设计一个日期类Date,,要求: (1)包含年(year)、月(month)和日(day)私有数据成员。 (2)包含构造函数,重载关于一日期加上天数的加法运算符+、重载关于一日期减去天数的减加运算符-
来源:互联网 发布:淘宝网货源网 编辑:程序博客网 时间:2024/06/05 17:41
#include<iostream>
using namespace std;
class Date{
private:
int year,month,day;
public:
Date(int y=1,int m=1 ,int d=1){
year=y;
month=m;
day=d;
}
int GetYear() const {return year;}
int GetMonth() const {return month;}
int GetDay() const {return day;}
Date operator+(int days);
Date operator-(int days);
static bool LeapYear(int y);
static int GetYearDays(int y);
static int GetMonthDays(const Date &d);
static int DateNum(const Date &d);
static Date NumDate(int n);
friend ostream &operator<<(ostream &cout,const Date&d);
friend istream &operator>>(istream &cin,const Date&d);
};
Date Date::operator+(int days)
{
int n=DateNum(*this)+days;
return NumDate(n);
}
Date Date::operator-(int days)
{ int n=DateNum(*this)-days;
return NumDate(n);
}
bool Date::LeapYear(int y)
{
if(y%4==0 && y%100!=0||y%400==0)
return true;
else
return false;
}
int Date::GetYearDays(int y)
{
if(LeapYear(y))
return 366;
else
return 365;
}
int Date::GetMonthDays(const Date &d)
{
int n;
switch(d.GetMonth())
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
n=31;
break;
case 4:
case 6:
case 9:
case 11:
n=30;
break;
case 2:
if(LeapYear(d.GetYear()))
n=29;
else
n=28;
}
return n;
}
int Date::DateNum(const Date &d)
{
int y,n=0;
for(y=1;y<d.GetYear();y++)
n+=GetYearDays(y);
for(int m=1;m<d.GetMonth();m++)
n+=GetMonthDays(Date(y,m,1));
n+=d.GetDay();
return n;
}
Date Date::NumDate(int n)
{
int y,m,d,rest=n;
for(y=1,rest=n;rest>GetYearDays(y);y++)
rest-=GetYearDays(y);
for(m=1;rest>GetMonthDays(Date(y,m,1));m++)
rest-=GetMonthDays(Date(y,m,1));
d=rest;
return Date(y,m,d);
}
ostream &operator<<(ostream &out,Date &d)
{
out<<d.GetYear()<<"-"<<d.GetMonth<<"-"<<d.GetDay();
return out;
}
istream &operator>>(istream &in,Date &d)
{
int year,month,day;
cin>>year>>month>>day;
d=Date(year,month,day);
return in;
}
int main(void)
{
Date d(2013,1,19);
cout<<d<<endl;
cout<<"10日后:"<<d+10<<endl;
cout<<"10日前:"<<d-10<<endl;
cin>>d;
cout<<"日期:"<<d<<endl;
return 0;
}
- 设计一个日期类Date,,要求: (1)包含年(year)、月(month)和日(day)私有数据成员。 (2)包含构造函数,重载关于一日期加上天数的加法运算符+、重载关于一日期减去天数的减加运算符-
- 设计一个日期类Date,,要求: (1)包含年(year)、月(month)和日(day)私有数据成员。 (2)包含构造函数,重载关于一日期加上天数的加法运算符+、重载关于一日期减去天数的减加运算符-
- 设计一个时间类Time,要求: (1)包含时(hour)、分(minute)和秒(second)私有数据成员。 (2)包含构造函数,重载关于一时间加上另一时间的加法运算符+、重载关于一时间减去另一时间
- 【7.11】为Date类重载“+”运算符,实现在某一个日期上(月、日、年)加一个天数。
- JAVA 获取相差天数和获取一日期相差某天数后的日期(可用于计算各种纪念日)
- 关于拷贝构造函数和重载成员运算符=
- 编写一个密码类,其中包含一个 str 密码字符串私有成员数据,一个“==”运算 符重载成员函数,用于比较用户输入的密码是否正确。并用数据测试该类。
- 8.3 编写一个密码类,其中包含一个 str 密码字符串私有成员数据,一个“==”运算 符重载成员函数,用于比较用户输入的密码是否正确。并用数据测试该类
- 关于C++成员函数和运算符的重载
- 求两日期之间的相隔天数(year,month,day)
- 成员函数和运算符的重载
- 关于运算符重载 成员函数运算符重载 友元函数运算符重载
- 8.6 编写一个类,其中有一个 double 型私有数据成员 yuan(存放人民币数量,单位 为元),另外包含一个转换运算符重载成员函数以将 yuan 转换成美元(假设 1 美元对换 8.268 人民
- 日期类的实现(其中包括构造函数,拷贝构造,赋值运算符重载,输出运算符重载,友元函数等等)
- 关于重载拷贝构造函数和赋值运算符
- 关于运算符的重载
- 第八周-运算符重载-实现复数类中的运算符重载(1)-类的成员函数
- 创建一个CPoint类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数, 运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。可以根据需要 加入自己的成员变量或成员函数
- UI的设计风格
- android中关于tools:context="activity name"解惑
- misc_vi简单配置
- [Codeforces] 148A - Insomnia cure
- 一、centos上VNC Server配置
- 设计一个日期类Date,,要求: (1)包含年(year)、月(month)和日(day)私有数据成员。 (2)包含构造函数,重载关于一日期加上天数的加法运算符+、重载关于一日期减去天数的减加运算符-
- 设计一个时间类Time,要求: (1)包含时(hour)、分(minute)和秒(second)私有数据成员。 (2)包含构造函数,重载关于一时间加上另一时间的加法运算符+、重载关于一时间减去另一时间
- 我读过的计算机书籍(修改版)
- Eclipse插件安装 duplicate location求助
- Jeecg 的DataGridTag 表格属性
- kkkk
- Jeecg DataGridTag 的子标签们
- Jeecg DataGridTag 的子标签们 二
- unable to open the service tomcat 解决方式