注册 登录
编程论坛 C++教室

棘手的问题,此程序如何改为面向对象形式,求解!

单调黑白 发布于 2007-11-05 17:05, 389 次点击
棘手的问题,此程序如何改为面向对象形式,求解!
#include<time.h>
#include<iostream>
using namespace std;

int main(void)
{
struct tm *local;
time_t t;

t=time(NULL);
local=localtime(&t);
cout<<"local time and date:"<<asctime(local)<<endl;

return 0;
}
2 回复
#2
单调黑白2007-11-05 17:10
回复:(单调黑白)棘手的问题,此程序如何改为面向对象...
问题补冲:
此程序输出的本地日期及时间如何改变为"yyyy-mm-dd HH:MM:SS"形式! 谢谢!
#3
高温煎饺2007-11-06 13:00

随便改的,比较简陋~呵呵
#include<time.h>
#include<iostream>
using namespace std;
class Time
{
private:
struct tm *local;
time_t t;

public:

Time()
{
t=time(NULL);
local=localtime(&t);
}

void display()
{
char* buff;
char buff1[20];
int j=0;

buff=asctime(local);

for(int i=20;i<24;i++,j++)
buff1[j]=buff[i];

buff1[j]=' ';

j ++;

for(i=4;i<20;i++,j++)
buff1[j]=buff[i];

cout<<"local time and date:";
for(i=0;i<20;i++)
cout<<buff1[i];

cout<<endl;
}



};
int main(void)
{

Time time;
time.display();


return 0;
}
1