These two days of love Anniversaries are approaching, I want to write a program to calculate the number of days that any two dates are directly separated by, think about it, the input is two dates, the output is a number of days problem, should be simple, the result is to change to a state of mind collapse, of course, this topic is not difficult, but there are many details really upsetting, and do not want to waste the last time I write it. Share it for your amusement only.

Explain several assumptions:

1. The input format is reshaped, such as January 1, 2020, when you enter January 11, 2020.

2. Enter dates separately by spaces, such as 2020 1 1 2021 1 1

3. For ease of calculation, we stipulate that the first date is less than the second date

4. Due to design problems, the year should not be less than 1949. Of course, you can modify this data.

5. It is stipulated that January 1, 2020 will be one day from January 2, 2020, but you can also set it to 10 days.

Solving ideas:

- Read user date
- Determine whether the date is legal and prevent the calculation from being affected (if not, prompt the user to retype and retrieve the data)
- Call the calculation function to return the number of days apart

{

1. Define related data

2. Set two integer variables l_day and r_day, which stores two dates separately from 1949.01.01

3. Use r_day minus l_day is the number of days apart

}

Determining whether it is a leap year function

bool isleapyear(int year) { return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ) ? true:false; }

Determine whether date input is legal function

bool islegal(int x1,int y1,int z1,int x2,int y2,int z2) { int month_day[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Start to determine whether the date of month, month, and year is legal, while ensuring that the first date is less than the second if(x1<=1949 || y1<=0 || z1<=0 || y1>12) return false; if(x2<=1949 || y2<=0 || z2<=0 || y2>12) return false; if(x1>x2 || (x1=x2 &&y1>y2)) return false; month_day[2]=(isleapyear(x1))? 29:28; if(z1-month_day[y1]>0) return false; month_day[2]=(isleapyear(x2))? 29:28; if(z2-month_day[y2]>0) return false; //When the above judgment is all right, return legitimate, that is, true return true; }

Calculate Days Interval Function

int Calculate(int x1,int y1,int z1,int x2,int y2,int z2) { //Define the number of days in a month and also define the first and second days apart int month_day[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; int l_day=0,r_day=0; //Calculate the year of the middle difference, if it is leap year + 366, otherwise + 365 for(int i=1949;i<x1;i++) l_day+=(isleapyear(i))? 366:365; //If it is a leap year, February should be changed to 29 days, then the number of days in the month with the middle difference will be added month_day[2]=(isleapyear(x1))?29:28; for(int i=1;i<y1;i++) l_day+=month_day[i]; //Finally add the difference of days and remember to subtract 1, because how many days is it from 1 l_day=l_day+z1-1; month_day[2]=(!isleapyear(x2))? 28:29; for(int i=1949;i<x2;i++) r_day+=(isleapyear(i))? 366:365; for(int i=1;i<y2;i++) r_day+=month_day[i]; r_day=r_day+z2-1; //Then you return the two differences, which are how many days apart they are return r_day-l_day; }

main function

int main() { int first_year,first_month,first_date,second_year,second_month,second_date; cout<<"Please enter two dates and we will output how many days they are apart"<<endl; cin>>first_year>>first_month>>first_date>>second_year>>second_month>>second_date; while(!islegal(first_year,first_month,first_date,second_year,second_month,second_date)) { cout<<"Your input is wrong, please re-enter!!!"<<endl; cin>>first_year>>first_month>>first_date>>second_year>>second_month>>second_date; } cout<<Calculate(first_year,first_month,first_date,second_year,second_month,second_date)<<" day"<<endl; return 0; }

In fact, this topic is really not difficult, but there are many ways to have a very complex classification discussion. Personally, I think this is the easiest to understand and calculate. If there is something wrong or worth upgrading, you are welcome to point out!

2021.11.13 01:29