编程求助,帮忙解释下这段C++程序的意思

来源:百度知道 编辑:UC知道 时间:2024/06/03 17:46:48
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>

using namespace std;

void showtime(tm* pt)
{
cout<<setfill('0')<<1900 + pt->tm_year<<'-';
cout<<setw(2)<<pt->tm_mon + 1<<'-';
cout<<setw(2)<<pt->tm_mday<<' ';
cout<<setw(2)<<pt->tm_hour<<':';
cout<<setw(2)<<pt->tm_min<<':';
cout<<setw(2)<<pt->tm_sec<<'\n';
}

int main()
{
time_t t = time(NULL);
tm* pt = localtime(&t);
int sec = pt->tm_sec;
showtime(pt);
while(1)
{
t = time(NULL);
pt = localtime(&t);
if(sec != pt->tm_sec)
{
sec = pt->tm_sec;
system("cls");
showtime(pt);
}
}

return 0;

#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>

using namespace std; //使用名空间,这样就可以直接使用cout打印

void showtime(tm* pt)
{
cout<<setfill('0')<<1900 + pt->tm_year<<'-'; //pt是时间格式 tm_year取出从1900到现在的年数
cout<<setw(2)<<pt->tm_mon + 1<<'-'; //取出月数,这个值的范围[0,11]
cout<<setw(2)<<pt->tm_mday<<' '; //取出天数 这个值[1,31]
cout<<setw(2)<<pt->tm_hour<<':'; //取出小时
cout<<setw(2)<<pt->tm_min<<':'; //取出分
cout<<setw(2)<<pt->tm_sec<<'\n'; //取出秒
}

int main()
{
time_t t = time(NULL); //定义时间类
tm* pt = localtime(&t); //取出本地时间
int sec = pt->tm_sec; //
showtime(pt); //调用函数

//利用一个循环来更新秒的变化
while(1)
{
t = time(NULL);
pt = local