- 论坛徽章:
- 0
|
// 我保证超级的准确, 哈哈
#include <iostream>;
using namespace std;
bool isleap(int y)
{ return y%4==0&&y%100!=0 || y%400==0;
}
bool is_correct_date(int d, int m, int y)
{
if(
(y<1||m<1||m>;12||d<1||d>;31) ||
((m==4||m==6||m==9||m==11)&&d>;30) ||
(m==2 && isleap(y) && d>;29) ||
(m==2 && !isleap(y) && d>;28 )
)
return false;
return true;
}
void swap(int& a,int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}
//input a date and increase by one day
void increase_one_day(int& d, int& m, int& y)
{
d++;
if(
(((m==1||m==3||m==5||m==7||m==8||m==10)&&d==32))
|| ((m==4||m==6||m==9||m==11)&&d==31)
|| (m==2 && isleap(y) && d==30)
|| (m==2 && !isleap(y) && d==29)
)
{
m++;
d=1;
}
else if(m==12&&d==32)
{
y++;
m=1;
d=1;
}
}
//input : day, month, year and the days increased
void after_n_days(int& d, int& m, int& y, int n_days)
{
while(n_days>;0)
{
increase_one_day(d, m, y);
n_days--;
}
}
//return the days between two date
int days_between_two_date(int d1, int m1, int y1,
int d2, int m2, int y2)
{
int days = 0;
if(y1<y2 || (y1==y2&&m1<m2) || (y1==y2&&m1==m2&&d1<d2))
{
swap(y1, y2);
swap(m1, m2);
swap(d1, d2);
}
while(y2!=y1 || m2!=m1 || d2!=d1)
{
increase_one_day(d2, m2, y2);
days++;
}
return days;
}
int main()
{
int d1, m1, y1, d2, m2, y2;
cout<<"Enter date_1(day month year): ";
cin>;>;d1>;>;m1>;>;y1;
cout<<"Enter date_2(day month year): ";
cin>;>;d2>;>;m2>;>;y2;
cout<<endl;
if(!is_correct_date(d2,m2,y2)||!is_correct_date(d1,m1,y1))
cout<<"Error Date Exist!"<<endl;
else
{
cout<<"There are "<<days_between_two_date(d1, m1, y1, d2, m2, y2)
<<" days between this two date."<<endl; cout<<endl;
int days;
cout<<"Enter the days you need to increase:";
cin>;>;days;
after_n_days(d1, m1, y1, days);
cout<<"date_1 after "<<days<<" days is "<<d1<<" "<<m1<<" "<<y1<<" "<<endl;
}
} |
|