c語言時間顯示
1. 如何用c語言編寫一個顯示時間的函數,要求時間顯示精度到毫秒級別。
#include <cstdio>
#include <ctime>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void printTime() {
struct tm t; //tm結構指針
time_t now; //聲明time_t類型變數
time(&now); //獲取系統日期和時間
localtime_s(&t, &now); //獲取當地日期和時間
//格式化輸出本地時間
printf("年-月-日-時-分-秒:%d-%d-%d %d:%d:%d ", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
}
int main(int argc, char** argv) {
printTime();
}
2. C語言中如何輸出顯示程序的運行時間 望賜教!
BOOLQueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);可以返回硬體支持的高精度計數器的頻率。先調用QueryPerformanceFrequency()函數獲得機器內部計時器的時鍾頻率。接著在需要嚴格計時的事件發生前和發生之後分別調用QueryPerformanceCounter(),利用兩次獲得的計數之差和時鍾頻率,就可以計算出事件經歷的精確時間。
#include"stdafx.h"
#include<windows.h>
#include<time.h>
#include"process.h"
#definerandom(x)(rand()%x)
int_tmain(intargc,_TCHAR*argv[])
{
LARGE_INTEGERfre={0};//儲存本機CPU時鍾頻率
LARGE_INTEGERstartCount={0};
LARGE_INTEGERendCount={0};
QueryPerformanceFrequency(&fre);//獲取本機cpu頻率
//開始計時
QueryPerformanceCounter(&startCount);
//運算
for(inti=0;i<10000000;i++)
{
floatfTem1=random(100)*random(1000)*random(10000)*random(100000);
}
//結束計時
QueryPerformanceCounter(&endCount);
//計算時間差
doubledTimeTake=((double)endCount.QuadPart-(double)startCount.QuadPart)/(double)fre.QuadPart;
printf("用時%f ",dTimeTake);
system("pause");
return0;
}
3. 怎樣在C語言中顯示時間
#include <stdio.h>
#include <time.h>
int main()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "The current date/time is: %s", asctime (timeinfo) );
return 0;
}
4. 怎樣在C語言中動態顯示時間,該怎麼處理
這里的關鍵是動態顯示,與c基本無關
如果你使用控制台,那麼可以利用退格輸出,將新的值輸出覆蓋舊的顯示,看起來就是動態的了,比如:printf("12:00:00"); 然後間隔1秒後輸出:printf("\b\b\b\b\b\b\b\b12:00:01");
5. 編寫一個簡單的C語言程序,在屏幕上顯示一行時間(包含小時、分鍾和秒鍾)的信息
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int main()
{
time_t timep,Tim;
struct tm *p;
time(&timep);
p = localtime(&timep); //此函數獲得的tm結構體的時間,是已經進行過時區轉化為本地時間
//p = gmtime(&timep); //把日期和時間轉換為格林威治(GMT)時間的函數
int Year = 1900 + p->tm_year;
int Month = 1 + p->tm_mon;
int Day = p->tm_mday;
int Hour = p->tm_hour;
int Minute = p->tm_min;
int Second = p->tm_sec;
char year[20];
char month[20];
char day[20];
char hour[20];
char minute[20];
char second[20];
printf("hour=%d\n", Hour);
printf("minute=%d\n", Minute);
printf("second=%d\n", Second);
return 0;
}
每編譯一次就會顯示這一刻的系統時間;這個程序我也不太懂,我學長講了半天,數據結構都整出來了,也沒把我整明白,我再研究研究;希望能幫到你吧;
6. c語言中什麼函數可以顯示實時時間用visualc++
主要就下面這幾個函數,會用即可。
/* #include <time.h>
庫函數
1 char *asctime(const struct tm *timeptr)
返回一個指向字元串的指針,它代表了結構 timeptr 的日期和時間。
2 clock_t clock(void)
返回程序執行起(一般為程序的開頭),處理器時鍾所使用的時間。
3 char *ctime(const time_t *timer)
返回一個表示當地時間的字元串,當地時間是基於參數 timer。
4 double difftime(time_t time1, time_t time2)
返回 time1 和 time2 之間相差的秒數 (time1-time2)。
5 struct tm *gmtime(const time_t *timer)
timer 的值被分解為 tm 結構,並用協調世界時(UTC)也被稱為格林尼治標准時間(GMT)表示。
6 struct tm *localtime(const time_t *timer)
timer 的值被分解為 tm 結構,並用本地時區表示。
7 time_t mktime(struct tm *timeptr)
把 timeptr 所指向的結構轉換為一個依據本地時區的 time_t 值。
8 size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
根據 format 中定義的格式化規則,格式化結構 timeptr 表示的時間,並把它存儲在 str 中。
9 time_t time(time_t *timer)
計算當前日歷時間,並把它編碼成 time_t 格式。
*/