Code:
// OOP-Giuaky2011.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; class ngay { private: int dd,mm,yy; public: ngay() { dd=mm=yy=0; } ~ngay() { } ngay(int a,int b,int c) { dd=a;mm=b;yy=c; } friend istream &operator >>(istream &is,ngay &d); friend ostream &operator <<(ostream &os,ngay &d); int operator -(ngay a) { int kc=0; kc=abs(yy-a.yy)*365; int ngay1=dd-1+(mm-1)*30; int ngay2=a.dd-1+(a.mm-1)*30; kc=kc+abs(ngay1-ngay2); return kc; } ngay operator +(int a) { ngay tamp; switch(mm) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: { if(dd<=31-a) { tamp.dd=dd+a; tamp.mm=mm; tamp.yy=yy; } else { if(mm==12) { tamp.dd=dd+a-31; tamp.mm=1; tamp.yy=yy+1; } else { tamp.dd=dd+a-31; tamp.mm=mm+1; tamp.yy=yy; } } break; } case 4: case 6: case 9: case 11: { if(dd<=30-a) { tamp.dd=dd+a; tamp.mm=mm; tamp.yy=yy; } else { tamp.dd=dd+a-30; tamp.mm=mm+1; tamp.yy=yy; } break; } case 2: { if((yy%4==0&&yy%100!=0)||(yy%400)==0) { if(dd<=29-a) { tamp.dd=dd+a; tamp.mm=mm; tamp.yy=yy; } else { tamp.dd=dd+a-29; tamp.mm=3; tamp.yy=yy; } } else { if(dd<=28-a) { tamp.dd=dd+a; tamp.mm=mm; tamp.yy=yy; } else { tamp.dd=dd+a-28; tamp.mm=3; tamp.yy=yy; } } break; } } return tamp; } ngay operator -(int a) { ngay tamp; switch(mm) { case 1: case 2: case 4: case 6: case 8: case 9: case 11: { if(dd>=1+a) { tamp.dd=dd-a; tamp.mm=mm; tamp.yy=yy; } else { if(mm==1) { tamp.dd=dd-a+31; tamp.mm=12; tamp.yy=yy-1; } else { tamp.dd=dd-a+31; tamp.mm=mm-1; tamp.yy=yy; } } break; } case 5: case 7: case 10: { if(dd>=1+a) { tamp.dd=dd-a; tamp.mm=mm; tamp.yy=yy; } else { tamp.dd=dd-a+30; tamp.mm=mm-1; tamp.yy=yy; } break; } case 3: { if((yy%4==0&&yy%100!=0)||(yy%400)==0) { if(dd>=1+a) { tamp.dd=dd-a; tamp.mm=mm; tamp.yy=yy; } else { tamp.dd=dd-a+29; tamp.mm=2; tamp.yy=yy; } } else { if(dd>=1+a) { tamp.dd=dd-a; tamp.mm=mm; tamp.yy=yy; } else { tamp.dd=dd-a+28; tamp.mm=2; tamp.yy=yy; } } break; } } return tamp; } }; istream &operator >>(istream &is,ngay &d) { cout<<"Nhap ngay thang nam (dd/mm/yyyy)"<<endl; cin>>d.dd; cin>>d.mm; cin>>d.yy; return is; } ostream &operator <<(ostream &os,ngay &d) { cout<<"Ngay thang nam: "; os<<d.dd<<"/"<<d.mm<<"/"<<d.yy<<endl; return os; } void main() { ngay a(31,12,2010),b; cin>>b; cout<<a-b<<endl; b=a+5; b=a-4; cout<<b; system("pause"); }
Comment