Defines a struct variable (including year, month, and day ). Is the day of the current year counted? Pay attention to the leap year issue.

Source: Internet
Author: User
This question does not include the correct date judgment function # include <stdio. h>

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

Int leap_year (int );
Int cal_day (struct date );

/* Determine a leap year */
Int leap_year (int)
{
If (a % 400 = 0 | (a % 4 = 0 & A % 100! = 0 ))
Return 1;
Else
Return 0;

// If (a % 400 = 0)
// Return 1;
// Else if (a % 100 = 0)
// Return 0;
// Else if (a % 4 = 0)
// Return 1;
// Else
// Return 0;
}

/* Calculate the day of the year */
Int cal_day (struct date)
{
Int sum = 0, B [] = {31,28, 31,30, 31,30, 31,31, 30,31, 30,31 };
For (INT I = 0; I <A. Month-1; I ++)
Sum + = B [I];
If (A. Month> 2)
Sum = sum + A. day + leap_year (A. year );
Else
Sum = sum + A. Day;
Return sum;
}

Void main ()
{
Struct date;
Int N;
Printf ("\ n enter the date (year month day) \ n ");
Scanf ("% d", & A. Year, & A. Month, & A. Day );
N = cal_day ();
Printf ("this day in this year is % d day \ n", N );
}

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.