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

創(chuàng)新互聯(lián)是一家專業(yè)提供中站企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、成都做網(wǎng)站、H5建站、小程序制作等業(yè)務(wù)。10年已為中站眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
#includestdio.h
#includestring.h
struct?TTime
{
int?h,m,s;
long?GetSec(){return?3600L*h+60*m+s;}
void?StrToTime(char?_str[])
{
int?i,j,len=strlen(_str);
/*去空格*/
for(i=0;ilen;++i)
if(_str[i]=='?')
{
for(j=i;jlen-1;++j)
_str[j]=_str[j+1];
--len;
i=-1;
continue;
}
/*讀小時*/
j=0;
for(i=0;ilen;++i)
if(_str[i]==':')
break;
else
j=j*10?+?_str[i]-'0';
h?=?j;
/*讀分鐘*/
j=0;
for(++i;ilen;++i)
if(_str[i]==':')
break;
else
j=j*10?+?_str[i]-'0';
m?=?j;
/*讀秒*/
j=0;
for(++i;ilen;++i)
j=j*10?+?_str[i]-'0';
s?=?j;
}
void?ToPlan(long?t)
{
int?hh,mm,ss;
hh?=?t/3600;
t%=3600;
mm?=?t/60;
t%=60;
ss=t;
printf("%2.2d:%2.2d:%2.2d\n",hh,mm,ss);
}
}Ta,Tb,Tc;
void?main()
{
char?a[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());
}
/**
time.c
定義一個結(jié)構(gòu)體實現(xiàn)兩個時間的加減
*/
#includestdio.h
#includestring.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);//默認(rèn)第一個時間比第二個大
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("-----輸入時間格式錯誤!請重新輸入\n");
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("-----輸入時間格式錯誤!請重新輸入\n");
again=1;
}
else
again=0;
}
swap(time1,time2);
printf("? ? ? ? ? ?");
printTime(time1);
printf(" - ");
printTime(time2);
time3=subtract(time1,time2);
printf(" = ");
printTime(time3);
printf("\n");
printf("繼續(xù)[y/n]?:");
scanf("%s",againch);
}
return 0;
}
//檢查時間的格式
int checkTime(Time time)
{
//? ? printf("小時格式錯誤:%d\n",(time.hours=24||time.hours0));
//? ? printf("分鐘格式錯誤:%d\n",(time.minutes=60||time.minutes0));
//? ? printf("秒格式錯誤? :%d\n",(time.seconds=60||time.minutes0));
return ((time.hours24||time.hours0)||(time.minutes=60||time.minutes0)||(time.seconds=60||time.minutes0));
}
//輸出按個數(shù)輸出時間
void printTime(Time time)
{
printf("%d:%d:%d",time.hours,time.minutes,time.seconds);
}
//大的時間放到第一個變量,小的時間方法哦第二個變量
void swap(Time *time1,Time *time2)
{
//保證第一個時間永遠(yuǎn)大于第二個時間
if(time2-hourstime1-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-minutestime1-minutestime1-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-secondstime1-secondstime1-minutes==time2-minutes)
{
//交換兩個時間的秒:
time1-seconds^=time2-seconds;
time2-seconds^=time1-seconds;
time1-seconds^=time2-seconds;
}
}
//計算兩個時間的差
Time subtract(Time *first,Time *second)//默認(rèn)第一個時間比第二個大
{
Time result;
//先對秒進(jìn)行相減
if(first-seconds=second-seconds)//如果第一個秒大于或者等于
{
result.seconds=first-seconds-second-seconds;
}
else//如果第一個的秒數(shù)小的話
{
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//如果第一個的秒數(shù)小的話
{
first-hours=first-hours-1;//借位
first-minutes=first-minutes+60;
result.minutes=first-minutes-second-minutes;
}
//交換后 默認(rèn)第一個小時會大于第一個,沒有借位的情況,不用
result.hours=first-hours-second-hours;
return result;
拓展資料
C語言是一門通用計算機(jī)編程語言,應(yīng)用廣泛。C語言的設(shè)計目標(biāo)是提供一種能以簡易的方式編譯、處理低級存儲器、產(chǎn)生少量的機(jī)器碼以及不需要任何運(yùn)行環(huán)境支持便能運(yùn)行的編程語言。
盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平臺的特性,以一個標(biāo)準(zhǔn)規(guī)格寫出的C語言程序可在許多電腦平臺上進(jìn)行編譯,甚至包含一些嵌入式處理器(單片機(jī)或稱MCU)以及超級電腦等作業(yè)平臺。
二十世紀(jì)八十年代,為了避免各開發(fā)廠商用的C語言語法產(chǎn)生差異,由美國國家標(biāo)準(zhǔn)局為C語言制定了一套完整的美國國家標(biāo)準(zhǔn)語法,稱為ANSI C,作為C語言最初的標(biāo)準(zhǔn)。目前2011年12月8日,國際標(biāo)準(zhǔn)化組織(ISO)和國際電工委員會(IEC)發(fā)布的C11標(biāo)準(zhǔn)是C語言的第三個官方標(biāo)準(zhǔn),也是C語言的最新標(biāo)準(zhǔn),該標(biāo)準(zhǔn)更好的支持了漢字函數(shù)名和漢字標(biāo)識符,一定程度上實現(xiàn)了漢字編程。
C語言中有時間函數(shù)(time函數(shù)),可以打印出系統(tǒng)時間,相減就行。當(dāng)然,也有各類延時函數(shù)。sleep族函數(shù)。
#include stdio.h
#include time.h
int main(void)
{
struct tm time_cha;
time_t t1,t2;
long int d;
int year, month, day,hour,min,sec;
scanf("%4d%2d%2d%2d%2d%2d",year,month,day,hour,min,sec);
time_cha.tm_year = year - 1900; //tm結(jié)構(gòu)記錄年為實際-1900
time_cha.tm_mon = month - 1;
time_cha.tm_mday = day;
time_cha.tm_hour = hour;
time_cha.tm_min = min;
time_cha.tm_sec = sec;
t1 = mktime(time_cha);//獲得從1970年1月1日0時0分0秒以來過去的時間,秒
scanf("%4d%2d%2d%2d%2d%2d",year,month,day,hour,min,sec);
time_cha.tm_year = year - 1900; //tm結(jié)構(gòu)記錄年為實際-1900
time_cha.tm_mon = month - 1;
time_cha.tm_mday = day;
time_cha.tm_hour = hour;
time_cha.tm_min = min;
time_cha.tm_sec = sec;
t2 = mktime(time_cha);
d=t1-t2;//經(jīng)過的時間差(秒)
printf("%ld\n", d);
return 0;
}
分享名稱:時間差的秒數(shù)c語言函數(shù),計算天數(shù)和秒數(shù)c語言
轉(zhuǎn)載源于:http://chinadenli.net/article13/dseosds.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、搜索引擎優(yōu)化、網(wǎng)站制作、電子商務(wù)、品牌網(wǎng)站設(shè)計、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)