c语言用函数计算天数 计算天数c语言pta
c语言 用库函数计算两日期相差的天数
#include stdio.h
成都创新互联公司成都企业网站建设服务,提供成都网站设计、成都做网站网站开发,网站定制,建网站,网站搭建,网站设计,成都响应式网站建设公司,网页设计师打造企业风格网站,提供周到的售前咨询和贴心的售后服务。欢迎咨询做网站需要多少钱:18980820575
#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语言计算天数
#includestdio.h
int main()
{
int yyyy,mm,dd,a,b;
scanf("%d/%d/%d",yyyy,mm,dd);
a=31*(mm1)+28*(mm2)+31*(mm3)+30*(mm4)+31*(mm5)+30*(mm6)+31*(mm7)+31*(mm8)+30*(mm9)+31*(mm10)+30*(mm11)+dd;
b=((yyyy%4==0)*(yyyy%100!=0)+(yyyy%400==0))*(mm2);
printf("%d\n",a+b);
return 0;
}
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语言编写一个函数,功能是算出每个月的天数
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语言用函数计算天数 计算天数c语言pta
网站地址:http://azwzsj.com/article/dojjecd.html