日期的加减
/*
设计一个日期类Date,包括年、月、日等私有成员。
要求实现日期的基本运算,例如某日期加上天数或减去天数,两日期相减的天数等。
实现要求:实现运算符加与减的重载
*/
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
using namespace std;
class Date
{
private:
int year;
int month;
int day;
static int sumDays;
public:
int monthDay(int year1,int month1);
void setDate();
void print_sumDays();
string weekday();
bool isYear(int year1);
void print();
Date(int year=1,int month=1,int day=1){};
Date operator +(int days);
Date operator -(int days);
Date operator -(Date date1);
~Date(){}
};
int Date::sumDays=0;
void Date::print_sumDays()
{
cout<<"两日期相差"<<Date::sumDays<<"天"<<endl;
}
string Date::weekday()
{
int year1=year;
int month1=month;
int day1=day;
if(month1==1) {month1=13;year1--;}
if(month1==2) {month1=14;year1--;}
int week=(day1+2*month1+3*(month1+1)/5+year1+year1/4-year1/100+year1/400)%7;
string str="";
switch(week)
{
case 0: str="一"; break;
case 1: str="二"; break;
case 2: str="三"; break;
case 3: str="四"; break;
case 4: str="五"; break;
case 5: str="六"; break;
case 6: str="日"; break;
}
return str;
}
Date Date::operator +(int days)
{
Date a;
a.year=year;
a.month=month;
a.day=day;
while(days>0)//防止出现1月31日
{
a.day++;
days--;
if(a.day>monthDay(a.year,(a.month-1)))
{
a.month++;
a.day=1;
break;
}
}
while(days>=monthDay(a.year,(a.month-1)))
{
days=days-monthDay(a.year,(a.month-1));
a.month++;
if(a.month==13)
{
a.year++;
a.month=1;
break;
}
}
while(true)
{
if(isYear(a.year)&&(days>=366))
{
days=days-366;
a.year++;
}
else if(!isYear(a.year)&&(days>=365))
{
days=days-365;
a.year++;
}
else
break;
}
while(days>=monthDay(a.year,(a.month-1)))
{
days=days-monthDay(a.year,(a.month-1));
a.month++;
if(a.month==13)
{
a.year++;
a.month=1;
}
}
while(days>0)
{
a.day++;
days--;
}
return a;
}
Date Date::operator -(int days)
{
Date a;
a.year=year;
a.month=month;
a.day=day;
while(days>0)//防止出现1月31日
{
if(a.day==monthDay(a.year,a.month-1))
break;
a.day--;
days--;
if(a.day==0)
{
a.month--;
if(a.month==0)
{
a.year--;
a.month=12;
}
a.day=monthDay(a.year,a.month-1);
break;
}
}
while(days>=monthDay(a.year,a.month-1))
{
days=days-monthDay(a.year,(a.month-1));
a.day=monthDay(a.year,(a.month-2+12)%12);
a.month--;
if(a.month==0)
{
a.year--;
a.month=12;
break;
}
}
while(true)
{
if(isYear(a.year)&&(days>=366))
{
days=days-366;
a.year--;
}
else if(!isYear(a.year)&&(days>=365))
{
days=days-365;
a.year--;
}
else
break;
}
while(days>=monthDay(a.year,a.month-1))
{
days=days-monthDay(a.year,(a.month-1));
a.day=monthDay(a.year,(a.month-2+12)%12);
a.month--;
if(a.month==0)
{
a.year--;
a.month=12;
}
}
while(days>0)
{
a.day--;
days--;
}
return a;
}
Date Date::operator -(Date date1)
{
Date a;
if(year>date1.year)
{
for(int year1=date1.year+1;year1<year;year1++)
{
if(isYear(year1))
a.sumDays+=366;
else
a.sumDays+=365;
}
a.sumDays+=monthDay(date1.year,date1.month-1)-date1.day;
while(date1.month<12)
{
sumDays+=monthDay(date1.year,date1.month);
date1.month++;
}
a.sumDays+=day;
int i=0;
while(i<month-1)
{
a.sumDays+=monthDay(year,i);
i++;
}
}
else if(year==date1.year)
{
if(month>date1.month)
{
for(int month1=date1.month;month1<month-1;month1++)
{
sumDays+=monthDay(date1.year,month1);
}
a.sumDays+=monthDay(date1.year,date1.month-1)-date1.day;
a.sumDays+=day;
}
else if(month==date1.month)
{
a.sumDays+=abs(day-date1.day);
}
else
{
for(int month1=month;month1<date1.month-1;month1++)
{
sumDays+=monthDay(year,month1);
}
a.sumDays+=monthDay(year,month-1)-day;
a.sumDays+=date1.day;
}
}
else
{
for(int year1=year+1;year1<date1.year;year1++)
{
if(isYear(year1))
a.sumDays+=366;
else
a.sumDays+=365;
}
a.sumDays+=monthDay(year,month-1)-day;
while(month<12)
{
sumDays+=monthDay(year,month);
month++;
}
a.sumDays+=date1.day;
int i=0;
while(i<date1.month-1)
{
a.sumDays+=monthDay(date1.year,i);
i++;
}
}
return a;
}
void Date::setDate()
{
cout<<"请初始化年月日:"<<endl;
cin>>year>>month>>day;
}
int Date::monthDay(int year1,int month1)
{
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isYear(year1)&&month1==1)
return 29;
else
return day[month1];
}
bool Date::isYear(int year1)
{
if((year1%4==0&&year1%100!=0)||year1%400==0)
return true;
else
return false;
}
void Date::print()
{
cout<<"当前日期为: "<<year<<":"<<month<<":"<<day<<" 星期"<<weekday()<<endl;
}
void main()
{
Date a,b,c;
int select,num;
system("color 4e");
while(true)
{
system("cls");
cout<<"1 某日期加上天数 2 某日期减去天数"<<endl;
cout<<"3 两日期相减的天数 4 退出"<<endl;
cout<<"请选择: ";
cin>>select;
switch(select)
{
case 1:
a.setDate();
cout<<"请输入天数:";
cin>>num;
c=a+num;
c.print();
break;
case 2:
a.setDate();
cout<<"请输入天数:";
cin>>num;
c=a-num;
c.print();
break;
case 3:
a.setDate();
b.setDate();
c=a-b;
c.print_sumDays();
case 4:exit(0);
default:exit(0);
}
system("pause");
}
}