c語言時間差
#include<stdio.h>
intmain()
{
inth1,s1,h2,s2,h3,s3;;
printf("Inputtimeone(hour,second):");
scanf("%d,%d",&h1,&s1);
printf("Inputtimetwo(hour,second):");
scanf("%d,%d",&h2,&s2);
h3=h1-h2;
s3=s1-s2;
if(s3<0&&h3==0)s3=-s3;
elseif(h3<0)h3=-h3,s3=-s3;
if(s3<0)h3-=1,s3+=60;
printf("%dhour%dsecond ",h3,s3);
}
B. C語言 計時函數時間差總為零
C語言中變數是有作用域的,除非定義了全局變數,一般而言不同的函數體中的變數相互不可見。
因此,主函數中的t_start與leave_car()函數中的t_start並不是同一個變數!
修改如下:
將主函數中的
time_t t_start,t_end;
t_start=time(NULL);
兩個語句去掉,因為對整個程序而言沒有實際的作用。在leave_car()中
printf("yes");
前添加
t_start=time(NULL);
另外
printf("time is %f s ",difftime(t_end,t_start);
少寫了一個反括弧
C. C語言生日到當前日期時間差計算
給個模板,造福人類
//-------------------從X年X月X日開始到X年X月X日之間多少天了
#include<stdio.h>
//給定一個年月日,計算這一年已經過的天數
inttotal_day(intyear,intmonth,intday)
{
intsum=0;
switch(month)
{
case1:sum=day;
break;
case2:sum=31+day;
break;
case3:sum=59+day;
break;
case4:sum=90+day;
break;
case5:sum=120+day;
break;
case6:sum=151+day;
break;
case7:sum=181+day;
break;
case8:sum=212+day;
break;
case9:sum=243+day;
break;
case10:sum=273+day;
break;
case11:sum=304+day;
break;
case12:sum=334+day;
break;
default:
printf("輸入的月份有錯誤 ");
break;
}
if(month>2)
{
if(((year%4==0)&&(year%100!=0))||(year%400)==0){
sum=sum+1;
}
}
returnsum;
}
//年與年相差的天數
inttotal_year_day(intyear1,intyear2)
{
intsum_year_day=0;
inti=0;
sum_year_day=(year2-year1)*365;
for(i=year1;i<year2;i++)
{
if(((i%4==0)&&(i%100!=0))||(i%400)==0){
sum_year_day=sum_year_day+1;
}
}
returnsum_year_day;
}
intmain()
{
intyear1=2013,month1=1,day1=1;
intyear2=2013,month2=1,day2=1;
intsum=0;
printf("~~~~~~計算兩天之間相差的天數~~~~~~ ");
printf("請輸入起始的年月日(格式為:XXXX:XX:XX)");
scanf("%d:%d:%d",&year1,&month1,&day1);
printf("請輸入結束的年月日(格式為:XXXX:XX:XX)");
scanf("%d:%d:%d",&year2,&month2,&day2);
sum=total_year_day(year1,year2)-total_day(year1,month1,day1)+total_day(year2,month2,day2);
printf("它們之間相差的天數為:%d ",sum);
return0;
}
D. C語言 文件中時間與當前系統時間 時間差比較(比較結果返回為天數)最好是調用子函數解決
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intdays(char*begin_time)//格式:"20180914000000"//2018-9-140點0分0秒
{
structtmtm1;
time_ttime1,time_now;
sscanf(begin_time,"%4d%2d%2d%2d%2d%2d",&tm1.tm_year,&tm1.tm_mon,&tm1.tm_mday,&tm1.tm_hour,&tm1.tm_min,&tm1.tm_sec);
tm1.tm_year-=1900;
tm1.tm_mon--;
tm1.tm_isdst=-1;
time1=mktime(&tm1);//構造time1
time_now=time(NULL);
tm1=*(localtime(&time_now));//當時日期
return(time_now-time1)/(3600*24);//返回相差天數
}
intmain()
{
chartime_in_file[30]={"20180914000000 "};
printf(" %d",days(time_in_file));
return0;
}
E. C語言怎麼把時間1和時間2換成分鍾,再計算時間差
unsigned int time1, time2, h, m;
time1 = h1 * 60 + m1;
time2 = h2 * 60 + m2;
time1 = time1 > time2 ? time1 - time2 : time2 - time1;
h = time1 / 60;
m = time1 % 60;
printf("時間差為%u小時%u分鍾\r\n", h, m);
F. 如何用c語言計算兩個時間的時間差
#include <time.h>
clock_t start; clock_t end;
start = clock();
...//需要計算時間的代碼片斷
end = clock();
printf("%ld", (end - start)/CLK_TCK/60);
G. 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 );
}
(7)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()計算兩者的差,即可得到耗費時間。
H. C語言中如何計算時間差 秒
C語言中有時間函數(time函數),可以列印出系統時間,相減就行。當然,也有各類延時函數。sleep族函數。
I. c語言如何計算時間差
boolcomputer(file_date_tt1,file_date_tt2)
{
intmin=t1.i_dd<t2.i_dd?t1.i_dd:t2.i_dd;
inttime1=(t1.i_dd-min)*24+t1.i_hh;
inttime2=(t2.i_dd-min)*24+t2.i_hh;
if(time1>time2)
{
if(time1-time2>12)
{
printf("時間超過12個小時! ");
returntrue;
}
printf("時間不超過12個小時! ");
returnfalse;
}
else
{
if(time2-time1>12)
{
printf("時間超過12個小時! ");
returntrue;
}
printf("時間不超過12個小時! ");
returnfalse;
}
}