Question 15: Define a struct variable (including year, month, and day), input the birthdays of two people, and find out how many days they differ.

Source: Internet
Author: User

/*************************************** ************************
C Language

AUTHOR: liuyongshui
 
**************************************** ***********************/
/*
Question 15: Define a struct variable (including year, month, and day), input the birthdays of two people, and find out how many days they differ.
*/

# Include <stdio. h>

Struct date
{
Int year;
Int month;
Int day;
};

Int checkyear (int n); // statement of the original function, used to check whether it is a leap year or a flat year. If it is 'distinct', 1 is returned; otherwise, 0 is returned.

Const int Day_Of_Month [] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31 }; // The number of days of each month on a yearly basis

Int main ()
{
Int I;
Int j;
Int flag;
Int sum_day [2] = {0, 0 };

Struct date person_birthday [2];

Printf ("Enter the date of birth of two people \ n ");
For (I = 0; I <2; I ++)
{
Printf ("the date of birth of person % d:", I + 1); // print the person

Scanf ("% d", & person_birthday [I]. year,
& Amp; person_birthday [I]. month,
& Person_birthday [I]. day );
}
For (I = 0; I <2; I ++)
{
Flag = checkyear (person_birthday [I]. year );
If (flag) // if the flag is distinct, 1 is returned, and 0 is returned.
{
Sum_day [I] ++;
}

For (j = 0; j <person_birthday [I]. month; j ++)
{
Sum_day [I] + = Day_Of_Month [j];
}

Sum_day [I] + = person_birthday [I]. day;

}
For (I = 0; I <2; I ++)
{
Printf ("output % d person's birthday % d day \ n", I + 1, sum_day [I]);
}
If (sum_day [0]> sum_day [1])
{
Printf ("% d days \ n", sum_day [0]-sum_day [1]);
}
Else
{
Printf ("% d days \ n", sum_day [1]-sum_day [0]);
}
Return 0;
}


// Function Definition
Int checkyear (int n)
{
If (n % 4 = 0 & n % 100! = 0) | n % 400 = 0) // It is a leap year.
Return 0;
Else // year
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.