当前位置:首页 » 编程语言 » 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);

}