日期间隔计算器,连续两天间隔记为一天
#include<stdio.h>
//日期暴力计算
const int maxn = 20;
int mon[maxn] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
struct Date { int y, m, d; }now;
struct Datel { int y, m, d; }future;
//闰年判断
int is_run(int year)
{
if ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return 1;
return 0;
}
//主程序
int main()
{
printf("请输入您所要查询的开始日期:");
scanf_s("%d %d %d", &now.y, &now.m, &now.d);
printf("请输入您所要查询的结束日期:");
scanf_s("%d %d %d", &future.y, &future.m, &future.d);
int n1 = 0, n2 = 0, n = 0;
if (now.m >= 3)
{
for (int i = 3; i < now.m; i++)
{
n1 += mon[i];
}
n1 += now.d;
n1 = (-n1);
}
else
{
for (int i = 1; i < now.m; i++)
{
n1 += mon[i];
}
n1 += now.d;
if (is_run(now.y))
{
n1 = 60 - n1;
}
else
{
n1 = 59 - n1;
}
}
if (future.m >= 3)
{
for (int i = 3; i < future.m; i++)
{
n2 += mon[i];
}
n2 += future.d;
}
else
{
for (int i = 1; i < future.m; i++)
{
n2 += mon[i];
}
n2 += future.d;
if (is_run(future.y))
{
n2 = 60 - n2;
}
else
{
n2 = 59 - n2;
}
n2 = (-n2);
}
for (int i = now.y + 1; i <= future.y; i++)
{
if (is_run(i))
{
n += 366;
}
else
{
n += 365;
}
}
n = n + n1 + n2;
printf("日期间隔天数为%d", n);
return 0;
}
学习记录用,代码有待完善。。