关于求两个日期之间相距的天数,C/C++
我的程序达不到预期的效果,麻烦大家帮我看一下了,谢谢!#include<iostream>
using namespace std;
struct Day
{
int year;
int month;
int day;
}x,y;
void Ready()
{
cout<<"请输入一个日期:"<<endl;
cin>>x.year>>x.month>>x.day;
cout<<"请输入一个日期:"<<endl;
cin>>y.year>>y.month>>y.day;
}
double SW(int month,double days)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days+=31;break;
case 4:
case 6:
case 9:
case 11:days+=30;break;
case 2:days+=28;break;
default:cout<<"Error!"<<endl;break;
}
return days;
}
double Days(Day x,Day y)
{
int i;
double days=y.day;
days-=x.day;
if((x.year==y.year)&&(x.month==y.month)&&(x.day==y.day))
return 0;
if(x.year>y.year||((x.year==y.year)&&(x.month>y.month))||((x.year==y.year)&&(x.month==y.month)&&(x.day>y.day)))
{
if((x.year%400==0||(x.year%100!=0&&x.year%4==0))&&x.month<=2)
days++;
for(i=x.month;i<=12;i++)
days=SW(i,days);
if(x.year-y.year>1)
for(i=x.year+1;i<y.year;i++)
if(i%400==0||(i%100!=0&&i%4==0))
days+=366;
else
days+=365;
return days;
}
else
return Days(y,x);
}
void Show(double n)
{
cout<<n<<endl;
}
void main()
{
Ready();
Show(Days(x,y));
}