对时间的各种操作

时间:2021-10-09 00:14:21

/*

实现运算符加与减的重载

设计一个时间类,要求能够在时间类上实行以分钟为单位的加减运算,同时当分钟增或减的变化应体现日期变化。

如:2012年11月28日12:10PM 增加12小时后,应变为2012年11月29日00:10AM

输入一个未来时间(含日期和时间),计算时间间隔,建立一个倒计时显示的计时器(以秒为单位变化)

。如当前时间为2012年12月5日15:40,输入时间为2012年12月6日14:40.

 

  输入一个未来时间(含日期和时间),计算时间间隔,建立一个倒计时显示的计时器(以秒为单位变化)。

  如当前时间为2012年12月5日15:40,输入时间为2012年12月6日14:40.

 

*/

#include<iostream>

#include<string>

#include<iomanip>

#include<cmath>

#include<windows.h>

using namespace std;

class Date

{

public:

        

         int year;

         int month;

         int day;

         static int sumDays;

        

        

         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;

class Time

{

private:

         int hour;

         int minute;

         Date date;

         Date date1;

public:

         void setTime();

         void setTime1(Time &time);

         void operator +(int min);

         void operator -(int min);

         void daojishi();

};

void Time::setTime1(Time &time)

{

         date.setDate();

         cout<<"输入时间1 :";

         cin>>hour>>minute;

         date1.setDate();

         cout<<"输入时间2 :";

         cin>>time.hour>>time.minute;

}

 

int gotoxy(int x,int y)

{

    COORD pos={x,y};

         HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);

         SetConsoleCursorPosition(hOut,pos);

         return(0);

}//屏幕从左向右代表x的正方向,从上至下为y正方向。

void Time::daojishi()

{

         int sec=0,i;

         Time u;

         setTime1(u);

         cout<<"倒计时:"<<endl;

         while(true)

         {

                  

                   gotoxy(0,11);

                   cout<<"                             ";

             gotoxy(0,11);

                   cout<<date1.year<<"-"<<date1.month<<"-"<<date1.day<<"  "<<u.hour<<":"<<u.minute<<":"<<sec;

         if((date.year==date1.year)&&(date.month==date1.month)&&(date.day==date1.day)&&(hour==u.hour)&&(minute==u.minute)&&(sec==0))

                            break;

      

                   Sleep(50);

                   sec--;

                   if(sec<0)

                   {

                            sec=(sec+60)%60;

                            u.minute--;

                            if(u.minute<0)

                            {

                                     u.hour--;

                                     u.minute=(u.minute+60)%60;

                                               if(u.hour<0)

                                               {

                                                       

                                                        date1.day--;

                                                        u.hour=(u.hour+24)%24;

                                                       

                                                        if(date1.day<1)

                                                        {

                                                                 date1.month--;

                                                                 if(date1.month<1)

                                                                 {

                                                                           date1.year--;

                                                                           date1.month=12;

                                                                 }

                                                                 date1.day=date1.monthDay(date1.year,date1.month-1);

                                                        }

                                               }

                            }

                   }

         }

         gotoxy(0,12);

}

void Time::setTime()

{

         date.setDate();

         cout<<"请初始化时间:"<<endl;

         cin>>hour>>minute;

}

void Time::operator +(int min)

{

         int d,h,m;

         d=min/(24*60);

         min=min%(24*60);

         h=min/60;

         min=min%60;

         m=min;

         if(d!=0)

         {

                   date=date.operator +(d);

         }

         if(hour+h>=24)

         {

                   hour=(hour+h)%24;

                   date.day++;

                   if(date.day>date.monthDay(date.year,date.month-1))

                   {

                            date.month++;

                            date.day=1;

                            if(date.month>12)

                            {

                                     date.year++;

                                     date.month=date.month%12;

                            }

                           

                   }

         }

         else

                   hour=(hour+h)%24;

         if(minute+m>=60)

         {

                   minute=(minute+m)%60;

                   hour++;

                   if(hour>=24)

                            date.day++;

                   if(date.day>date.monthDay(date.year,date.month-1))

                   {

                            date.month++;

                            if(date.month>12)

                                     date.year++;

                   }

         }

         else

                   minute=(minute+m)%60;

         date.print();

         cout<<" "<<hour<<":"<<minute<<endl;

        

}

void Time::operator -(int min)

{

         int d,h,m;

         d=min/(24*60);

         min=min%(24*60);

         h=min/60;

         min=min%60;

         m=min;

         cout<<min<<endl;

         if(d!=0)

         {

                   date=date.operator -(d);

         }

         if(hour-h<0&&h!=0)

         {

                   hour=(hour-h+24)%24;

                   date.day--;

                   if(date.day<=0)

                   {

                            date.month--;

                            if(date.month==0)

                                     date.year--;

                            if(date.month==0)

                                     date.month=12;

                            date.day=date.monthDay(date.year,(date.month-1)%12);

                   }

         }

         else

                   hour=(hour-h+24)%24;

         if(minute-m<0)

         {

                   minute=(minute-m+60)%60;

                   hour--;

                  

                   if(hour<0)

                            date.day--;

                   hour=(hour+24)%24;

                  

                   if(date.day==0)

                   {

                            date.month--;

                           

                            if(date.month==0)

                                     date.year--;

                            if(date.month==0)

                                     date.month=12;

                            date.day=date.monthDay(date.year,(date.month-1)%12);

                           

                   }

         }

         else

                   minute=(minute-m+60)%60;

         date.print();

         cout<<" "<<hour<<":"<<minute<<endl;

        

}

 

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++;

                            if(a.month==13)

                            {

                                     a.year++;

                                     a.month=1;

                                     a.day=1;

                                     break;

                            }

                            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;

                   }

         }

         if(days>0)

         {

                   a.day=1+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.month--;

                   a.day=monthDay(a.year,(a.month-1+12)%12);

                  

                   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.month--;

                   a.day=monthDay(a.year,(a.month-1+12)%12);

                  

                   if(a.month==0)

                   {

                            a.year--;

                            a.month=12;

                   }

                  

                  

                  

         }

         if(days>0)

         {

                   a.day=monthDay(a.year,(a.month-1+12)%12)-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();

        

}

void main()

{

         Date a,b,c;

         Time time;

         int select,num;

         system("color 4e");

         while(true)

         {

                   system("cls");

                   cout<<"1 某日期加上天数                   2 某日期减去天数"<<endl;

                   cout<<"3 两日期相减的天数                 4 某时间加分钟得时间"<<endl;

                   cout<<"5 某时间减分钟得时间               6 倒计时"<<endl;

                   cout<<"7 退出"<<endl;

                   cout<<"请选择: ";

                   cin>>select;

                   switch(select)

                   {

                   case 1:

                            a.setDate();

                            cout<<"请输入天数:";

                            cin>>num;

                            c=a+num;

                            c.print();

                            cout<<endl;

                            break;

                   case 2:

                            a.setDate();

                            cout<<"请输入天数:";

                            cin>>num;

                            c=a-num;

                            c.print();

                            cout<<endl;

                            break;

                   case 3:

                            a.setDate();

                            b.setDate();

                            c=a-b;

                            c.print_sumDays();

                            break;

                   case 4:

                            time.setTime();

                            cout<<"请输入分钟:";

                            cin>>num;

                            time.operator +(num);

                            break;

                   case 5:

                            time.setTime();

                            cout<<"请输入分钟:";

                            cin>>num;

                            time.operator -(num);

                            break;

        case 6:

                            time.daojishi();

                            break;

                   case 7:

                            exit(0);

                   default:exit(0);

                   }

                   system("pause");

         }

}

 

/*

6

2011 12 31

23 58

2012 1 1

0 1

 

*/