题目要求:
1、定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
2、用day函数实现第一题的计算,由主函数将年月日传递给day函数,计算后将日子数传回主函数并输出
输入:
2024 8 21
输出:
该日是2024年中第234天
程序1:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
struct date
{
int year;
int month;
int days;
};
int main()
{
int days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
struct date d;
scanf("%d%d%d", &d.year, &d.month, &d.days);
if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0)
{
days[1]++;
}
int i,sum=0;
for (i = 0; i < d.month-1; i++)
{
sum = sum + days[i];
}
sum = sum + d.days;
printf("该日是%d年中第%d天",d.year,sum);
}
程序2:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
struct date
{
int year;
int month;
int day;
};
int main()
{
int day(int year, int month, int day);
struct date d;
scanf("%d%d%d", &d.year, &d.month, &d.day);
int result = day(d.year,d.month,d.day);
printf("今天是%d年第%d天", d.year, result);
return 0;
}
int day(int year, int month, int day)
{
int days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
days[1]++;
}
int i, sum = 0;
for (i = 0; i < month - 1; i++)
{
sum += days[i];
}
sum += day;
return(sum);
}