怎么用C++定义一个datetime类,使其对象可以显示当前系统时间和日期 ?谢谢
2个回答
展开全部
#include<time.h>
#include<stdio.h>
class datetime
{
public:
int year;
int month;
int day;
int hour;
int min;
int sec;
datetime()
{
struct tm* ptm;
time_t ltime;
time(<ime);
ptm = localtime(<ime);
year = ptm->tm_year+1900;
month = ptm->tm_mon+1;
day = ptm->tm_mday;
hour = ptm->tm_hour;
min = ptm->tm_min;
sec = ptm->tm_sec;
}
void output()
{
printf("%.4d/%.2d/%.2d %.2d:%.2d:%.2d\n",year,month,day,hour,min,sec);
}
};
void main(void)
{
datetime d;
d.output();
}
#include<stdio.h>
class datetime
{
public:
int year;
int month;
int day;
int hour;
int min;
int sec;
datetime()
{
struct tm* ptm;
time_t ltime;
time(<ime);
ptm = localtime(<ime);
year = ptm->tm_year+1900;
month = ptm->tm_mon+1;
day = ptm->tm_mday;
hour = ptm->tm_hour;
min = ptm->tm_min;
sec = ptm->tm_sec;
}
void output()
{
printf("%.4d/%.2d/%.2d %.2d:%.2d:%.2d\n",year,month,day,hour,min,sec);
}
};
void main(void)
{
datetime d;
d.output();
}
追问
你自己编译过了吗??
追答
真是个有趣的现象我写的 time(& l t ime); 和localtime(& l time);会被百度转换,大概& l t就是
#include
class datetime
{
public:
int year;
int month;
int day;
int hour;
int min;
int sec;
datetime()
{
struct tm* ptm;
time_t m;
time(&m);
ptm = localtime(&m);
year = ptm->tm_year+1900;
month = ptm->tm_mon+1;
day = ptm->tm_mday;
hour = ptm->tm_hour;
min = ptm->tm_min;
sec = ptm->tm_sec;
}
void output()
{
printf("%.4d/%.2d/%.2d %.2d:%.2d:%.2d\n",year,month,day,hour,min,sec);
}
};
void main(void)
{
datetime d;
d.output();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
路过,我也有这个需求。谢谢。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询