C language, use a struct to read two dates in the same year, determine whether the date is legal, and calculate the number of days between two dates. The structure is defined as follows:

Source: Internet
Author: User


as follows:
typedef struct date
{
int year;
int month;
int day;
};
prompt:
1. Use the following function to complete the corresponding function
int isleapyear (int y); // Calculate whether it is a leap year
int islegal (Date x); // Calculate whether the date is legal
int calcday (Date x); // Calculate the date is the day of the year, used to calculate the difference between the number of days between the two dates
2. Used for one-dimensional array to represent the number of days in each month of the year
int dayofmonth [12] = {31,28,31,30,31,30,31,31,30,31,30,31};
3. Need to consider the situation in leap years

Input: The input is two lines. The first line is three integers representing the year, month, and day of the first date; the second line is three integers, representing the year, month, and day of the second date.
Enter the prompt: "Please input the first date:"
Input format: "% d% d% d"
Enter the prompt: "Please input the second date:"
Input format: "% d% d% d"
Such as:
Please input the first date: 2016 2 30
Please input the second date: 2016 5 10
or
Please input the first date: 2016 2 10
Please input the second date: 2015 12 9
or
Please input the first date: 2016 5 1
Please input the second date: 2015 6 1

Output: The output line is an integer representing the number of days between the two dates. If the two dates are not in the same year, or the dates are not valid, -1 is output.
Output prompt message: "Day between two dates:"
Output format: "% d"
Sample output:
Day between two dates: -1
or
Day between two dates: -1
or
Day between two dates ::-1






#include <stdio.h>
#include <math.h>
typedef struct DATE
{
int year;
int month;
int day;
}date;
int isleapyear (int y);
int Islegal (Date a);
int Calcday (Date a);



int main () {
int i=1;



Date A;
Date b;
printf ("Please input the first date:");
scanf ("%d%d%d",& (a.year),& (a.month),& (a.day));
printf ("Please input the second date:");
scanf ("%d%d%d",& (b.year),& (b.month),& (b.day));
printf ("Day between-dates:");



if (a.year! = b.year) {
printf ("-1");
}else if (Islegal (a) ==-1 | | Islegal (b) ==-1) {
printf ("-1");
}else{
printf ("%d", Calcday (b)-calcday (a));
}



}
int isleapyear (int y) {
int i=0;
if ((y%4==0 && y%100!=0) | | (y%400==0)) {
I=1;
}
return i;
}
int Islegal (struct date a) {
int dayofmonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
if (Isleapyear (a.year) ==1) {
dayofmonth[1]++;
}
if (A.day<=dayofmonth[a.month-1] && a.day>0 && a.month>0 && a.month<13) {
return 1;
}else{
return-1;
}



}
int Calcday (struct date a) {
int i,day=0;
int dayofmonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
if (Isleapyear (a.year) ==1) {
dayofmonth[1]++;
}
for (i=0;i<=a.month-2;i++) {
Day=dayofmonth[i]+day;
}
Day=day+a.day;
Return day;
}



C language, use a struct to read two dates in the same year, determine whether the date is legal, and calculate the number of days between two dates. The structure is defined as follows:


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.