當前位置:首頁 » 編程語言 » c語言求時間相差
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言求時間相差

發布時間: 2022-10-20 06:42:31

A. 【急求】c語言 求兩個時間的差值

/*可以處理空格!!!*/

#include<stdio.h>

#include<string.h>

structTTime

{

inth,m,s;

longGetSec(){return3600L*h+60*m+s;}

voidStrToTime(char_str[])

{

inti,j,len=strlen(_str);

/*去空格*/

for(i=0;i<len;++i)

if(_str[i]=='')

{

for(j=i;j<len-1;++j)

_str[j]=_str[j+1];

--len;

i=-1;

continue;

}

/*讀小時*/

j=0;

for(i=0;i<len;++i)

if(_str[i]==':')

break;

else

j=j*10+_str[i]-'0';

h=j;

/*讀分鍾*/

j=0;

for(++i;i<len;++i)

if(_str[i]==':')

break;

else

j=j*10+_str[i]-'0';

m=j;

/*讀秒*/

j=0;

for(++i;i<len;++i)

j=j*10+_str[i]-'0';

s=j;

}

voidToPlan(longt)

{

inthh,mm,ss;

hh=t/3600;

t%=3600;

mm=t/60;

t%=60;

ss=t;

printf("%2.2d:%2.2d:%2.2d ",hh,mm,ss);

}

}Ta,Tb,Tc;

voidmain()

{

chara[105],b[105];

gets(a);

gets(b);

Ta.StrToTime(a);

Tb.StrToTime(b);

printf("sec:%ld,time:",Tb.GetSec()-Ta.GetSec());

Tc.ToPlan(Tb.GetSec()-Ta.GetSec());

}

B. 如何用c語言計算兩個時間的時間差

#include
<time.h>
clock_t
start;
clock_t
end;
start
=
clock();
...//需要計算時間的代碼片斷
end
=
clock();
printf("%ld",
(end
-
start)/clk_tck/60);

C. C語言中計算2個時間的差值的函數

#include<time.h>
#include<stdio.h>

time_t_mktime(char*slTime)/**yyyy-mm-dd**/
{
structtmtm_t;
intyear;
intmon;
intday;

sscanf(slTime,"%4d-%2d-%2d",&year,&mon,&day);

tm_t.tm_year=year-1900;
tm_t.tm_mon=mon-1;
tm_t.tm_mday=day;
tm_t.tm_hour=12;
tm_t.tm_min=00;
tm_t.tm_sec=01;
tm_t.tm_wday=0;
tm_t.tm_yday=0;
tm_t.tm_isdst=0;

returnmktime(&tm_t);
}

intdaydiff(char*date1,char*date2)/**yyyy-mm-dd**/
{
time_tt1=_mktime(date1);
time_tt2=_mktime(date2);
time_tdiff=abs(t2-t1);
return(int)(diff/(24*60*60));
}
intmain()
{
chardate1[12],date2[12];
printf("inputdate1:");
scanf("%s",date1);
fflush(stdin);
printf("inputdate2:");
scanf("%s",date2);
fflush(stdin);
printf("%s-%sis%ddays ",date1,date2,daydiff(date1,date2));
}

D. C語言輸入兩個時間(同一天的兩個時和分),計算其時間差,輸出相差幾小時幾分鍾

/**

time.c

定義一個結構體實現兩個時間的加減

*/

#include<stdio.h>

#include<string.h>

typedef struct

{

int seconds;

int minutes;

int hours;

}Time;

int checkTime(Time time);

void printTime(Time time);

void swap(Time *time1,Time *time2);//大的時間放在前面

Time subtract1(Time *first,Time *second);

Time subtract(Time *first,Time *second);//默認第一個時間比第二個大

int main()

{

Time time1;

Time time2;

Time time3;

char againch[5]="y";

while(strcmp(againch,"y")==0||strcmp(againch,"Y")==0)

{

int again=1;

while(again)

{

printf("輸入時間1:");

scanf("%d:%d:%d",&time1.hours,&time1.minutes,&time1.seconds);

if(checkTime(time1))

{

printf("-----輸入時間格式錯誤!請重新輸入 ");

again=1;

}

else

again=0;

}

again=1;

while(again)

{

printf("輸入時間2:");

scanf("%d:%d:%d",&time2.hours,&time2.minutes,&time2.seconds);

if(checkTime(time2))

{

printf("-----輸入時間格式錯誤!請重新輸入 ");

again=1;

}

else

again=0;

}

swap(&time1,&time2);

printf(" ");

printTime(time1);

printf(" - ");

printTime(time2);

time3=subtract(&time1,&time2);

printf(" = ");

printTime(time3);

printf(" ");

printf("繼續[y/n]?:");

scanf("%s",againch);

}

return 0;

}

//檢查時間的格式

int checkTime(Time time)

{

// printf("小時格式錯誤:%d ",(time.hours>=24||time.hours<0));

// printf("分鍾格式錯誤:%d ",(time.minutes>=60||time.minutes<0));

// printf("秒格式錯誤 :%d ",(time.seconds>=60||time.minutes<0));

return ((time.hours>24||time.hours<0)||(time.minutes>=60||time.minutes<0)||(time.seconds>=60||time.minutes<0));

}

//輸出按個數輸出時間

void printTime(Time time)

{

printf("%d:%d:%d",time.hours,time.minutes,time.seconds);

}

//大的時間放到第一個變數,小的時間方法哦第二個變數

void swap(Time *time1,Time *time2)

{

//保證第一個時間永遠大於第二個時間

if(time2->hours>time1->hours)//如果有time

{

//交換兩個時間的小時

time2->hours^=time1->hours;

time1->hours^=time2->hours;

time2->hours^=time1->hours;

//交換兩個時間的分鍾:

time1->minutes^=time2->minutes;

time2->minutes^=time1->minutes;

time1->minutes^=time2->minutes;

//交換兩個時間的秒:

time1->seconds^=time2->seconds;

time2->seconds^=time1->seconds;

time1->seconds^=time2->seconds;

}

else if(time2->minutes>time1->minutes&&time1->hours==time2->hours)

{

//交換兩個時間的分鍾:

time1->minutes^=time2->minutes;

time2->minutes^=time1->minutes;

time1->minutes^=time2->minutes;

//交換兩個時間的秒:

time1->seconds^=time2->seconds;

time2->seconds^=time1->seconds;

time1->seconds^=time2->seconds;

}

else if(time2->seconds>time1->seconds&&time1->minutes==time2->minutes)

{

//交換兩個時間的秒:

time1->seconds^=time2->seconds;

time2->seconds^=time1->seconds;

time1->seconds^=time2->seconds;

}

}

//計算兩個時間的差

Time subtract(Time *first,Time *second)//默認第一個時間比第二個大

{

Time result;

//先對秒進行相減

if(first->seconds>=second->seconds)//如果第一個秒大於或者等於

{

result.seconds=first->seconds-second->seconds;

}

else//如果第一個的秒數小的話

{

first->minutes=first->minutes-1;//借位

first->seconds=first->seconds+60;

result.seconds=first->seconds-second->seconds;

}

//接著對分鍾相減

if(first->minutes>=second->minutes)//如果第一個秒大於或者等於

{

result.minutes=first->minutes-second->minutes;

}

else//如果第一個的秒數小的話

{

first->hours=first->hours-1;//借位

first->minutes=first->minutes+60;

result.minutes=first->minutes-second->minutes;

}

//交換後 默認第一個小時會大於第一個,沒有借位的情況,不用

result.hours=first->hours-second->hours;

return result;

E. C語言中如何計算時間差 秒

C語言中有時間函數(time函數),可以列印出系統時間,相減就行。當然,也有各類延時函數。sleep族函數。

F. 如何用c語言計算兩個時間的時間差

#include <time.h>
clock_t start; clock_t end;
start = clock();
...//需要計算時間的代碼片斷
end = clock();
printf("%ld", (end - start)/CLK_TCK/60);

G. 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;

}

}

H. c語言中,如何比較兩個時間相差幾天

計算兩個年月日之間的天數,思路是分別算出日期的總天數然後相減。

要考慮閏年的情況,判斷閏年的口訣:4年一閏,100年不閏,400年再閏。

((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)

網上找了一個(偷懶= =!),修改下如下:

#include <stdio.h>

int sum(int y,int m,int d)

{

unsigned char x[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int i,s=0;

for(i=1;i<y;i++)

if(i%4==0 && i%100!=0 || i%400==0)

s+=366;//閏年

else

s+=365;//平年

if(y%4==0 && y%100!=0 || y%400==0)

x[2]=29;

for(i=1;i<m;i++)

s+=x[i];//整月的天數

s+=d;//日的天數

return s;//返回總天數,相對公元1年

}

void main()

{

unsigned char y1,m1,d1,y2,m2,d2;

int s1,s2;

printf("輸入第一個年 月 日:");

scanf("%d %d %d",&y1,&m1,&d1);

printf("輸入第二個年 月 日:");

scanf("%d %d %d",&y2,&m2,&d2);

s1=sum(y1,m1,d1);

s2=sum(y2,m2,d2);

if (s1 > s2)

printf("相差天數:%ld ",s1-s2);

else

printf("相差天數:%ld ",s2-s1);

}