68 lines
1.6 KiB
C
68 lines
1.6 KiB
C
// 编写程序并设计函数,接收年、月、日三个整数,函数返回是这一年的第几天 【提示】使用数组的方式计算,将每月的天数放在一个数组中
|
|
#include <stdio.h>
|
|
|
|
int todayIs(int year, int month, int day); // 数组加 switch-case 穿透方法
|
|
int todayIsFun2(int year, int month, int day); // 数组循环方法
|
|
|
|
int main()
|
|
{
|
|
int y, m, d;
|
|
printf("请输入年,月,日(用,分隔): ");
|
|
scanf("%d,%d,%d", &y, &m, &d);
|
|
printf("今天是 %d 年的第 %d 天\n", y, todayIsFun2(y, m, d));
|
|
|
|
return 0;
|
|
}
|
|
|
|
int todayIs(int year, int month, int day)
|
|
{
|
|
int days = day;
|
|
int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
switch (month)
|
|
{
|
|
case 12:
|
|
days += months[10];
|
|
case 11:
|
|
days += months[9];
|
|
case 10:
|
|
days += months[8];
|
|
case 9:
|
|
days += months[7];
|
|
case 8:
|
|
days += months[6];
|
|
case 7:
|
|
days += months[5];
|
|
case 6:
|
|
days += months[4];
|
|
case 5:
|
|
days += months[3];
|
|
case 4:
|
|
days += months[2];
|
|
case 3:
|
|
days += months[1];
|
|
case 2:
|
|
days += months[0];
|
|
}
|
|
|
|
// 判断是否闰年?加一天:不加
|
|
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
|
|
days += 1;
|
|
|
|
return days;
|
|
}
|
|
|
|
int todayIsFun2(int year, int month, int day)
|
|
{
|
|
int days = day;
|
|
int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
for (int i = 0; i < month - 1; i++)
|
|
{
|
|
days += months[i];
|
|
}
|
|
|
|
// 判断是否闰年?加一天:不加
|
|
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
|
|
days += 1;
|
|
|
|
return days;
|
|
} |