华为又出变态面试题!大家请看以下代码 在看懂的同时 修改错误!
#include<iostream>using namespace std;
#include <ctime>
class CLF_DateTime
{
private:
struct tm *local;
public:
CLF_DateTime(int year, int month, int day,int hour, int min,int sec);
CLF_DateTime(CLF_DateTime &x);
CLF_DateTime()
{
}
~CLF_DateTime(void);
void display(char * strdisplay);
void setdisplay(char * setstrdisplay);
int compare(CLF_DateTime* pa, CLF_DateTime* pb);
};
CLF_DateTime::CLF_DateTime(CLF_DateTime &x)
{
local = new tm;
local->tm_year =x.local->tm_year;//struct tm中定义的年成员tm_year是以1900为基数的,也就是说他显示2007时tm_year值并不是2007
local->tm_mon = x.local->tm_mon;//月份可见definition里有说明是[0,11]所以比现实小了一个月
local->tm_mday = x.local->tm_mday;
local->tm_hour =x.local->tm_hour;
local->tm_min =x.local->tm_min;
local->tm_sec =x.local->tm_sec;
}
CLF_DateTime::CLF_DateTime(int year, int month, int day,int hour, int min,int sec)
{
local = new tm;
local->tm_year = year-1900;//struct tm中定义的年成员tm_year是以1900为基数的,也就是说他显示2007时tm_year值并不是2007
local->tm_mon = month-1;//月份可见definition里有说明是[0,11]所以比现实小了一个月
local->tm_mday = day;
local->tm_hour =hour;
local->tm_min =min;
local->tm_sec =sec;
}
CLF_DateTime::~CLF_DateTime(void)
{
delete(local);
local = NULL;
}
void CLF_DateTime::display(char * strdisplay)
{
strftime(strdisplay,80,"%Y-%m-%d %H:%M:%S",local);
}
void CLF_DateTime::setdisplay(char * setstrdisplay)
{
if(local->tm_sec<30&&local->tm_sec>=0)
{
local->tm_sec=0;
strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
}
else if (local->tm_sec>=30&&local->tm_sec<=60)
{
local->tm_min+=1;
local->tm_sec=0;
strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
}
if (local->tm_min>=60)
{
local->tm_hour+=1;
local->tm_min=0;
strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
}
if (local->tm_hour>=24)
{
local->tm_mday+=1;
local->tm_hour=0;
strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
}
if (local->tm_mon==1||local->tm_mon==3||local->tm_mon==5||local->tm_mon==7||local->tm_mon==8||local->tm_mon==10||local->tm_mon==12)
{
if(local->tm_mday>=31)
{
local->tm_mon+=1;
local->tm_mday=1;
strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
}
}
else if (local->tm_mon==4||local->tm_mon==6||local->tm_mon==9||local->tm_mon==11)
{
if(local->tm_mday>=30)
{
local->tm_mon+=1;
local->tm_mday=1;
strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
}
}
else if (local->tm_mon==2&&(local->tm_year%400==0||(local->tm_year%4==0&&local->tm_year%100!==0)))
{
if(local->tm_mday>=29)
{
local->tm_mon+=1;
local->tm_mday=1;
strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
}
}
else if (local->tm_mon=2)
{
if(local->tm_mday>=28)
{
local->tm_mon+=1;
local->tm_mday=1;
strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
}
}
if (local->tm_mon>=12)
{
local->tm_year+=1;
local->tm_mon=1;
strftime(setstrdisplay,80,"%Y-%m-%d %H:%M:%S",local);
}
int CLF_DateTime::compare(CLF_DateTime* pa, CLF_DateTime* pb)
{
if(pa.local->tm_year>pb.local->tm_year)
{
cout<<"1"<<endl;
}
else if(pa.local->tm_year<pb.local->tm_year)
{
cout<<"-1"<<endl;
}
else if(pa.local->tm_yaer==pb.local->tm_year)
{
if (pa.local->tm_mon>pb.local->tm_mon)
{
cout<<"1"<<endl;
}
else if(pa.local->tm_mon<pb.local->tm_mon)
{
cout<<"-1"<<endl;
}
else if(pa.local->tm_mon==pb.local->tm_mon)
{
if(pa.local->tm_mday>pb.local->tm_mday)
{
cout<<"1"<<endl;
}
else if(pa.local->tm_mday<pb.local->tm_mday)
{
cout<<"-1"<<endl;
}
else if(pa.local->tm_mday==pb.local->tm_mday)
{
if(pa.local->tm_hour>pb.local->tm_hour)
{
cout<<"1"<<endl;
}
else if(pa.local->tm_hour<pb.local->tm_hour)
{
cout<<"-1"<<endl;
}
else if(pa.local->tm_hour==pb.local->tm_hour)
{
if(pa.local->tm_min>pb.local->tm_min)
{
cout<<"1"<<endl;
}
else if(pa.local->tm_min<pb.local->tm_min)
{
cout<<"-1"<<endl;
}
else if (pa.local->tm_min==pb.local->tm_min)
{
if(pa.local->tm_sec!=0)
{
cout<<"1"<<endl;
}
else
{
cout<<"0"<<endl;
}
}
}
void main()
{
CLF_DateTime pa(2007,11,8,14,52,10);
CLF_DateTime pb(pa);
char testdisplay[100];
cout<<"Your input date and time is: ";
pa.display(testdisplay);
cout<<strdisplay<<endl;
cout<<"Provide rounding:";
pa.setdisplay(testdisplay);
cout <<setstrdisplay<<endl;
cout<<"compare time:";
(pb);
}