❶ C語言 終止時間的計算
minute=start % 100;
hour=start / 100;
minute=c % 60;
final=hour*100+minute;
應該是這樣了
❷ 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語言clock()函數如何統計輸入結束到程序運行結束的時間本人是菜鳥,C語言剛入門。
clock_t tb,te;
tb=clock();//開始計時
............
te=clock();//結束計時
printf("%lf毫秒",(tb-te)/CLK_TCK);
❹ 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 );
}
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語言怎麼編:編寫一個程序,計算用戶輸入的起始時間和終止時間之間的相距天數
你把這個程序的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語言然後是幾點
供參考 不懂追問
#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語言基礎
你好,其實這個很簡單,一分鍾就寫好了,希望能幫到你。
望採納!
#include<stdio.h>
intmain()
{
inttime,num;
while(scanf("%d%d",&time,&num))
{
intshi=time/100;
intfen=time%100;
if(num>=0)
{
for(inti=1;i<=num;++i)
{
fen++;
if(fen==60)
{
shi++;
fen=0;
}
}
}
if(num<0)
{
for(inti=1;i<=num;++i)
{
fen--;
if(fen<=0)
{
shi--;
fen=60;
}
}
}
if(fen==60)
{
shi++;
fen=0;
}
printf("%d%d ",shi,fen);
}
}
❽ C語言運行一條語句所用時間如何計算
把time.h
include進來
然後在代碼的前面和後面都加一條代碼,獲得時間
long
start=clock();
long
end=clock();
兩個減一下就是秒數
❾ C語言怎麼計算下面的程序的執行時間
51單片機在晶振12M下,從模擬上來看大概是1ms(模擬的單位是s),具體時間可以用編個程序運行,
while(1)
{
delay();
P1=~P1;
}
然後用示波器觀察P1口波形周期
❿ 在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));