#include stdio.h

創(chuàng)新互聯(lián)是一家專業(yè)提供衛(wèi)東企業(yè)網(wǎng)站建設(shè),專注與做網(wǎng)站、成都做網(wǎng)站、H5頁(yè)面制作、小程序制作等業(yè)務(wù)。10年已為衛(wèi)東眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計(jì)公司優(yōu)惠進(jìn)行中。
#include stdlib.h
#include time.h
void main()
{
unsigned char time1[] = {?10, 8, 31, 9, 26 };
unsigned char time2[] = { 10, 8, 31, 9, 50 };
struct tm t1 = {0};
struct tm t2 = {0};
time_t _t1;
time_t _t2;
double diff;
t1.tm_year = time1[0] + 100;
t1.tm_mon = time1[1];
t1.tm_mday = time1[2];
t1.tm_hour = time1[3];
t1.tm_min = time1[4];
t2.tm_year = time2[0] + 100;
t2.tm_mon = time2[1];
t2.tm_mday = time2[2];
t2.tm_hour = time2[3];
t2.tm_min = time2[4];
_t1 = _mkgmtime( t1 );
_t2 = _mkgmtime( t2 );
diff = difftime(_t2, _t1 );
printf( "相差 %.0f 分鐘\n", diff / 60 );
}
擴(kuò)展資料:
C語(yǔ)言中有兩個(gè)相關(guān)的函數(shù)用來計(jì)算時(shí)間差,分別是:
time_t time( time_t *t)? ?與 clock_t clock(void)
頭文件: time.h
計(jì)算的時(shí)間單位分別為: s? ?, ms
time_t 和 clock_t 是函數(shù)庫(kù)time.h 中定義的用來保存時(shí)間的數(shù)據(jù)結(jié)構(gòu)
返回值:
1、time? : 返回從公元1970年1月1號(hào)的UTC時(shí)間從0時(shí)0分0秒算起到現(xiàn)在所經(jīng)過的秒數(shù)。如果參數(shù) t 非空指針的話,返回的時(shí)間會(huì)保存在 t 所指向的內(nèi)存。
2、clock:返回從“開啟這個(gè)程序進(jìn)程”到“程序中調(diào)用clock()函數(shù)”時(shí)之間的CPU時(shí)鐘計(jì)時(shí)單元(clock tick)數(shù)。? ? ?1單元 = 1 ms。
所以我們可以根據(jù)具體情況需求,判斷采用哪一個(gè)函數(shù)。
具體用法如下例子:
#include time.h
#include stdio.h
#include stdlib.h
int main()
{
time_t c_start, t_start, c_end, t_end;
c_start = clock();? ? //! 單位為ms
t_start = time(NULL);? //! 單位為s
system("pause");
c_end? ?= clock();
t_end = time(NULL);
//!difftime(time_t, time_t)返回兩個(gè)time_t變量間的時(shí)間間隔,即時(shí)間差
printf("The pause used %f ms by clock()\n",difftime(c_end,c_start));
printf("The pause used %f s by time()\n",difftime(t_end,t_start));
system("pause");
return 0;
}
因此,要計(jì)算某一函數(shù)塊的占用時(shí)間時(shí),只需要在執(zhí)行該函數(shù)塊之前和執(zhí)行完該函數(shù)塊之后調(diào)用同一個(gè)時(shí)間計(jì)算函數(shù)。再調(diào)用函數(shù)difftime()計(jì)算兩者的差,即可得到耗費(fèi)時(shí)間。
標(biāo)準(zhǔn)庫(kù)的time.h里有時(shí)間函數(shù)
time_t time (time_t *timer)
計(jì)算從1970年1月1日到當(dāng)前系統(tǒng)時(shí)間,并把結(jié)果返回給timer變量,
函數(shù)本身返回的也是這個(gè)結(jié)果.time_t這個(gè)類型其實(shí)就是一個(gè)int.
另有:
double difftime ( time_t timer2, time_t timer1 )
把返回time2和time1所儲(chǔ)存的時(shí)間的差.
希望能夠我的思路可以幫助你:
①如果password="124567"時(shí),歡迎進(jìn)入!
②如果password != "124567"時(shí),等待15分鐘!
③等待15分鐘后返回重新輸入密碼!
#include stdio.h
#include string.h
#includewindows.h
int main()
{
char str[20], password;
int x,i;
//執(zhí)行4次循環(huán)0,1,2,3
for(x=0; x=3 strcmp(str,"1234567")!=0; x++)
{
printf("Enter password please:");
scanf("%s",str);
//當(dāng)密碼錯(cuò)誤時(shí)提示輸入錯(cuò)誤!
if(strcmp(str,"1234567")!=0)
{
printf("Input error!\n");
}
//當(dāng)錯(cuò)誤了3次時(shí)執(zhí)行等待,并重置x的初值
if(x==2)
{
printf("Please wait another 15 min.");
for(i=0;i=(15*60);i++)
Sleep(1000); //停滯一秒
//重置x的初值
x=0;
}
else
//密碼輸入正確時(shí)跳出循環(huán),執(zhí)行for循環(huán)之外的語(yǔ)句
{
if(strcmp(str,"1234567")==0)
printf("Welcome\n");
break;
}
}
//可以插入驗(yàn)證后要執(zhí)行的代碼
return 0;
}
c語(yǔ)言時(shí)間函數(shù):
1、獲得日歷時(shí)間函數(shù):
可以通過time()函數(shù)來獲得日歷時(shí)間(Calendar Time),其原型為:time_t time(time_t * timer);
如果已經(jīng)聲明了參數(shù)timer,可以從參數(shù)timer返回現(xiàn)在的日歷時(shí)間,同時(shí)也可以通過返回值返回現(xiàn)在的日歷時(shí)間,即從一個(gè)時(shí)間點(diǎn)(例如:1970年1月1日0時(shí)0分0秒)到現(xiàn)在此時(shí)的秒數(shù)。如果參數(shù)為空(NUL),函數(shù)將只通過返回值返回現(xiàn)在的日歷時(shí)間,比如下面這個(gè)例子用來顯示當(dāng)前的日歷時(shí)間:
2、獲得日期和時(shí)間函數(shù):
這里說的日期和時(shí)間就是平時(shí)所說的年、月、日、時(shí)、分、秒等信息。從第2節(jié)我們已經(jīng)知道這些信息都保存在一個(gè)名為tm的結(jié)構(gòu)體中,那么如何將一個(gè)日歷時(shí)間保存為一個(gè)tm結(jié)構(gòu)的對(duì)象呢?
其中可以使用的函數(shù)是gmtime()和localtime(),這兩個(gè)函數(shù)的原型為:
struct tm * gmtime(const time_t *timer);
struct tm * localtime(const time_t * timer);
其中g(shù)mtime()函數(shù)是將日歷時(shí)間轉(zhuǎn)化為世界標(biāo)準(zhǔn)時(shí)間(即格林尼治時(shí)間),并返回一個(gè)tm結(jié)構(gòu)體來保存這個(gè)時(shí)間,而localtime()函數(shù)是將日歷時(shí)間轉(zhuǎn)化為本地時(shí)間。比如現(xiàn)在用gmtime()函數(shù)獲得的世界標(biāo)準(zhǔn)時(shí)間是2005年7月30日7點(diǎn)18分20秒,那么用localtime()函數(shù)在中國(guó)地區(qū)獲得的本地時(shí)間會(huì)比世界標(biāo)準(zhǔn)時(shí)間晚8個(gè)小時(shí),即2005年7月30日15點(diǎn)18分20秒。
方法一,#includetime.h
int main()
{
time_t timep;
struct tm *p;
time (timep);
p=gmtime(timep);
printf("%d\n",p-tm_sec); /*獲取當(dāng)前秒*/
printf("%d\n",p-tm_min); /*獲取當(dāng)前分*/
printf("%d\n",8+p-tm_hour);/*獲取當(dāng)前時(shí),這里獲取西方的時(shí)間,剛好相差八個(gè)小時(shí)*/
printf("%d\n",p-tm_mday);/*獲取當(dāng)前月份日數(shù),范圍是1-31*/
printf("%d\n",1+p-tm_mon);/*獲取當(dāng)前月份,范圍是0-11,所以要加1*/
printf("%d\n",1900+p-tm_year);/*獲取當(dāng)前年份,從1900開始,所以要加1900*/
printf("%d\n",p-tm_yday); /*從今年1月1日算起至今的天數(shù),范圍為0-365*/
}
方法二.#include?stdio.h
#include?time.h
int?main?()
{
time_t?t
struct?tm?*?lt;????time?(t);//獲取Unix時(shí)間戳。
lt?=?localtime?(t);//轉(zhuǎn)為時(shí)間結(jié)構(gòu)。
printf?(?"%d/%d/%d?%d:%d:%d\n",lt-tm_year+1900,?lt-tm_mon,?lt-tm_mday,
lt-tm_hour,?lt-tm_min,?lt-tm_sec);//輸出結(jié)果
return?0;}
擴(kuò)展資料
1、CTimeSpan類
如果想計(jì)算兩段時(shí)間的差值,可以使用CTimeSpan類,具體使用方法如下:
CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //計(jì)算當(dāng)前系統(tǒng)時(shí)間與時(shí)間t1的間隔
int iDay=span.GetDays(); //獲取這段時(shí)間間隔共有多少天
int iHour=span.GetTotalHours(); //獲取總共有多少小時(shí)
int iMin=span.GetTotalMinutes();//獲取總共有多少分鐘
int iSec=span.GetTotalSeconds();//獲取總共有多少秒
2、timeb()函數(shù)
_timeb定義在SYS\TIMEB.H,有四個(gè)fields
dstflag
millitm
time
timezone
void _ftime( struct _timeb *timeptr );
struct _timeb timebuffer;
_ftime( timebuffer );
參考資料來源:百度百科:time函數(shù)
網(wǎng)頁(yè)題目:估算時(shí)間的函數(shù)c語(yǔ)言,c++計(jì)算時(shí)間函數(shù)
本文鏈接:http://chinadenli.net/article14/hcpsde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、定制網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站排名、搜索引擎優(yōu)化、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)