C++ class Date
来源:互联网 发布:c语言编写一个计算器 编辑:程序博客网 时间:2024/06/07 02:30
<pre name="code" class="cpp">#include<iostream>#include<windows.h>using namespace std;static int data[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};class Date{ friend int count(const Date &);//有元函数,计算该日期是该年的第几天 friend bool leap_year(int);//判断是否为闰年 friend bool operator<(Date &,Date &);//重载< friend bool operator>(Date &,Date &);//重载> friend bool operator==(Date &,Date &);//重载==public: Date();//默认构造函数 void set(int,int,int);//设置日期 Date& operator-(int);//一个日期减去天数 Date& operator+(int);//一个日期加上天数 int sum();//从0天开始算起,该日期一共多少天 void display();//显示日期private: int year,month,day;//定义私有变量};Date::Date(){ year=month=day=0;}bool leap_year(int y){ if(y%400==0||(y%4==0&&y%100!=0)) { data[2]=29; return true; } return false;}void Date::set(int y,int m,int d){ year=y>0?y:0; month=(m>0&&m<=12)?m:0; leap_year(y); day=(d>0&&d<=data[m])?d:0;}int Date::sum(){ int sum; if(leap_year(year)) sum=(year-1)*366; sum=(year-1)*365; for(int i=1;i<month;i++) sum+=data[i]; sum+=day; return sum;}int count(const Date &a){ int count=0; leap_year(a.year); for(int i=0;i<a.month;i++) count+=data[i]; count+=a.day; return count;}bool operator<(Date &a,Date &b){ return a.sum()<b.sum();}bool operator>(Date &a,Date &b){ return a.sum()>b.sum();}bool operator==(Date &a,Date &b){ return a.sum()==b.sum();}Date& Date::operator+(int x){ int flag=0; if(x==0) return *this; leap_year(year); if((day+x)>data[month]) { month++; flag++; if(month>12) { year++; month=1; } } if(flag==0) day+=x; else day=x; return *this;}Date& Date::operator-(int x){ int flag=0; if(x==0) return *this; leap_year(year); if(day<=x) { month--; flag++; if(month<1) { year--; month=12; } } if(flag==0) day-=x; day=data[month]+1-x; return *this;}int operator-(Date &a,Date &b){ int t; if(a==b) t=a.sum()-b.sum(); else if(a<b) t=b.sum()-a.sum(); else t=a.sum()-b.sum();}void Date::display(){ cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;}int main(){ Date a,b; cout<<"set date"<<endl; a.set(2000,2,29); b.set(2000,2,1); cout<<"data a:"<<endl; a.display(); cout<<"data b:"<<endl; b.display(); cout<<"从0开始该日期共多少天"<<endl; cout<<count(a); cout<<endl; cout<<"date a>date b"<<endl; cout<<(a>b)<<endl; cout<<"date a<date b"<<endl; cout<<(a<b)<<endl; cout<<"date a==date b"<<endl; cout<<(a==b);}
结果如下:
0 0
- 【C#】16. IMM (Date class 更新)
- 1641 OOP in C (Date class)
- Date class
- C++ class Date
- android-Date.class
- class Date 日期类
- SOJ.Date Class with Exception
- error C2629: unexpected \'class Date (\'
- C.Class
- [C++]Class
- c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date'
- 【C#】 8. Date类型
- [C#] Date类型
- 【C++】 日期类Date
- 1004. The Date class (version 1)
- 2016/5/27 1002. Modify Date class
- 日期类的实现 class Date()
- 【C++】c++日期类Date
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 7 Spaceship
- 好事客导航_新一代安全、实用、便捷的网址导航
- java反射
- 深入理解HTTP Session
- IP查询接口
- C++ class Date
- 《c程序设计语言》读书笔记-5.6-指针重写getline等函数
- [UI]Android启动引导界面ViewPager
- apache jmeter压力测试初体会-Mongodb Elasticsearch Test
- iphone系统架构以及各层提供的主要服务
- Ueditor
- 策略模式和简单工厂
- <<深入理解计算机系统>>之构建tinyweb源码
- 解决listview与SwipeRefreshLayout滑动冲突问题