Write a function days to implement the main function to pass the year, month, and day (structure type) to the days function. The days function calculates that the month and day of the year is the day of the year and returns the output of the main function

Write a function days to realize that the main function passes the year, month, and day (structure type) to the days function. The days function calculates that the month and day of the year is the day of the year and returns the output of the main function.
The running example of the program is as follows:
Please enter the date (year, month, day)
1990,2,14
February 14 is the 45th day of 1990.

Input format:
"Please enter the date (year, month, day)\n"
"%d,%d,%d"
output format:
"\n%d month%d day is the %d day of %d year."

Code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

int Month[12] = {
    
     31,28,31,30,31,30,31,31,30,31,30,31 };
int IsLeap(int year)
{
    
    
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		return 1;
	else
		return 0;
}
int days(int year, int month, int day)   //函数功能---计算天数
{
    
    
	int i;
	int sum = 0;
	for (i = 0; i < month-1; i++)
	{
    
    
		sum += Month[i];
	}
	sum += day;      //加上当月天数
	if (IsLeap(year) == 1 && month > 2)
	{
    
    
		sum++;
	}
	return sum;
}
int main()
{
    
    
	int year, month, day;
	printf("请输入日期(年,月,日)\n");
	scanf("%d,%d,%d", &year, &month, &day);
	int ret = days(year, month, day);
	printf("\n%d月%d日是%d年的第%d天。", month, day, year, ret);
}

Guess you like

Origin blog.csdn.net/qq_52698632/article/details/113745691