『壹』 c語言編程,怎麼計算時間
#include <stdio.h>
#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 分鍾
", diff / 60 );
}
(1)c語言終止時間的計算方法擴展閱讀:
C語言中有兩個相關的函數用來計算時間差,分別是:
time_t time( time_t *t) 與 clock_t clock(void)
頭文件: time.h
計算的時間單位分別為: s , ms
time_t 和 clock_t 是函數庫time.h 中定義的用來保存時間的數據結構
返回值:
1、time : 返回從公元1970年1月1號的UTC時間從0時0分0秒算起到現在所經過的秒數。如果參數 t 非空指針的話,返回的時間會保存在 t 所指向的內存。
2、clock:返回從「開啟這個程序進程」到「程序中調用clock()函數」時之間的CPU時鍾計時單元(clock tick)數。 1單元 = 1 ms。
所以我們可以根據具體情況需求,判斷採用哪一個函數。
具體用法如下例子:
#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)返回兩個time_t變數間的時間間隔,即時間差
printf("The pause used %f ms by clock() ",difftime(c_end,c_start));
printf("The pause used %f s by time() ",difftime(t_end,t_start));
system("pause");
return 0;
}
因此,要計算某一函數塊的佔用時間時,只需要在執行該函數塊之前和執行完該函數塊之後調用同一個時間計算函數。再調用函數difftime()計算兩者的差,即可得到耗費時間。
『貳』 在c語言里想要獲得程序運行的開始時間和結束時間,怎麼寫
這樣干,你少寫了兩行
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf("程序運行開始,Currentlocal time and date: %s\n", asctime(timeinfo));
……
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf("程序運行結束,Currentlocal time and date: %s", asctime(timeinfo));
『叄』 C語言編程 關於計算時間的問題 望高手解答!
希望能夠我的思路可以幫助你:
①如果password="124567"時,歡迎進入!
②如果password != "124567"時,等待15分鍾!
③等待15分鍾後返回重新輸入密碼!
#include <stdio.h>
#include <string.h>
#include<windows.h>
int main()
{
char str[20], password;
int x,i;
//執行4次循環0,1,2,3
for(x=0; x<=3 && strcmp(str,"1234567")!=0; x++)
{
printf("Enter password please:");
scanf("%s",&str);
//當密碼錯誤時提示輸入錯誤!
if(strcmp(str,"1234567")!=0)
{
printf("Input error!\n");
}
//當錯誤了3次時執行等待,並重置x的初值
if(x==2)
{
printf("Please wait another 15 min.");
for(i=0;i<=(15*60);i++)
Sleep(1000); //停滯一秒
//重置x的初值
x=0;
}
else
//密碼輸入正確時跳出循環,執行for循環之外的語句
{
if(strcmp(str,"1234567")==0)
printf("Welcome\n");
break;
}
}
//可以插入驗證後要執行的代碼
return 0;
}
『肆』 C語言運行一條語句所用時間如何計算
unsigned long HighStart,LowStart,HighEnd,LowEnd;
__int64 start =0;
__int64 end = 0;
__int64 timer =0;
//獲取代碼運行開始時cpu內部計數器的值
__asm
{
RDTSC
mov HighStart, edx
mov LowStart, eax
}
for(int i= 0; i<100000; i++ )
{
for(int i= 0; i<100000; i++ )
{
}
}
//獲取代碼結束時cpu內部計數器的值,並減去初值
__asm
{
RDTSC
mov HighEnd, edx
mov LowEnd, eax
}
start = (__int64) HighStart<<32;
start |= (__int64) LowStart;
end = (__int64) HighEnd<<32;
end |= (__int64) LowEnd;
timer = end - start;
//輸出代碼段運行的時鍾周期數
//以頻率1.1Gcpu為例,如果換計算機把其中的2.6改乘其它即可,因為相信大家的cpu都應該在1G以上 ^_^
cout<< (double) (timer /2.6/1000000000) << endl;
return 0;
『伍』 C語言計算時間
在C語言中計算時間,可以使用標准庫中的計時函數——clock()。
函數原型:
clock_tclock(void);
其中clock_t是用來保存時間的數據類型,在time.h文件中,可以找到對它的定義:
#ifndef_CLOCK_T_DEFINED
typedeflongclock_t;
#define_CLOCK_T_DEFINED
#endif
很明顯,clock_t是一個長整形數。在time.h文件中,還定義了一個常量CLOCKS_PER_SEC,它用來表示一秒鍾會有多少個時鍾計時單元,其定義如下:
#defineCLOCKS_PER_SEC((clock_t)1000)
可以看到每過千分之一秒(1毫秒),調用clock()函數返回的值就加1。下面舉個例子,可以使用公式clock()/CLOCKS_PER_SEC來計算一個進程自身的運行時間:
voidelapsed_time()
{
printf("Elapsedtime:%usecs. ",clock()/CLOCKS_PER_SEC);
}
當然,也可以用clock函數來計算的機器運行一個循環或者處理其它事件到底花了多少時間:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intmain(void)
{
longi=10000000L;
clock_tstart,finish;
doubleration;
printf("Timetodo%ldemptyloopsis",i);
start=clock();
while(i--);
finish=clock();
ration=(double)(finish-start)/CLOCKS_PER_SEC;
printf("%fseconds ",ration);
system("pause");
}
『陸』 怎麼用C語言計算過程運行時間具體點,文件頭,等等...
//vc環境
<time.h>
clock_t tb,te;
tb=clock(NULL);
for(int i=0;i<1000000;i++)
for(int j=0;j<1000000;j++)
;
te=clock(NULL);
cout << (te-tb)/CLK_TK << "毫秒"
『柒』 這個題目用c語言怎麼編:編寫一個程序,計算用戶輸入的起始時間和終止時間之間的相距天數
你把這個程序的birth都改成start就行了
其實不改也無妨
#include <stdio.h>
//判斷是否閏年
bool IsLeapYear( int year )
{
return ( year % 400==0 || ( year %4==0 && year %100 !=0 ) );
}
//獲取某年中某月的天數
int GetDaysOfMonth( int year, int month )
{
int day = 31;
if ( month >12 || month < 0 )
{
return 0;
}
switch ( month )
{
case 4:
case 6:
case 9:
case 11:
day=30;
break;
case 2:
day = ( IsLeapYear( year) ) ? 29 : 28;
break;
default:
break;
}
return day;
}
//計算兩個日期間的天數
void FunctionFour( int birthYear, int birthMonth, int birthDay, int year, int month, int day )
{
int iResult=0;
for ( int i=birthYear; i<=year; i++ )
{
iResult += ( ( IsLeapYear(i) ) ? 366 : 365);
}
for ( int i=1; i<birthMonth; i++ )
{
iResult -= GetDaysOfMonth( birthYear, i );
}
for ( int i=1; i < birthDay; i++ )
{
iResult--;
}
for ( int i=month; i <=12; i++ )
{
iResult -= GetDaysOfMonth( year, i );
}
for ( int i=1; i < day; i++ )
{
iResult++;
}
printf("\n%d年%d月%日-%d年%d月%d日 = %d天\n", year, month, day, birthYear, birthMonth, birthDay );
}
int main()
{
char ch;
int year, month, day;
int birthYear, birthMonth, birthDay;
printf( "請輸入你的生日(年月日, 以空格分隔):");
scanf( "%d%d%d", &birthYear, &birthMonth, &day );
printf( "\n請輸入計算日期(年月日, 以空格分隔):");
scanf( "%d%d%d", &year, &month, &birthDay );
FunctionFour( birthYear, birthMonth, birthDay, year, month, day);
printf( "Any key to exit...");
scanf( "%c", &ch);
return 0;
}
『捌』 計算C語言程序運行時間(hello world)
#include "time.h"
#include "stdio.h"
main()
{
double start, finish;
start = clock();//取開始時間
printf("Hello, World!\n");
finish = clock();//取結束時間
printf( "%f seconds\n",(finish - start) / CLOCKS_PER_SEC);//以秒為單位顯示之
}
上面的代碼理論上是可以顯示printf("Hello, World!\n");語句的運行時間的,但我猜實際的顯示結果是0,因為printf("Hello, World!\n");這個語句的運行時間是可以忽略不計的,加一個次數較多的循環才能看到效果
『玖』 C語言然後是幾點
供參考 不懂追問
#include<stdio.h>
intmain()
{
intstart,end;
intstart_min,end_min;
intpassed;
scanf("%d%d",&start,&passed);//輸入
start_min=start/100*60+start%100;
//計算start表示的時間距離00:00有多少分鍾
end_min=start_min+passed;
end=end_min/60*100+end_min%60;//上面的逆操作原理相同
printf("%d ",end);
}
『拾』 C語言 終止時間的計算
minute=start % 100;
hour=start / 100;
minute=c % 60;
final=hour*100+minute;
應該是這樣了