#includestdio.h
創(chuàng)新互聯建站專業(yè)為企業(yè)提供石龍網站建設、石龍做網站、石龍網站設計、石龍網站制作等企業(yè)網站建設、網頁設計與制作、石龍企業(yè)網站模板建站服務,10多年石龍做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。
int days(int y,int m,int d)
{int i,a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(y1||m1||d1||da[m]+(y%4==0y%1000||y%400==0))
{printf("%d-%d-%d不是一個有效的日期!\n",y,m,d);
return -1;
}
for(i=1;im;i++)
d+=a[i];
return d;
}
int main()
{int year,month,day,answer;
printf("請輸入一個日期yyyy-mm-dd\n");
scanf("%d-%d-%d",year,month,day);
answer=days(year,month,day);
if(answer0)
printf("%d-%d-%d 是該年的第 %d 天.\n",year,month,day,answer);
getch();
return 0;
}
int?GetMonthDays(int?year,int?month)
{
switch(month)
{
case?1:
case?3:
case?5:
case?7:
case?8:
case?10:
case?12:
return?31;
break;
case?4:
case?6:
case?9:
case?11:
return?30;
break;
case?2:
if((year%4==0??year%100!=0?)?||?(year%400==0))
{
return?29;
}
else
{
return?28;
}
break;
default:
printf("month?error!");
return?0;
break;
}
}
#include?stdio.h
#include?stdlib.h
#include?string.h
#include?math.h
#include?time.h
int??get_days(const?char*?from,?const?char*?to);
time_t?convert(int?year,int?month,int?day);
int?main()
{
const?char*?from="2013-3-15";
const?char*?to="2015-8-14";
int?days=get_days(from,to);
printf("From:%s\nTo:%s\n",from,to);
printf("%d\n",days);
return?0;
}
time_t?convert(int?year,int?month,int?day)
{
struct?tm?info={0};
info.tm_year=year-1900;
info.tm_mon=month-1;
info.tm_mday=day;
return?mktime(info);
}
int??get_days(const?char*?from,?const?char*?to)
{
int?year,month,day,fromSecond,toSecond;
sscanf(from,"%d-%d-%d",year,month,day);
fromSecond=(int)convert(year,month,day);
sscanf(to,"%d-%d-%d",year,month,day);
toSecond=(int)convert(year,month,day);
return?(toSecond-fromSecond)/24/3600;
}
From:2013-3-15
To:2015-8-14
882
Press?any?key?to?continue
這才算是用了庫函數了···
#include?stdio.h
const?int?days[13]?=?{0,?31,?28,?31,?30,?31,?30,?31,?31,?30,?31,?30,?31};
struct?Date
{
int?year,?month,?day;
};
int?DateCmp(Date?a,?Date?b)
{
if?(a.year?!=?b.year)
return?a.year??b.year;
if?(b.month?!=?a.month)
return?a.month??b.month;
return?a.day??b.day;
}
int?isLunar(int?year)
{
return?year?%?400?==?0?||?(year?%?100??year?%?4?==?0);
}
int?DateDistace(Date?a,?Date?b)
{
if?(DateCmp(a,?b))//保證a小于b
{
Date?t?=?b;
b?=?a;
a?=?t;
}
//計算結果存儲在res中
//首先計算相差的年
int?i?=?a.year?+?1,?j?=?b.year?-?1,?res?=?a.year?==?b.year???0?:?(j?-?i?+?1)?*?365;
for?(int?ii?=?(i?+?3)?/?4?*?4,?jj?=?j?/?4?*?4;ii?=?jj;ii?+=?4,?res++);
for?(int?ii?=?(i?+?99)?/?100?*?100,?jj?=?j?/?100?*?100;ii?=?jj;ii?+=?100,?res--);
for?(int?ii?=?(i?+?399)?/?400?*?400,?jj?=?j?/?400?*?400;ii?=?jj;ii?+=?400,?res++);
//然后計算相差的月
if?(a.year?!=?b.year)
{
for?(int?ii?=?a.month?+?1;ii?=?12;ii++)
res?+=?ii?==?2???28?+?isLunar(a.year)?:?days[ii];
for?(int?ii?=?1;ii?=?b.month?-?1;ii++)
res?+=?ii?==?2???28?+?isLunar(b.year)?:?days[ii];
}
else
{
for?(int?ii?=?a.month?+?1;ii??b.month;ii++)
res?+=?ii?==?2???28?+?isLunar(a.year)?:?days[ii];
}
//最后計算相差的日
if?(a.year?!=?b.year?||?a.month?!=?b.month)
{
res?+=?(a.month?==?2???28?+?isLunar(a.year)?:?days[a.month])?-?a.day?+?b.day;
}
else
{
res?+=?b.day?-?a.day;
}
return?res;
}
int?main()
{
Date?a,?b;
a.year?=?2000;?a.month?=?12;?a.day?=?31;
b.year?=?1000;?b.month?=?1;?b.day?=?1;
printf("%d\n",?DateDistace(b,?a));
return?0;
}
文章標題:c語言函數天數計算,用函數計算天數
瀏覽地址:http://muchs.cn/article34/hssgse.html
成都網站建設公司_創(chuàng)新互聯,為您提供品牌網站制作、網站建設、網站策劃、服務器托管、手機網站建設、虛擬主機
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯