C++實現(xiàn)當前時間動態(tài)顯示的方法
本文實例講述了C++實現(xiàn)當前時間動態(tài)顯示的方法。分享給大家供大家參考。具體如下:
/* 24-06-10 10:44
動態(tài)顯示時間 但不是最優(yōu)的 功能很單一
本程序關(guān)鍵是對時鐘函數(shù)的使用
**tm結(jié)構(gòu)定義了 年、月、日、時、分、秒、星期、當年中的某一天、夏令時
**用localtime獲取當前系統(tǒng)時間,該函數(shù)將一個time_t時間轉(zhuǎn)換成tm結(jié)構(gòu)表示的時間,函數(shù)原型:
struct tm * localtime(const time_t *)
**使用gmtime函數(shù)獲取格林尼治時間,函數(shù)原型:
struct tm * gmtime(const time_t *) 包含的頭文件是time.h */
//struct tm {
// int tm_sec; /* seconds after the minute - [0,59] */
// int tm_min; /* minutes after the hour - [0,59] */
// int tm_hour; /* hours since midnight - [0,23] */
// int tm_mday; /* day of the month - [1,31] */
// int tm_mon; /* months since January - [0,11] */
// int tm_year; /* years since 1900 */
// int tm_wday; /* days since Sunday - [0,6] */
// int tm_yday; /* days since January 1 - [0,365] */
// int tm_isdst; /* daylight savings time flag */
// };
#include <iostream>
#include <time.h>
#include "dos.h"
#include <windows.h>
using namespace std;
int main()
{
char *myweek[]={"日","一","二","三","四","五","六"};
time_t nowtime; //typedef long time_t;在編譯器定義的頭文件中
nowtime = time(NULL); //獲取當前時間 此時它是用一個長整形表示的
struct tm *local; /*時間結(jié)構(gòu)體變量*/
local = localtime(&nowtime); //獲取當前系統(tǒng)時鐘
while (1)
{
cout<<"當前時間:";
cout<<local->tm_year+1900<<"年"<<local->tm_mon+1<<"月"<<local->tm_mday<<"日"<<" ";
cout<<local->tm_hour<<"時"<<local->tm_min<<"分"<<local->tm_sec<<"秒"<<" ";
cout<<"星期"<<myweek[local->tm_wday]<<endl;
/* 對當前時間進行判斷 讓它動態(tài)變化
*/
if(local->tm_sec==59 && local->tm_min!=59)
//當秒到59,分未到59時 分鐘加1,秒清0
{
local->tm_min++;
local->tm_sec=0;
}
//當秒和分都為59 時不為23時 ,秒和分鐘都清0,時鐘加1
else if(local->tm_sec==59 && local->tm_min==59 && local->tm_hour!=23)
{
local->tm_min=0;
local->tm_sec=0;
local->tm_hour++;
}
//當秒和分都為59 時為23時 ,秒,分鐘和時鐘都清0
else if(local->tm_sec==59&&local->tm_min==59&&local->tm_hour==23)
{
local->tm_sec=0;
local->tm_min=0;
local->tm_hour=0;
}
else //其它情況秒鐘進行不斷加1
{
local->tm_sec++;
}
Sleep(1000); /*Sleep()里面的單位,是以毫秒為單位,
system("cls"); /*清屏命令 出現(xiàn)動態(tài)顯示*/
}
system("pause");
return 0;
}
希望本文所述對大家的C++程序設(shè)計有所幫助。
上一篇:c實現(xiàn)linux下的數(shù)據(jù)庫備份
欄 目:C語言
本文標題:C++實現(xiàn)當前時間動態(tài)顯示的方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2969.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實現(xiàn)方法


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改


