王桂林 C++基础与提高 练习题——格式时钟输出(实时的,精确到秒)

 这里直接使用了 localtime 函数获取 tm 时间然后做显示。代码如下:

#include <iostream>
#include <windows.h>
#include "time.h"
#include <process.h>
using namespace std;


void Clock()
{
    struct tm *ptr;
    time_t lt = time(NULL);
    ptr = localtime(&lt);
    cout<<"It is--- ";
    cout.setf(ios::right);
    cout.width(2);
    cout.fill('0');
    cout<<ptr->tm_hour;
    cout<<":";
    cout.setf(ios::right);
    cout.width(2);
    cout.fill('0');
    cout<<ptr->tm_min;
    cout<<":";
    cout.setf(ios::right);
    cout.width(2);
    cout.fill('0');
    cout<<ptr->tm_sec;
    cout<<" ---now."<<endl;
}

int main()
{
    while(true)
    {
        system("cls");
        Clock();
        Sleep(1000);
    }
    return 0;

}

 

 


版权声明:本文为hnfeng2014原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。