c语言函数天数计算,用函数计算天数
C语言编写days函数 计算该日是该年的第几天
#includestdio.h
创新互联建站专业为企业提供石龙网站建设、石龙做网站、石龙网站设计、石龙网站制作等企业网站建设、网页设计与制作、石龙企业网站模板建站服务,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;
}
用c语言编写一个函数,功能是算出每个月的天数
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;
}
}
c语言 用库函数计算两日期相差的天数
#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
这才算是用了库函数了···
C语言编程:定义一个结构体用于存储年、月、日数据,并定义一个函数用于求两个日期之间的天数。
#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://ybzwz.com/article/hssgse.html