C语言小程序 如何判断两个日期之差
来源:本站原创|时间:2020-01-10|栏目:C语言|点击: 次
1.普通的写法
复制代码 代码如下:
#include <stdio.h>
int leapyear(int year)
{
if((year%4==0 && year%100!=0) || year%400==0)
return 1;
else
return 0;
}
int days(int *day1, int *day2)
{
int i=0;
int *tmp;
int diff = 0;
const int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(day1[0] == day2[0])
{
if(day1[1] == day2[1])
{
diff = day1[2] - day2[2];
diff = (diff < 0)?(-diff):diff;
}
else
{
if(day1[1] < day2[1]) //day1=1991-5-8 day2=1991-6-2
{
tmp = day1; //day1=1991-6-2 day2=1991-5-8
day1 = day2;
day2 = tmp;
}
for(i=day2[1]+1; i<day1[1]; i++)
{
diff += month[i];
}
diff += month[day2[1]] - day2[2] + day1[2];
if(day2[1] <= 2 && day1[1] >2)
if(leapyear(day2[0]))
diff++;
}
}
else
{
if(day1[0] < day2[0])
{
tmp = day1;
day1 = day2;
day2 = tmp;
}
for(i=day2[0]+1; i<day1[0]; i++)
{
if(leapyear(i))
diff += 366;
else
diff += 365;
}
for(i=day2[1]+1; i<=12; i++) //day1=1992-1-1 day2=1991-1-1
{
diff += month[i];
}
diff += (month[day2[1]] - day2[2]);
if(day2[1] <= 2)
if(leapyear(day2[0]))
diff++;
for(i=1; i<day1[1]; i++)
{
diff += month[i];
}
diff += day1[2];
if(day1[1] > 2)
if(leapyear(day1[0]))
diff++;
}
return diff;
}
int main()
{
int day1[3], day2[3];
int day = 0;
printf("输入日期:");
scanf("%d-%d-%d",&day1[0], &day1[1], &day1[2]);
printf("输入另一个日期:");
scanf("%d-%d-%d",&day2[0], &day2[1], &day2[2]);
day = days(day1, day2);
printf("两个日期之间共有%d天。\n",day);
return 0;
}
2.利用结构体,代码更整洁一些
复制代码 代码如下:
#include <stdio.h>
typedef struct date
{
int year;
int month;
int day;
}DATE;
int leapyear(int year)
{
if((year%4==0 && year%100!=0) || year%400==0)
return 1;
else
return 0;
}
int compare(DATE *d1, DATE *d2) //如果第一个日期比第二个日期大,交换日期
{
DATE *tmp;
if(d1->year == d2->year) //年数相等
{
if(d1->month > d2->month) //月数相等
{
tmp = d1;
d1 = d2;
d2 = d1;
}
else if(d1->month == d2->month) //日期相等
{
if(d1->day > d2->day)
{
tmp = d1;
d1 = d2;
d2 = d1;
}
}
}
else if(d1->year > d2->year)
{
tmp = d1;
d1 = d2;
d2 = tmp;
}
return 0;
}
int diff(DATE *date1, DATE *date2)
{
int i;
int diff = 0;
const int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(date1->year == date2->year)
{
if(date1->month == date2->month)
{
diff = date2->day - date1->day;
}
else
{
for(i=date1->month+1; i<date2->month; i++)
{
diff += month[i];
}
diff += month[date1->month] - date1->day + date2->day;
if(leapyear(date1->year))
if(date1->month <=2 && date2->month >2)
diff++;
}
}
else
{
for(i=date1->year+1; i<date2->year; i++)
{
if(leapyear(i))
diff += 366;
else
diff += 365;
}
for(i=date1->month+1; i<=12; i++) //date1距离年末多少天
{
diff += month[i];
}
diff += month[date1->month] - date1->day;
if(date1->month <= 2)
if(leapyear(date1->year))
diff++;
for(i=1; i<date2->month; i++) //date2距离年初多少天
{
diff += month[i];
}
diff += date2->day;
if(date1->month > 2)
if(leapyear(date2->year))
diff++;
}
return diff;
}
int main()
{
int days = 0;
DATE day1, day2;
DATE *date1, *date2;
date1 = &day1;
date2 = &day2;
printf("输入日期:");
scanf("%d-%d-%d",&(date1->year), &(date1->month), &(date1->day));
printf("输入另一个日期:");
scanf("%d-%d-%d",&date2->year, &date2->month, &date2->day);
compare(date1, date2);
days = diff(date1, date2);
printf("两个日期之间共有%d天。\n",days);
return 0;
}
上一篇:C语言关键字大全(共32个)
栏 目:C语言
本文标题:C语言小程序 如何判断两个日期之差
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/4283.html
您可能感兴趣的文章
- 04-02c语言函数调用后清空内存 c语言调用函数删除字符
- 04-02c语言的正则匹配函数 c语言正则表达式函数库
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言中对数函数的表达式 c语言中对数怎么表达
- 04-02c语言用函数写分段 用c语言表示分段函数
- 04-02c语言编写函数冒泡排序 c语言冒泡排序法函数
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段函数
- 04-02C语言中怎么打出三角函数 c语言中怎么打出三角函数的值
- 04-02c语言调用函数求fibo C语言调用函数求阶乘
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-11ajax实现页面的局部加载
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10C#中split用法实例总结
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10SublimeText编译C开发环境设置
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 04-02jquery与jsp,用jquery