计算任意两个日期的天数
在线等 编写一个C程序计算任意两个日期之间间隔天数,要求以yyyy-mm-dd的格式不分先后输入两个有效日期,计算他们之间的间隔天数。
回复 4楼 wp231957
我写的这个代码问题出在哪点了?#include<stdio.h>
int p;
int ping[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int year_allday(int year)
{
if((year%4==0)&&(year%100!=0))
{
p=366;
ping[2]=29;
}
else
if(year%400==0)
{
p=366;
ping[2]=29;
}
else
p=365;
return p;
}
void swap(int *x,int *y)
{
int t;
t=*y;
*y=*x;
*x=t;
}
int main()
{
int year1,year2,month1,month2,day1,day2,count_days=0;
int years,i,*pointer1,*pointer2;
pointer1=&year1;
pointer2=&year2;
printf("请以yyyy-mm-dd的形式输入年份:\n");
scanf("%d-%d-%d",&year1,&month1,&day1);
printf("请以yyyy-mm-dd的形式输入年份:\n");
scanf("%d-%d-%d",&year2,&month2,&day2);
year_allday(year1);
for(int m=0;m<month1+1;m++)
count_days+=ping[m];
count_days+=day1;
year_allday(year2);
for(int n=0;n<month2;n++)
count_days-=ping[n];
count_days-=day2;
if(year1>year2)
swap(pointer1,pointer2);
for(int y=year1;y<year2;y++)
{
year_allday(y);
count_days+=p;
}
printf("相差了%d天\n",count_days);
}